BUG with accessing to temporary tables of other sessions still exists

From: Daniil Davydov <3danissimo(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>, Jim Jones <jim(dot)jones(at)uni-muenster(dot)de>
Subject: BUG with accessing to temporary tables of other sessions still exists
Date: 2026-06-03 13:23:20
Message-ID: CAJDiXgiX2XZBHDNo+zBbvku+tchrUurvPRaN1_40mEQ1_sG90g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Recently, we have added two commits ([1], [2]) that are fixing a bug with
accessing temporary tables of other sessions. I found out that this bug didn't
go away completely :

== session 1 ==

postgres=# CREATE TABLE empty_table (id INT);
CREATE TABLE

== session 2 ==

postgres=# INSERT INTO pg_temp_0.empty_table VALUES (1);
INSERT 0 1

As you can see, the INSERT command completes successfully. This happens because
empty_table has no pages, so the insert path looks like this :
heap_insert -> RelationGetBufferForTuple -> RelationAddBlocks -> ....

The RelationAddBlocks extends relations's smgr and allocates a new temp buffer
without calling ReadBufferExtended. Thus, we are 1) bypassing the
"RELATION_IS_OTHER_TEMP" check and 2) creating a buffer in our own temp buffers
pool. The (2) will lead to an error [3] if we attempt to flush such a buffer.

I suggest fixing it by checking whether the relation is other-temp-rel inside
the ExtendBufferedRelLocal function. As far as I can see, all
temp-relation-extend paths include this function.

Please, see the attached patch that fixes a problem and adds a new test.

[1] 40927d458fe1a8c96bcf418b47169f0f6eb15946
[2] ce146621f7860d2e19c509f1466feca3bf777678
[3] ERROR: could not open file "base/5/t3_16386": No such file or directory

P.S.
Jim, I attach you in CC of this thread since you are the co-author of the
original fix. I hope you don't mind :)

--
Best regards,
Daniil Davydov

Attachment Content-Type Size
0001-Prevent-access-to-other-sessions-empty-temp-tables.patch text/x-patch 4.4 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Daniel Gustafsson 2026-06-03 13:25:08 Re: Use streaming read I/O when enabling data checksums online
Previous Message Zsolt Parragi 2026-06-03 13:12:54 Re: CREATE TABLE LIKE INCLUDING PRIVILEGES