summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Lane2023-10-16 18:06:11 +0000
committerTom Lane2023-10-16 18:06:14 +0000
commit54b208f90963cb8b48b9794a5392b2fae4b40a98 (patch)
tree6abe3df7ab73c883c151a65eb7545f4de4c3f4ca
parent8fb13dd6ab5bffdbfafd8894ffcc5deb44d0c0b0 (diff)
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 277692220 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
-rw-r--r--src/backend/commands/tablecmds.c8
-rw-r--r--src/test/regress/expected/temp.out20
-rw-r--r--src/test/regress/sql/temp.sql16
3 files changed, 44 insertions, 0 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 8820738d91..fb48772fd5 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -17645,12 +17645,20 @@ PreCommit_on_commit_actions(void)
}
/*
+ * Object deletion might involve toast table access (to clean up
+ * toasted catalog entries), so ensure we have a valid snapshot.
+ */
+ PushActiveSnapshot(GetTransactionSnapshot());
+
+ /*
* Since this is an automatic drop, rather than one directly initiated
* by the user, we pass the PERFORM_DELETION_INTERNAL flag.
*/
performMultipleDeletions(targetObjects, DROP_CASCADE,
PERFORM_DELETION_INTERNAL | PERFORM_DELETION_QUIETLY);
+ PopActiveSnapshot();
+
#ifdef USE_ASSERT_CHECKING
/*
diff --git a/src/test/regress/expected/temp.out b/src/test/regress/expected/temp.out
index a5b3ed34a3..2a246a7e12 100644
--- a/src/test/regress/expected/temp.out
+++ b/src/test/regress/expected/temp.out
@@ -113,6 +113,26 @@ SELECT * FROM temptest;
ERROR: relation "temptest" does not exist
LINE 1: SELECT * FROM temptest;
^
+-- Test it with a CHECK condition that produces a toasted pg_constraint entry
+BEGIN;
+do $$
+begin
+ execute format($cmd$
+ CREATE TEMP TABLE temptest (col text CHECK (col < %L)) ON COMMIT DROP
+ $cmd$,
+ (SELECT string_agg(g.i::text || ':' || random()::text, '|')
+ FROM generate_series(1, 100) g(i)));
+end$$;
+SELECT * FROM temptest;
+ col
+-----
+(0 rows)
+
+COMMIT;
+SELECT * FROM temptest;
+ERROR: relation "temptest" does not exist
+LINE 1: SELECT * FROM temptest;
+ ^
-- ON COMMIT is only allowed for TEMP
CREATE TABLE temptest(col int) ON COMMIT DELETE ROWS;
ERROR: ON COMMIT can only be used on temporary tables
diff --git a/src/test/regress/sql/temp.sql b/src/test/regress/sql/temp.sql
index 424d12b283..2a487a1ef7 100644
--- a/src/test/regress/sql/temp.sql
+++ b/src/test/regress/sql/temp.sql
@@ -101,6 +101,22 @@ COMMIT;
SELECT * FROM temptest;
+-- Test it with a CHECK condition that produces a toasted pg_constraint entry
+BEGIN;
+do $$
+begin
+ execute format($cmd$
+ CREATE TEMP TABLE temptest (col text CHECK (col < %L)) ON COMMIT DROP
+ $cmd$,
+ (SELECT string_agg(g.i::text || ':' || random()::text, '|')
+ FROM generate_series(1, 100) g(i)));
+end$$;
+
+SELECT * FROM temptest;
+COMMIT;
+
+SELECT * FROM temptest;
+
-- ON COMMIT is only allowed for TEMP
CREATE TABLE temptest(col int) ON COMMIT DELETE ROWS;