Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-35915

Implement Global temporary tables

    XMLWordPrintable

Details

    • Q4/2025 Server Development

    Description

      GLOBAL TEMPORARY TABLE is a feature found in Oracle, MSSQL, PostgreSQL etc.

      Syntax in Oracle:

      CREATE GLOBAL TEMPORARY TABLE tmp_table
      { (create_definition, ...)
        | [SELECT * FROM tbl_NAME WHERE conditions ] }
      ON COMMIT [DELETE | PRESERVE] ROWS
      

      The purpose of this syntax is to create temporary table where each user can have
      their own data, which is destroyed at the end of the session.
      Any user with access right to this table can access and use it.

      One suggested way to implement this in MariaDB is to have "CREATE GLOBAL TEMPORARY t1" create a 'template' for future temporary tables with name t1.
      Internally in MariaDB we would only create a .frm file for the table.
      On first access to the table, MariaDB would internally execute
      "CREATE TEMPORARY TABLE t1 LIKE t1" on the engine used when "CREATE GLOBAL ..." was created. Any future usage of t1 would use the temporary table t1.
      "DROP TABLE t1" would drop the internal temporary table.
      To drop the global one, one would have to use "DROP GLOBAL TEMPORARY TABLE".

      Existing implementations

      ORACLE

      ORACLE has GLOBAL TEMPORARY TABLE syntax as part of their normal CREATE TABLE definition.
      Both CREATE TABLE …SELECT and CREATE TABLE (create_definition…) work.
      https://docs.oracle.com/en/database/oracle/oracle-database/18/sqlrf/CREATE-TABLE.html#GUID-F9CE0CC3-13AE-4744-A43C-EAC7A71AAAB6__I2153132
      https://oracle-base.com/articles/misc/temporary-tables

      Examples:
      https://dbfiddle.uk/O8KdF2kw
      CREATE... SELECT: https://dbfiddle.uk/rY75s2Y5, https://dbfiddle.uk/7LipZHrC

      TRUNCATE TABLE behavior:
      When you truncate a temporary table, only the rows created during the current session are removed

      IBM DB2

      DB2 supports Declared Global Temporary Tables (DGTT), which are declared with a DECLARE GLOBAL TEMPORARY TABLE statement rather than CREATE …. The data is session-private.
      https://www.ibm.com/docs/en/db2-for-zos/12?topic=statements-declare-global-temporary-table

      MS SQL Server

      Uses a concept of local (#table) and global (##table) temporary tables, but these are not the same as the standard’s “global temporary table”. They have their own rules about scoping and session handling.

      PostgreSQL

      Docs state that GLOBAL keyword has no effect.

      However, there is an Oracle compatibility module, pgtt, which adds support.

      But:

      PostgreSQL temporary table clause ON COMMIT DROP is not supported by the extension, GTT are persistent over transactions.

      There is also an ad-hoc trigger based suggestion.

      MS SQL

      Global temporary tables in MSSQL are created as a normal table with the a prefix of ## for the table name.

      Documentation notes

      GTTs doc page deserves a replication section.

      What should be written there is:

      Global temporary tables replicate only DDL statements. Data stored in Global temporary tables is never replicated. That is, DML statements involving Global temporary tables are forced to be replicated in the row mode.GTTs doc page deserves a replication section. What should be written there is:

      Global temporary tables replicate only DDL statements. Data stored in Global temporary tables is never replicated. That is, DML statements involving Global temporary tables are forced to be replicated in the row mode.

      Attachments

        Issue Links

          Activity

            People

              nikitamalyavin Nikita Malyavin
              monty Michael Widenius
              Nikita Malyavin Nikita Malyavin
              Michael Widenius Michael Widenius
              Roel Van de Paar Roel Van de Paar
              Stefan Hinz Stefan Hinz
              Votes:
              1 Vote for this issue
              Watchers:
              14 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.