Re: Fix bug with accessing to temporary tables of other sessions

Jim Jones <jim.jones@uni-muenster.de>

From: Jim Jones <jim.jones@uni-muenster.de>
To: "David G. Johnston" <david.g.johnston@gmail.com>
Cc: Michael Paquier <michael@paquier.xyz>, Alexander Korotkov <aekorotkov@gmail.com>, Soumya S Murali <soumyamurali.work@gmail.com>, Daniil Davydov <3danissimo@gmail.com>, Tom Lane <tgl@sss.pgh.pa.us>, Stepan Neretin <slpmcf@gmail.com>, PostgreSQL Hackers <pgsql-hackers@postgresql.org>, Mohamed Ali <moali.pg@gmail.com>, Nazneen Jafri <jafrinazneen@gmail.com>, Shawn McCoy <shawn.the.mccoy@gmail.com>
Date: 2026-04-25T13:53:41Z
Lists: pgsql-hackers

On 25/04/2026 01:01, Jim Jones wrote:
> Until PG16 an error message was raised:
> 
> psql (16.13 (Debian 16.13-1.pgdg13+1))
> Type "help" for help.
> 
> postgres=# \d pg_temp*.*
>                     Table "pg_temp_3.t"
>      Column      |  Type   | Collation | Nullable | Default
> -----------------+---------+-----------+----------+---------
>  generate_series | integer |           |          |
> 
> postgres=# SELECT * FROM pg_temp_3.t;
> ERROR:  cannot access temporary tables of other sessions


The PG16 test above was against a non-empty TEMPORARY TABLE. If the
table is empty, the same behaviour from PG17 and PG18 can be observed:

psql (16.13 (Debian 16.13-1.pgdg13+1))
Type "help" for help.

postgres=# \d pg_temp*.*
                Table "pg_temp_4.t"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           |          |

postgres=# SELECT * FROM pg_temp_4.t;
 id
----
(0 rows)

The same applies for PG14 and PG15

psql (14.22 (Debian 14.22-1.pgdg13+1))
Type "help" for help.

postgres=# \d pg_temp*.*
                Table "pg_temp_3.t"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           |          |

postgres=# SELECT * FROM pg_temp_3.t;
 id
----
(0 rows)

psql (15.17 (Debian 15.17-1.pgdg13+1))
Type "help" for help.

postgres=# \d pg_temp*.*
                Table "pg_temp_3.t"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           |          |

postgres=# SELECT * FROM pg_temp_3.t;
 id
----
(0 rows)


Since the table is indeed empty, the result is actually correct. But I'd
argue that we should raise an ERROR even if the table is empty. IMHO,
getting an error message or a "0 rows" result depending on the table row
count isn't ideal.

After populating the TEMPORARY TABLE the expected error message appears:

ERROR:  cannot access temporary tables of other sessions

Best, Jim




Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Prevent access to other sessions' temp tables

  2. Add tests for cross-session temp table access

  3. Doc: use "an SQL" consistently rather than "a SQL"

  4. Modify the relcache to record the temp status of both local and nonlocal