diff --git a/src/backend/catalog/heap.c b/src/backend/catalog/heap.c index 064cf9dbf6..2c2d287f0e 100644 --- a/src/backend/catalog/heap.c +++ b/src/backend/catalog/heap.c @@ -3121,7 +3121,8 @@ RelationTruncateIndexes(Relation heapRelation) /* * heap_truncate * - * This routine deletes all data within all the specified relations. + * This routine deletes all data within all the specified relations, + * ignoring any that don't have any storage to begin with * * This is not transaction-safe! There is another, transaction-safe * implementation in commands/tablecmds.c. We now use this only for @@ -3151,8 +3152,12 @@ heap_truncate(List *relids) { Relation rel = lfirst(cell); - /* Truncate the relation */ - heap_truncate_one_rel(rel); + /* + * Truncate the relation. Regular and partitioned tables can be + * temporary relations, but only regular tables have storage. + */ + if (rel->rd_rel->relkind == RELKIND_RELATION) + heap_truncate_one_rel(rel); /* Close the relation, but keep exclusive lock on it until commit */ heap_close(rel, NoLock); diff --git a/src/test/regress/expected/temp.out b/src/test/regress/expected/temp.out index addf1ec444..ed2c591fc6 100644 --- a/src/test/regress/expected/temp.out +++ b/src/test/regress/expected/temp.out @@ -199,3 +199,17 @@ select pg_temp.whoami(); (1 row) drop table public.whereami; +-- for partitioned temp tables, ON COMMIT action should ignore storage-less +-- partitioned parent table +begin; +create temp table temp_parted_oncommit_test (a int) partition by list (a) on commit delete rows; +create temp table temp_parted_oncommit_test1 partition of temp_parted_oncommit_test for values in (1) on commit delete rows; +insert into temp_parted_oncommit_test values (1); +commit; +-- temp_parted_oncommit_test1 must've been emptied during the commit +select * from temp_parted_oncommit_test; + a +--- +(0 rows) + +drop table temp_parted_oncommit_test; diff --git a/src/test/regress/sql/temp.sql b/src/test/regress/sql/temp.sql index 5183c727f5..0274bdb13f 100644 --- a/src/test/regress/sql/temp.sql +++ b/src/test/regress/sql/temp.sql @@ -151,3 +151,14 @@ select whoami(); select pg_temp.whoami(); drop table public.whereami; + +-- for partitioned temp tables, ON COMMIT action should ignore storage-less +-- partitioned parent table +begin; +create temp table temp_parted_oncommit_test (a int) partition by list (a) on commit delete rows; +create temp table temp_parted_oncommit_test1 partition of temp_parted_oncommit_test for values in (1) on commit delete rows; +insert into temp_parted_oncommit_test values (1); +commit; +-- temp_parted_oncommit_test1 must've been emptied during the commit +select * from temp_parted_oncommit_test; +drop table temp_parted_oncommit_test;