Skip to content

Commit 52ea6a8

Browse files
committed
Fix dependency handling of partitions and inheritance for ON COMMIT
This commit fixes a set of issues with ON COMMIT actions when used on partitioned tables and tables with inheritance children: - Applying ON COMMIT DROP on a partitioned table with partitions or on a table with inheritance children caused a failure at commit time, with complains about the children being already dropped as all relations are dropped one at the same time. - Applying ON COMMIT DELETE on a partition relying on a partitioned table which uses ON COMMIT DROP would cause the partition truncation to fail as the parent is removed first. The solution to the first problem is to handle the removal of all the dependencies in one go instead of dropping relations one-by-one, based on a suggestion from Álvaro Herrera. So instead all the relation OIDs to remove are gathered and then processed in one round of multiple deletions. The solution to the second problem is to reorder the actions, with truncation happening first and relation drop done after. Even if it means that a partition could be first truncated, then immediately dropped if its partitioned table is dropped, this has the merit to keep the code simple as there is no need to do existence checks on the relations to drop. Contrary to a manual TRUNCATE on a partitioned table, ON COMMIT DELETE does not cascade to its partitions. The ON COMMIT action defined on each partition gets the priority. Author: Michael Paquier Reviewed-by: Amit Langote, Álvaro Herrera, Robert Haas Discussion: https://postgr.es/m/68f17907-ec98-1192-f99f-8011400517f5@lab.ntt.co.jp Backpatch-through: 10
1 parent c09daa9 commit 52ea6a8

File tree

4 files changed

+203
-25
lines changed

4 files changed

+203
-25
lines changed

doc/src/sgml/ref/create_table.sgml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,8 @@ FROM ( { <replaceable class="PARAMETER">numeric_literal</replaceable> | <replace
10551055
All rows in the temporary table will be deleted at the end
10561056
of each transaction block. Essentially, an automatic <xref
10571057
linkend="sql-truncate"> is done
1058-
at each commit.
1058+
at each commit. When used on a partitioned table, this
1059+
is not cascaded to its partitions.
10591060
</para>
10601061
</listitem>
10611062
</varlistentry>
@@ -1065,7 +1066,9 @@ FROM ( { <replaceable class="PARAMETER">numeric_literal</replaceable> | <replace
10651066
<listitem>
10661067
<para>
10671068
The temporary table will be dropped at the end of the current
1068-
transaction block.
1069+
transaction block. When used on a partitioned table, this action
1070+
drops its partitions and when used on tables with inheritance
1071+
children, it drops the dependent children.
10691072
</para>
10701073
</listitem>
10711074
</varlistentry>

src/backend/commands/tablecmds.c

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12827,6 +12827,7 @@ PreCommit_on_commit_actions(void)
1282712827
{
1282812828
ListCell *l;
1282912829
List *oids_to_truncate = NIL;
12830+
List *oids_to_drop = NIL;
1283012831

1283112832
foreach(l, on_commits)
1283212833
{
@@ -12853,36 +12854,66 @@ PreCommit_on_commit_actions(void)
1285312854
oids_to_truncate = lappend_oid(oids_to_truncate, oc->relid);
1285412855
break;
1285512856
case ONCOMMIT_DROP:
12856-
{
12857-
ObjectAddress object;
12858-
12859-
object.classId = RelationRelationId;
12860-
object.objectId = oc->relid;
12861-
object.objectSubId = 0;
12862-
12863-
/*
12864-
* Since this is an automatic drop, rather than one
12865-
* directly initiated by the user, we pass the
12866-
* PERFORM_DELETION_INTERNAL flag.
12867-
*/
12868-
performDeletion(&object,
12869-
DROP_CASCADE, PERFORM_DELETION_INTERNAL);
12870-
12871-
/*
12872-
* Note that table deletion will call
12873-
* remove_on_commit_action, so the entry should get marked
12874-
* as deleted.
12875-
*/
12876-
Assert(oc->deleting_subid != InvalidSubTransactionId);
12877-
break;
12878-
}
12857+
oids_to_drop = lappend_oid(oids_to_drop, oc->relid);
12858+
break;
1287912859
}
1288012860
}
12861+
12862+
/*
12863+
* Truncate relations before dropping so that all dependencies between
12864+
* relations are removed after they are worked on. Doing it like this
12865+
* might be a waste as it is possible that a relation being truncated will
12866+
* be dropped anyway due to its parent being dropped, but this makes the
12867+
* code more robust because of not having to re-check that the relation
12868+
* exists at truncation time.
12869+
*/
1288112870
if (oids_to_truncate != NIL)
1288212871
{
1288312872
heap_truncate(oids_to_truncate);
1288412873
CommandCounterIncrement(); /* XXX needed? */
1288512874
}
12875+
if (oids_to_drop != NIL)
12876+
{
12877+
ObjectAddresses *targetObjects = new_object_addresses();
12878+
ListCell *l;
12879+
12880+
foreach(l, oids_to_drop)
12881+
{
12882+
ObjectAddress object;
12883+
12884+
object.classId = RelationRelationId;
12885+
object.objectId = lfirst_oid(l);
12886+
object.objectSubId = 0;
12887+
12888+
Assert(!object_address_present(&object, targetObjects));
12889+
12890+
add_exact_object_address(&object, targetObjects);
12891+
}
12892+
12893+
/*
12894+
* Since this is an automatic drop, rather than one directly initiated
12895+
* by the user, we pass the PERFORM_DELETION_INTERNAL flag.
12896+
*/
12897+
performMultipleDeletions(targetObjects, DROP_CASCADE,
12898+
PERFORM_DELETION_INTERNAL | PERFORM_DELETION_QUIETLY);
12899+
12900+
#ifdef USE_ASSERT_CHECKING
12901+
12902+
/*
12903+
* Note that table deletion will call remove_on_commit_action, so the
12904+
* entry should get marked as deleted.
12905+
*/
12906+
foreach(l, on_commits)
12907+
{
12908+
OnCommitItem *oc = (OnCommitItem *) lfirst(l);
12909+
12910+
if (oc->oncommit != ONCOMMIT_DROP)
12911+
continue;
12912+
12913+
Assert(oc->deleting_subid != InvalidSubTransactionId);
12914+
}
12915+
#endif
12916+
}
1288612917
}
1288712918

1288812919
/*

src/test/regress/expected/temp.out

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,88 @@ select * from temp_parted_oncommit;
216216
(0 rows)
217217

218218
drop table temp_parted_oncommit;
219+
-- Check dependencies between ON COMMIT actions with a partitioned
220+
-- table and its partitions. Using ON COMMIT DROP on a parent removes
221+
-- the whole set.
222+
begin;
223+
create temp table temp_parted_oncommit_test (a int)
224+
partition by list (a) on commit drop;
225+
create temp table temp_parted_oncommit_test1
226+
partition of temp_parted_oncommit_test
227+
for values in (1) on commit delete rows;
228+
create temp table temp_parted_oncommit_test2
229+
partition of temp_parted_oncommit_test
230+
for values in (2) on commit drop;
231+
insert into temp_parted_oncommit_test values (1), (2);
232+
commit;
233+
-- no relations remain in this case.
234+
select relname from pg_class where relname like 'temp_parted_oncommit_test%';
235+
relname
236+
---------
237+
(0 rows)
238+
239+
-- Using ON COMMIT DELETE on a partitioned table does not remove
240+
-- all rows if partitions preserve their data.
241+
begin;
242+
create temp table temp_parted_oncommit_test (a int)
243+
partition by list (a) on commit delete rows;
244+
create temp table temp_parted_oncommit_test1
245+
partition of temp_parted_oncommit_test
246+
for values in (1) on commit preserve rows;
247+
create temp table temp_parted_oncommit_test2
248+
partition of temp_parted_oncommit_test
249+
for values in (2) on commit drop;
250+
insert into temp_parted_oncommit_test values (1), (2);
251+
commit;
252+
-- Data from the remaining partition is still here as its rows are
253+
-- preserved.
254+
select * from temp_parted_oncommit_test;
255+
a
256+
---
257+
1
258+
(1 row)
259+
260+
-- two relations remain in this case.
261+
select relname from pg_class where relname like 'temp_parted_oncommit_test%';
262+
relname
263+
----------------------------
264+
temp_parted_oncommit_test
265+
temp_parted_oncommit_test1
266+
(2 rows)
267+
268+
drop table temp_parted_oncommit_test;
269+
-- Check dependencies between ON COMMIT actions with inheritance trees.
270+
-- Using ON COMMIT DROP on a parent removes the whole set.
271+
begin;
272+
create temp table temp_inh_oncommit_test (a int) on commit drop;
273+
create temp table temp_inh_oncommit_test1 ()
274+
inherits(temp_inh_oncommit_test) on commit delete rows;
275+
insert into temp_inh_oncommit_test1 values (1);
276+
commit;
277+
-- no relations remain in this case
278+
select relname from pg_class where relname like 'temp_inh_oncommit_test%';
279+
relname
280+
---------
281+
(0 rows)
282+
283+
-- Data on the parent is removed, and the child goes away.
284+
begin;
285+
create temp table temp_inh_oncommit_test (a int) on commit delete rows;
286+
create temp table temp_inh_oncommit_test1 ()
287+
inherits(temp_inh_oncommit_test) on commit drop;
288+
insert into temp_inh_oncommit_test1 values (1);
289+
insert into temp_inh_oncommit_test values (1);
290+
commit;
291+
select * from temp_inh_oncommit_test;
292+
a
293+
---
294+
(0 rows)
295+
296+
-- one relation remains
297+
select relname from pg_class where relname like 'temp_inh_oncommit_test%';
298+
relname
299+
------------------------
300+
temp_inh_oncommit_test
301+
(1 row)
302+
303+
drop table temp_inh_oncommit_test;

src/test/regress/sql/temp.sql

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,62 @@ commit;
165165
-- partitions are emptied by the previous commit
166166
select * from temp_parted_oncommit;
167167
drop table temp_parted_oncommit;
168+
169+
-- Check dependencies between ON COMMIT actions with a partitioned
170+
-- table and its partitions. Using ON COMMIT DROP on a parent removes
171+
-- the whole set.
172+
begin;
173+
create temp table temp_parted_oncommit_test (a int)
174+
partition by list (a) on commit drop;
175+
create temp table temp_parted_oncommit_test1
176+
partition of temp_parted_oncommit_test
177+
for values in (1) on commit delete rows;
178+
create temp table temp_parted_oncommit_test2
179+
partition of temp_parted_oncommit_test
180+
for values in (2) on commit drop;
181+
insert into temp_parted_oncommit_test values (1), (2);
182+
commit;
183+
-- no relations remain in this case.
184+
select relname from pg_class where relname like 'temp_parted_oncommit_test%';
185+
-- Using ON COMMIT DELETE on a partitioned table does not remove
186+
-- all rows if partitions preserve their data.
187+
begin;
188+
create temp table temp_parted_oncommit_test (a int)
189+
partition by list (a) on commit delete rows;
190+
create temp table temp_parted_oncommit_test1
191+
partition of temp_parted_oncommit_test
192+
for values in (1) on commit preserve rows;
193+
create temp table temp_parted_oncommit_test2
194+
partition of temp_parted_oncommit_test
195+
for values in (2) on commit drop;
196+
insert into temp_parted_oncommit_test values (1), (2);
197+
commit;
198+
-- Data from the remaining partition is still here as its rows are
199+
-- preserved.
200+
select * from temp_parted_oncommit_test;
201+
-- two relations remain in this case.
202+
select relname from pg_class where relname like 'temp_parted_oncommit_test%';
203+
drop table temp_parted_oncommit_test;
204+
205+
-- Check dependencies between ON COMMIT actions with inheritance trees.
206+
-- Using ON COMMIT DROP on a parent removes the whole set.
207+
begin;
208+
create temp table temp_inh_oncommit_test (a int) on commit drop;
209+
create temp table temp_inh_oncommit_test1 ()
210+
inherits(temp_inh_oncommit_test) on commit delete rows;
211+
insert into temp_inh_oncommit_test1 values (1);
212+
commit;
213+
-- no relations remain in this case
214+
select relname from pg_class where relname like 'temp_inh_oncommit_test%';
215+
-- Data on the parent is removed, and the child goes away.
216+
begin;
217+
create temp table temp_inh_oncommit_test (a int) on commit delete rows;
218+
create temp table temp_inh_oncommit_test1 ()
219+
inherits(temp_inh_oncommit_test) on commit drop;
220+
insert into temp_inh_oncommit_test1 values (1);
221+
insert into temp_inh_oncommit_test values (1);
222+
commit;
223+
select * from temp_inh_oncommit_test;
224+
-- one relation remains
225+
select relname from pg_class where relname like 'temp_inh_oncommit_test%';
226+
drop table temp_inh_oncommit_test;

0 commit comments

Comments
 (0)