Skip to main content
CockroachDB supports session-scoped temporary tables (also called “temp tables”). Unlike , temp tables can only be accessed from the session in which they were created, and they are dropped at the end of the session. To create a temp table, add TEMP/TEMPORARY to a or statement. For full syntax details, see the and pages. For example usage, see Examples.
By default, temp tables are disabled in CockroachDB. To enable temp tables, set the experimental_enable_temp_tables to on.
CockroachDB also supports and .

Details

  • Temp tables are automatically dropped at the end of the session.
  • A temp table can only be accessed from the session in which it was created.
  • Temp tables persist across transactions in the same session.
  • Temp tables can reference persistent tables, but persistent tables cannot reference temp tables.
  • Temp tables cannot be converted to persistent tables.
  • For PostgreSQL compatibility, CockroachDB supports the clause ON COMMIT PRESERVE ROWS at the end of CREATE TEMP TABLE statements. CockroachDB only supports session-scoped temp tables, and does not support the clauses ON COMMIT DELETE ROWS and ON COMMIT DROP, which are used to define transaction-scoped temp tables in PostgreSQL.
By default, every 30 minutes CockroachDB cleans up all temporary objects that are not tied to an active session. You can change how often the cleanup job runs with the sql.temp_object_cleaner.cleanup_interval .

Performance considerations

  • Temporary tables are not optimized for performance. They use the same underlying mechanisms as “regular” tables, and may be slower than expected compared to alternatives such as .
  • Avoid patterns that create and drop very large numbers of temp tables in rapid succession. Creating and dropping large numbers of temp tables can enqueue many and degrade overall cluster performance.
  • Prefer for intermediate results where possible. If you do use temp tables instead of CTEs, consider reusing a small set of temp tables with instead of repeatedly creating and dropping new ones. Always test both approaches for your workload.

Temporary schemas

Temp tables are not part of the public schema. Instead, when you create the first temp table for a session, CockroachDB generates a single temporary schema (pg_temp_<id) for all of the temp tables, , and in the current session for a database. In a session, you can reference the session’s temporary schema as pg_temp.
Because the statement defaults to the public schema (which doesn’t include temp tables), using SHOW TABLES without specifying a schema will not return any temp tables.

Examples

For intermediate results, consider using instead of temp tables. For more information, see Performance considerations.
To use temp tables, you need to set experimental_enable_temp_tables to on:

Create a temp table

You can use to view temp tables:
To show the newly created pg_temp schema, use :

Create a temp table that references another temp table

To create another temp table that references users:

Show all temp tables in a session

To show all temp tables in a session’s temporary schema, use SHOW TABLES FROM pg_temp:
You can also use the full name of the temporary schema in the SHOW statement (e.g., SHOW TABLES FROM pg_temp_1602087923187609000_1).

Show temp tables in information_schema

Although temp tables are not included in the public schema, metadata for temp tables is included in the and pg_catalog schemas. For example, the table includes information about all tables in all schemas in all databases, including temp tables:

Cancel a session

If you end the session, all temp tables are lost.

See also

  • .