Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 54b208f

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 8fb13dd commit 54b208f

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
@@ -17644,13 +17644,21 @@ PreCommit_on_commit_actions(void)
1764417644
add_exact_object_address(&object, targetObjects);
1764517645
}
1764617646

17647+
/*
17648+
* Object deletion might involve toast table access (to clean up
17649+
* toasted catalog entries), so ensure we have a valid snapshot.
17650+
*/
17651+
PushActiveSnapshot(GetTransactionSnapshot());
17652+
1764717653
/*
1764817654
* Since this is an automatic drop, rather than one directly initiated
1764917655
* by the user, we pass the PERFORM_DELETION_INTERNAL flag.
1765017656
*/
1765117657
performMultipleDeletions(targetObjects, DROP_CASCADE,
1765217658
PERFORM_DELETION_INTERNAL | PERFORM_DELETION_QUIETLY);
1765317659

17660+
PopActiveSnapshot();
17661+
1765417662
#ifdef USE_ASSERT_CHECKING
1765517663

1765617664
/*

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)