Skip to content

Commit a295684

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 f1634c9 commit a295684

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
@@ -14331,13 +14331,21 @@ PreCommit_on_commit_actions(void)
1433114331
add_exact_object_address(&object, targetObjects);
1433214332
}
1433314333

14334+
/*
14335+
* Object deletion might involve toast table access (to clean up
14336+
* toasted catalog entries), so ensure we have a valid snapshot.
14337+
*/
14338+
PushActiveSnapshot(GetTransactionSnapshot());
14339+
1433414340
/*
1433514341
* Since this is an automatic drop, rather than one directly initiated
1433614342
* by the user, we pass the PERFORM_DELETION_INTERNAL flag.
1433714343
*/
1433814344
performMultipleDeletions(targetObjects, DROP_CASCADE,
1433914345
PERFORM_DELETION_INTERNAL | PERFORM_DELETION_QUIETLY);
1434014346

14347+
PopActiveSnapshot();
14348+
1434114349
#ifdef USE_ASSERT_CHECKING
1434214350

1434314351
/*

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)