1

pg_temp is an alias for current session's temporary-table schema.

Likewise, the current session's temporary-table schema, pg_temp_nnn, is always searched if it exists. It can be explicitly listed in the path by using the alias pg_temp.

https://www.postgresql.org/docs/current/runtime-config-client.html#id-1.6.7.14.2.2.2.1.4

How does PostgreSQL resolve the pg_temp alias to the actual temporary-table schema? Or, if that's not accessible by the user, what's the easiest way to resolve it as a user?

(NB My guess would be that PostgreSQL does not resolve it via pg_my_temp_schema(), because pg_temp already existed in PostgreSQL 8.0, but pg_my_temp_schema() was added in PostgreSQL 8.2.)

dpprdan
  • 111
  • 3

1 Answers1

2

pg_my_temp_schema() is documented, so you can use it without any worries. It just exposes an internal PostgreSQL variable to the user. This is the documentation for that variable:

/*
 * myTempNamespace is InvalidOid until and unless a TEMP namespace is set up
 * in a particular backend session (this happens when a CREATE TEMP TABLE
 * command is first executed).  Thereafter it's the OID of the temp namespace.
 *
 * [...]
 */

PostgreSQL uses myTempNamespace to resolve pg_temp. If no temporary schema exists yet, it is ignored when looking up objects. When creating objects in pg_temp, the actual temporary schema is created on the fly if it does not exist yet.

Laurenz Albe
  • 61,070
  • 4
  • 55
  • 90