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.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 ROWSat the end ofCREATE TEMP TABLEstatements. CockroachDB only supports session-scoped temp tables, and does not support the clausesON COMMIT DELETE ROWSandON COMMIT DROP, which are used to define transaction-scoped temp tables in PostgreSQL.
sql.temp_object_cleaner.cleanup_interval .
Temporary schemas
Temp tables are not part of thepublic 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
To use temp tables, you need to setexperimental_enable_temp_tables to on:
Create a temp table
pg_temp schema, use :
Create a temp table that references another temp table
To create another temp table that referencesusers:
Show all temp tables in a session
To show all temp tables in a session’s temporary schema, useSHOW TABLES FROM pg_temp:
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
- .

