Skip to content

Commit 07eb22a

Browse files
committed
Ensure we have a snapshot while dropping ON COMMIT DROP temp tables.
Dropping a temp table could entail TOAST table access to clean out toasted catalog entries, such as large pg_constraint.conbin strings for complex CHECK constraints. If we did that via ON COMMIT DROP, we triggered the assertion in init_toast_snapshot(), because there was no provision for setting up a snapshot for the drop actions. Fix that. (I assume here that the adjacent truncation actions for ON COMMIT DELETE ROWS don't have a similar problem: it doesn't seem like nontransactional truncations would need to touch any toasted fields. If that proves wrong, we could refactor a bit to have the same snapshot acquisition cover that too.) The test case added here does not fail before v15, because that assertion was added in 2776922 which was not back-patched. However, the race condition the assertion warns of surely exists further back, so back-patch to all supported branches. Per report from Richard Guo. Discussion: https://postgr.es/m/CAMbWs4-x26=_QxxgdJyNbiCDzvtr2WV5ZDso_v-CukKEe6cBZw@mail.gmail.com
1 parent 43c9790 commit 07eb22a

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/backend/commands/tablecmds.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15373,13 +15373,21 @@ PreCommit_on_commit_actions(void)
1537315373
add_exact_object_address(&object, targetObjects);
1537415374
}
1537515375

15376+
/*
15377+
* Object deletion might involve toast table access (to clean up
15378+
* toasted catalog entries), so ensure we have a valid snapshot.
15379+
*/
15380+
PushActiveSnapshot(GetTransactionSnapshot());
15381+
1537615382
/*
1537715383
* Since this is an automatic drop, rather than one directly initiated
1537815384
* by the user, we pass the PERFORM_DELETION_INTERNAL flag.
1537915385
*/
1538015386
performMultipleDeletions(targetObjects, DROP_CASCADE,
1538115387
PERFORM_DELETION_INTERNAL | PERFORM_DELETION_QUIETLY);
1538215388

15389+
PopActiveSnapshot();
15390+
1538315391
#ifdef USE_ASSERT_CHECKING
1538415392

1538515393
/*

src/test/regress/expected/temp.out

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,26 @@ SELECT * FROM temptest;
108108
1
109109
(1 row)
110110

111+
COMMIT;
112+
SELECT * FROM temptest;
113+
ERROR: relation "temptest" does not exist
114+
LINE 1: SELECT * FROM temptest;
115+
^
116+
-- Test it with a CHECK condition that produces a toasted pg_constraint entry
117+
BEGIN;
118+
do $$
119+
begin
120+
execute format($cmd$
121+
CREATE TEMP TABLE temptest (col text CHECK (col < %L)) ON COMMIT DROP
122+
$cmd$,
123+
(SELECT string_agg(g.i::text || ':' || random()::text, '|')
124+
FROM generate_series(1, 100) g(i)));
125+
end$$;
126+
SELECT * FROM temptest;
127+
col
128+
-----
129+
(0 rows)
130+
111131
COMMIT;
112132
SELECT * FROM temptest;
113133
ERROR: relation "temptest" does not exist

src/test/regress/sql/temp.sql

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,22 @@ COMMIT;
101101

102102
SELECT * FROM temptest;
103103

104+
-- Test it with a CHECK condition that produces a toasted pg_constraint entry
105+
BEGIN;
106+
do $$
107+
begin
108+
execute format($cmd$
109+
CREATE TEMP TABLE temptest (col text CHECK (col < %L)) ON COMMIT DROP
110+
$cmd$,
111+
(SELECT string_agg(g.i::text || ':' || random()::text, '|')
112+
FROM generate_series(1, 100) g(i)));
113+
end$$;
114+
115+
SELECT * FROM temptest;
116+
COMMIT;
117+
118+
SELECT * FROM temptest;
119+
104120
-- ON COMMIT is only allowed for TEMP
105121

106122
CREATE TABLE temptest(col int) ON COMMIT DELETE ROWS;

0 commit comments

Comments
 (0)