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

Implement Global temporary tables

    XMLWordPrintable

Details

    • Q1/2026 Server Development, Q1/2026 Server Maintenance

    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.

      Architecture

      "CREATE GLOBAL TEMPORARY t1" creates a 'template' for future temporary tables with name t1.
      Internally in MariaDB we would only create a .frm file for the table. We denote this table as parent table.
      On first access to the parent table, MariaDB would internally execute
      "CREATE TEMPORARY TABLE t1 LIKE t1". This temporary table is denoted as a child table and is bound to the parent, it is not shown in temporary tables list anywhere in information schema, or otherwise.
      Any future DML of t1 would use the child table. There are a some commands that would intentionally use a parent table, like DDL.

      Child table, since it's temporary, exists only in current session. Its lifetime is transaction for ON COMMIT DELETE ROWS, or session lifetime, or until TRUNCATE is executed. TRUNCATE is the only way to manually drop the child table.

      A child table can't exist longer than its parent, so "DROP TABLE t1" would drop the global temporary table only if there is no data bound to this table from any session.
      This means, any existence of a child table would block DDL until all the bound child tables are dropped. This is implemented with child table holding an MDL-READ lock of the EXPLICIT duration (like HANDLER does) – more precisely, with a duration of that child table's existence.
      To conform Oracle behavior, where DROP TABLE exits immediately if it's blocked with some session-bound data, lock_wait_timeout is overriden to be 0 for DDL that Oracle has.

      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

      Naming ambiguity

      Now having global temporary tables, it becomes tricky to refer to /classic/ temporary tables due to naming ambiguity.
      Sql standard refers to classic ones (those we have for long, non-global) as local temporary tables
      Oracle uses a notion of PRIVATE temporary table for them.

      We generally prefer to stay standard-compatible. Perhaps, we should add use of PRIVATE keyword for extra compatibility (as a separate feature). However when it's ambiguous we should prefer referring to classic temporary tables as local temporary tables.

      How DDL beheves when GTT is in use

      When GTT is in use in some session (i.e. there is a data bound to that session), it keeps a lock on that GTT. So DDL would not go until all the data to all the sessions is freed (with truncate/commit). For Oracle compatibility, we internally set lock_wait_timeout=0, so all DDL act as if NOWAIT is specified.

      For now, WAIT x is not supported (though there is such syntax, so future support is possible).

      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. Statements involving Global temporary temporary tables are not suitable for statement-based replication (the advice is to use mixed or row-based). In statement-based replication, statements involving GTT will be forcefully replicated in row mode, with a warning.

      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:
              0 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.