mariadb/storage/innobase
Nikita Malyavin e2e79ad916 MDEV-35915 Implement Global temporary tables
Global temporary table (GTT) is a table with globally defined metadata,
but per-session data.

That is, the life duration of this data is limited with session
duration. Here we have two options:

1. ON COMMIT DELETE ROWS: a default option. The life duration of data is
transaction duration.
2. ON COMMIT PRESERVE ROWS: The life duration of data is session
duration.

# Implementation

The implementation consists of the following fundamental parts.

## Part 1: CREATE TABLE + frm format

1. Store GLOBAL TEMPORARY and ON COMMIT bits in
   create_info->table_options
2. Store higher word of create_info->table_options in frm header
3. Also disallow ON COMMIT keyword for (local) temporary tables

As a result, the bits will be available in share->db_create_options.

New Table_type enum element: TABLE_TYPE_GLOBAL_TEMPORARY (datadict.h)

## Part 2: open_table: a local data table is created on demand

On open, a session-local temporary table copy is created from a global
TABLE_SHARE. Let us refer to the former as "local data table of GTT",
and the latter as "global metadata table of GTT".

There is a number of cases, when a global metadata table is opened
instead, like ALTER, DROP, SHOW CREATE

A local data table holds:

1. A reference to that share, and holds the increased
tdc reference count.
2. An explicit MDL lock to the duration of local copy's
existence.

While at least one local data table exists
(2) guarantees that ALTER, DROP or RENAME will not happen.
(1) guarantees that a global metadata table will not be flushed

At an implementation level, a local data table is a temporary table,
invisible for a user and augmented with (1) and (2).

The reference and lock are released upon dropping this local data table,
which happens due to:
a) TRUNCATE TABLE
b) Connection close
c) If ON COMMIT DELETE ROWS is used, then on transaction commit/rollback

## Part 3: ALTER, DROP, RENAME

These operations always open a real global temporary table.

ALTER, RENAME and DROP TABLE require all local data tables to be
destroyed, thus, having no references to a modified/deleted table.
For that, they:
1. acquire an EXCLUSIVE MDL lock. To match oracle behavior,
lock_wait_timeout is 0 in ALTER TABLE.
2. Check that there is no matching local temporary table in the same
connection (which is not guaranteed by 1)

## Part 4 ON COMMIT DELETE ROWS

This clause is assumed by default, i.e. it is implicit.

When ON COMMIT DELETE ROWS, the local data table is dropped on
commit/rollback.

On table open, global_temporary_tables handlerton is injected,
implementing this behavior.

# Limitations

The following is forbidden for GTT:
system versioning
partitioning
fk
ONLINE ALTER
CREATE GLOBAL TEMPORARY under LOCK TABLES
FLUSH TABLE on Global temporary table is no-op for now

# Refactorings, details

1. Extract drop_tmp_table_share (temporary_tables.cc)
2. Extract THD::open_temporary_table_impl (temporary_tables.cc)
3. information_schema support:
 x) "GLOBAL TEMPORARY" type is show in information_schema.tables
 y) an artifical temporary table is hidden from SHOW TABLE STATUS

4. VIEWs are supported.
5. AS SELECT support

 x) Made in Oracle style, so that the data is only inserted in the
 session that creates GTT
 y) ON COMMIT DELETE ROWS inserts the data for real, and then deletes
 the data table on implicit commit

6. CREATE TABLE ... LIKE is supported.

7. Replication support
 x) Set share->table_creation_was_logged to 0 to make the table ignored
 for replication.
 y) Statements with GTT involved will be logged in Row-based style
 (RBR)
 z) Global temporary table creation and alter/drop will be logged.
2025-08-12 17:54:52 +02:00
..
btr Merge branch '11.8' into 12.0 2025-06-18 07:50:39 +02:00
buf Merge branch '11.8' into 12.0 2025-06-18 07:50:39 +02:00
data MDEV-35049: Use CRC-32C and avoid allocating heap 2025-01-10 16:39:44 +02:00
dict Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
eval MDEV-26938 Support descending indexes internally in InnoDB 2022-01-26 18:43:05 +01:00
fil Merge branch '11.8' into 12.0 2025-06-18 07:50:39 +02:00
fsp Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
fts Fix typos in C comments inside storage/ 2025-03-26 16:56:50 +04:00
fut MDEV-35689 InnoDB system tables cannot be optimized or defragmented 2025-04-10 17:13:34 +05:30
gis Merge branch '11.8' into main 2025-04-18 17:11:01 +02:00
ha MDEV-35472 Server crash in ha_storage_put_memlim upon reading from INNODB_LOCKS 2024-11-25 10:31:57 +02:00
handler MDEV-35915 Implement Global temporary tables 2025-08-12 17:54:52 +02:00
ibuf MDEV-29445: Reimplement SET GLOBAL innodb_buffer_pool_size 2025-03-26 17:05:44 +02:00
include Merge branch '11.8' into 12.0 2025-06-18 07:50:39 +02:00
lock Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
log Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
mem MDEV-35049: btr_search_check_free_space_in_heap() is a bottleneck 2025-01-10 16:30:42 +02:00
mtr Merge branch '11.4' into 11.8 2025-05-21 15:57:16 +02:00
mysql-test/storage_engine Remove dates from all rdiff files 2025-01-05 16:40:11 +02:00
os Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
page Fix typos in C comments inside storage/ 2025-03-26 16:56:50 +04:00
pars Fix typos in C comments inside storage/ 2025-03-26 16:56:50 +04:00
que MDEV-24035 Failing assertion: UT_LIST_GET_LEN(lock.trx_locks) == 0 causing disruption and replication failure 2024-12-12 18:02:00 +02:00
read Fix typos in C comments inside storage/ 2025-03-26 16:56:50 +04:00
rem MDEV-35049: Use CRC-32C and avoid allocating heap 2025-01-10 16:39:44 +02:00
row Merge branch '11.8' into 12.0 2025-06-18 07:50:39 +02:00
srv Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
sync Merge 10.6 into 10.11 2024-10-03 10:55:08 +03:00
trx Merge branch '11.8' into 12.0 2025-06-18 07:50:39 +02:00
unittest Merge 11.4 into 11.8 2025-04-02 14:07:01 +03:00
ut Merge branch '11.8' into 12.0 2025-05-22 09:22:55 +02:00
.clang-format-old switch off storage/innobase/.clang-format: InnoDB uses a common formatting style for all new code 2021-03-17 11:01:15 +03:00
CMakeLists.txt Merge branch '11.4' into 11.8 2025-06-18 07:43:24 +02:00
COPYING.Google
COPYING.Percona