All Products
Search
Document Center

PolarDB:Global temporary tables

Last Updated:Mar 28, 2026

PolarDB for PostgreSQL (Compatible with Oracle) supports global temporary tables and native PostgreSQL local temporary tables. Unlike native PostgreSQL temporary tables—where each session must create its own copy—a global temporary table has a shared definition that all sessions can use, while the data each session writes remains private to that session.

How it works

Global temporary tables have a split identity: the table definition is shared, but the data is not.

AspectBehavior
Table definitionShared across all sessions. When one session creates a global temporary table, other sessions can use it immediately.
DataPrivate to each session. A session can only read and write its own data.
Session cleanupWhen a session exits, its data and underlying storage are cleared automatically.
StatisticsTable-level and column-level statistics are private to each session, so the query planner generates accurate plans for each session's workload.

To drop a global temporary table, you must ensure that the global temporary table is used only in the current session.

Supported operations include JOIN with other tables, B-tree index creation, and index scans. Only B-tree indexes are supported.

Manual VACUUM and ANALYZE operations are supported to remove dead tuples and collect statistics.

Syntax

CREATE GLOBAL TEMPORARY|TEMP TABLE table-name
    { column-definition [ , column-definition ] * }
[ ON COMMIT {DELETE | PRESERVE} ROWS ]

ON COMMIT behavior

The ON COMMIT clause controls what happens to session data when a transaction ends.

ON COMMIT DELETE ROWS (default)

Creates a transaction-scoped temporary table. Data is deleted at the end of each transaction. If you omit the ON COMMIT clause, this is the default behavior.

ON COMMIT PRESERVE ROWS

Creates a session-scoped temporary table. Data persists across transactions within the same session and is cleared when the session ends.

ON COMMIT DROP is not supported.

Examples

Create a global temporary table with the default ON COMMIT DELETE ROWS behavior:

create global temp table gtt1(a int primary key, b text);

Explicitly specify ON COMMIT DELETE ROWS:

create global temporary table gtt2(a int primary key, b text) on commit delete rows;

Use ON COMMIT PRESERVE ROWS to retain data across transactions within a session:

create global temp table gtt3(a int primary key, b text) on commit PRESERVE rows;

Utility functions

These functions are packaged as an extension. Install it before use:

create extension polar_gtt;
FunctionDescription
polar_gtt_attached_pidReturns the sessions currently using a global temporary table. Use this during operations and maintenance.
polar_gtt_att_statisticReturns column-level statistics for a global temporary table.
polar_gtt_relstatsReturns table-level statistics for a global temporary table.

Drop a global temporary table

To drop a global temporary table, you must ensure that the global temporary table is used only in the current session.

To drop a global temporary table:

  1. Run polar_gtt_attached_pid to list all sessions using the table.

  2. Run pg_backend_pid() to get the process ID (PID) of the current session.

  3. For each session that is not the current session, run pg_terminate_backend(pid) to terminate it.

  4. Run DROP TABLE to drop the global temporary table.