Skip to content

Commit 8b4d582

Browse files
Allow partitioned tables to be dropped without CASCADE
Record partitioned table dependencies as DEPENDENCY_AUTO rather than DEPENDENCY_NORMAL, so that DROP TABLE just works. Remove all the tests for partitioned tables where earlier work had deliberately avoided using CASCADE. Amit Langote, reviewed by Ashutosh Bapat and myself
1 parent dbca84f commit 8b4d582

File tree

11 files changed

+44
-73
lines changed

11 files changed

+44
-73
lines changed

src/backend/commands/tablecmds.c

+22-8
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,11 @@ static List *MergeAttributes(List *schema, List *supers, char relpersistence,
289289
static bool MergeCheckConstraint(List *constraints, char *name, Node *expr);
290290
static void MergeAttributesIntoExisting(Relation child_rel, Relation parent_rel);
291291
static void MergeConstraintsIntoExisting(Relation child_rel, Relation parent_rel);
292-
static void StoreCatalogInheritance(Oid relationId, List *supers);
292+
static void StoreCatalogInheritance(Oid relationId, List *supers,
293+
bool child_is_partition);
293294
static void StoreCatalogInheritance1(Oid relationId, Oid parentOid,
294-
int16 seqNumber, Relation inhRelation);
295+
int16 seqNumber, Relation inhRelation,
296+
bool child_is_partition);
295297
static int findAttrByName(const char *attributeName, List *schema);
296298
static void AlterIndexNamespaces(Relation classRel, Relation rel,
297299
Oid oldNspOid, Oid newNspOid, ObjectAddresses *objsMoved);
@@ -725,7 +727,7 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
725727
typaddress);
726728

727729
/* Store inheritance information for new rel. */
728-
StoreCatalogInheritance(relationId, inheritOids);
730+
StoreCatalogInheritance(relationId, inheritOids, stmt->partbound != NULL);
729731

730732
/*
731733
* We must bump the command counter to make the newly-created relation
@@ -2248,7 +2250,8 @@ MergeCheckConstraint(List *constraints, char *name, Node *expr)
22482250
* supers is a list of the OIDs of the new relation's direct ancestors.
22492251
*/
22502252
static void
2251-
StoreCatalogInheritance(Oid relationId, List *supers)
2253+
StoreCatalogInheritance(Oid relationId, List *supers,
2254+
bool child_is_partition)
22522255
{
22532256
Relation relation;
22542257
int16 seqNumber;
@@ -2278,7 +2281,8 @@ StoreCatalogInheritance(Oid relationId, List *supers)
22782281
{
22792282
Oid parentOid = lfirst_oid(entry);
22802283

2281-
StoreCatalogInheritance1(relationId, parentOid, seqNumber, relation);
2284+
StoreCatalogInheritance1(relationId, parentOid, seqNumber, relation,
2285+
child_is_partition);
22822286
seqNumber++;
22832287
}
22842288

@@ -2291,7 +2295,8 @@ StoreCatalogInheritance(Oid relationId, List *supers)
22912295
*/
22922296
static void
22932297
StoreCatalogInheritance1(Oid relationId, Oid parentOid,
2294-
int16 seqNumber, Relation inhRelation)
2298+
int16 seqNumber, Relation inhRelation,
2299+
bool child_is_partition)
22952300
{
22962301
TupleDesc desc = RelationGetDescr(inhRelation);
22972302
Datum values[Natts_pg_inherits];
@@ -2325,7 +2330,14 @@ StoreCatalogInheritance1(Oid relationId, Oid parentOid,
23252330
childobject.objectId = relationId;
23262331
childobject.objectSubId = 0;
23272332

2328-
recordDependencyOn(&childobject, &parentobject, DEPENDENCY_NORMAL);
2333+
/*
2334+
* Partition tables are expected to be dropped when the parent partitioned
2335+
* table gets dropped.
2336+
*/
2337+
if (child_is_partition)
2338+
recordDependencyOn(&childobject, &parentobject, DEPENDENCY_AUTO);
2339+
else
2340+
recordDependencyOn(&childobject, &parentobject, DEPENDENCY_NORMAL);
23292341

23302342
/*
23312343
* Post creation hook of this inheritance. Since object_access_hook
@@ -10753,7 +10765,9 @@ CreateInheritance(Relation child_rel, Relation parent_rel)
1075310765
StoreCatalogInheritance1(RelationGetRelid(child_rel),
1075410766
RelationGetRelid(parent_rel),
1075510767
inhseqno + 1,
10756-
catalogRelation);
10768+
catalogRelation,
10769+
parent_rel->rd_rel->relkind ==
10770+
RELKIND_PARTITIONED_TABLE);
1075710771

1075810772
/* Now we're done with pg_inherits */
1075910773
heap_close(catalogRelation, RowExclusiveLock);

src/test/regress/expected/alter_table.out

+4-6
Original file line numberDiff line numberDiff line change
@@ -3339,10 +3339,8 @@ ALTER TABLE list_parted2 DROP COLUMN b;
33393339
ERROR: cannot drop column named in partition key
33403340
ALTER TABLE list_parted2 ALTER COLUMN b TYPE text;
33413341
ERROR: cannot alter type of column named in partition key
3342-
-- cleanup: avoid using CASCADE
3343-
DROP TABLE list_parted, part_1;
3344-
DROP TABLE list_parted2, part_2, part_5, part_5_a;
3345-
DROP TABLE range_parted, part1, part2;
3342+
-- cleanup
3343+
DROP TABLE list_parted, list_parted2, range_parted;
33463344
-- more tests for certain multi-level partitioning scenarios
33473345
create table p (a int, b int) partition by range (a, b);
33483346
create table p1 (b int, a int not null) partition by range (b);
@@ -3371,5 +3369,5 @@ insert into p1 (a, b) values (2, 3);
33713369
-- check that partition validation scan correctly detects violating rows
33723370
alter table p attach partition p1 for values from (1, 2) to (1, 10);
33733371
ERROR: partition constraint is violated by some row
3374-
-- cleanup: avoid using CASCADE
3375-
drop table p, p1, p11;
3372+
-- cleanup
3373+
drop table p;

src/test/regress/expected/create_table.out

+2-7
Original file line numberDiff line numberDiff line change
@@ -667,10 +667,5 @@ Check constraints:
667667
"check_a" CHECK (length(a) > 0)
668668
Number of partitions: 3 (Use \d+ to list them.)
669669

670-
-- cleanup: avoid using CASCADE
671-
DROP TABLE parted, part_a, part_b, part_c, part_c_1_10;
672-
DROP TABLE list_parted, part_1, part_2, part_null;
673-
DROP TABLE range_parted;
674-
DROP TABLE list_parted2, part_ab, part_null_z;
675-
DROP TABLE range_parted2, part0, part1, part2, part3;
676-
DROP TABLE range_parted3, part00, part10, part11, part12;
670+
-- cleanup
671+
DROP TABLE parted, list_parted, range_parted, list_parted2, range_parted2, range_parted3;

src/test/regress/expected/inherit.out

+2-20
Original file line numberDiff line numberDiff line change
@@ -1843,23 +1843,5 @@ explain (costs off) select * from range_list_parted where a >= 30;
18431843
Filter: (a >= 30)
18441844
(11 rows)
18451845

1846-
drop table list_parted cascade;
1847-
NOTICE: drop cascades to 3 other objects
1848-
DETAIL: drop cascades to table part_ab_cd
1849-
drop cascades to table part_ef_gh
1850-
drop cascades to table part_null_xy
1851-
drop table range_list_parted cascade;
1852-
NOTICE: drop cascades to 13 other objects
1853-
DETAIL: drop cascades to table part_1_10
1854-
drop cascades to table part_1_10_ab
1855-
drop cascades to table part_1_10_cd
1856-
drop cascades to table part_10_20
1857-
drop cascades to table part_10_20_ab
1858-
drop cascades to table part_10_20_cd
1859-
drop cascades to table part_21_30
1860-
drop cascades to table part_21_30_ab
1861-
drop cascades to table part_21_30_cd
1862-
drop cascades to table part_40_inf
1863-
drop cascades to table part_40_inf_ab
1864-
drop cascades to table part_40_inf_cd
1865-
drop cascades to table part_40_inf_null
1846+
drop table list_parted;
1847+
drop table range_list_parted;

src/test/regress/expected/insert.out

+2-5
Original file line numberDiff line numberDiff line change
@@ -314,10 +314,7 @@ select tableoid::regclass::text, a, min(b) as min_b, max(b) as max_b from list_p
314314
(9 rows)
315315

316316
-- cleanup
317-
drop table part1, part2, part3, part4, range_parted;
318-
drop table part_ee_ff3_1, part_ee_ff3_2, part_ee_ff1, part_ee_ff2, part_ee_ff3;
319-
drop table part_ee_ff, part_gg2_2, part_gg2_1, part_gg2, part_gg1, part_gg;
320-
drop table part_aa_bb, part_cc_dd, part_null, list_parted;
317+
drop table range_parted, list_parted;
321318
-- more tests for certain multi-level partitioning scenarios
322319
create table p (a int, b int) partition by range (a, b);
323320
create table p1 (b int not null, a int not null) partition by range ((b+0));
@@ -417,4 +414,4 @@ revoke all on key_desc_1 from someone_else;
417414
drop role someone_else;
418415
drop table key_desc, key_desc_1;
419416
-- cleanup
420-
drop table p, p1, p11, p12, p2, p3, p4;
417+
drop table p;

src/test/regress/expected/update.out

+1-6
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,4 @@ DETAIL: Failing row contains (b, 9).
219219
-- ok
220220
update range_parted set b = b + 1 where b = 10;
221221
-- cleanup
222-
drop table range_parted cascade;
223-
NOTICE: drop cascades to 4 other objects
224-
DETAIL: drop cascades to table part_a_1_a_10
225-
drop cascades to table part_a_10_a_20
226-
drop cascades to table part_b_1_b_10
227-
drop cascades to table part_b_10_b_20
222+
drop table range_parted;

src/test/regress/sql/alter_table.sql

+4-6
Original file line numberDiff line numberDiff line change
@@ -2199,10 +2199,8 @@ ALTER TABLE part_2 INHERIT inh_test;
21992199
ALTER TABLE list_parted2 DROP COLUMN b;
22002200
ALTER TABLE list_parted2 ALTER COLUMN b TYPE text;
22012201

2202-
-- cleanup: avoid using CASCADE
2203-
DROP TABLE list_parted, part_1;
2204-
DROP TABLE list_parted2, part_2, part_5, part_5_a;
2205-
DROP TABLE range_parted, part1, part2;
2202+
-- cleanup
2203+
DROP TABLE list_parted, list_parted2, range_parted;
22062204

22072205
-- more tests for certain multi-level partitioning scenarios
22082206
create table p (a int, b int) partition by range (a, b);
@@ -2227,5 +2225,5 @@ insert into p1 (a, b) values (2, 3);
22272225
-- check that partition validation scan correctly detects violating rows
22282226
alter table p attach partition p1 for values from (1, 2) to (1, 10);
22292227

2230-
-- cleanup: avoid using CASCADE
2231-
drop table p, p1, p11;
2228+
-- cleanup
2229+
drop table p;

src/test/regress/sql/create_table.sql

+2-7
Original file line numberDiff line numberDiff line change
@@ -595,10 +595,5 @@ CREATE TABLE part_c_1_10 PARTITION OF part_c FOR VALUES FROM (1) TO (10);
595595
-- returned.
596596
\d parted
597597

598-
-- cleanup: avoid using CASCADE
599-
DROP TABLE parted, part_a, part_b, part_c, part_c_1_10;
600-
DROP TABLE list_parted, part_1, part_2, part_null;
601-
DROP TABLE range_parted;
602-
DROP TABLE list_parted2, part_ab, part_null_z;
603-
DROP TABLE range_parted2, part0, part1, part2, part3;
604-
DROP TABLE range_parted3, part00, part10, part11, part12;
598+
-- cleanup
599+
DROP TABLE parted, list_parted, range_parted, list_parted2, range_parted2, range_parted3;

src/test/regress/sql/inherit.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -612,5 +612,5 @@ explain (costs off) select * from range_list_parted where b is null;
612612
explain (costs off) select * from range_list_parted where a is not null and a < 67;
613613
explain (costs off) select * from range_list_parted where a >= 30;
614614

615-
drop table list_parted cascade;
616-
drop table range_list_parted cascade;
615+
drop table list_parted;
616+
drop table range_list_parted;

src/test/regress/sql/insert.sql

+2-5
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,7 @@ insert into list_parted (b) values (1);
186186
select tableoid::regclass::text, a, min(b) as min_b, max(b) as max_b from list_parted group by 1, 2 order by 1;
187187

188188
-- cleanup
189-
drop table part1, part2, part3, part4, range_parted;
190-
drop table part_ee_ff3_1, part_ee_ff3_2, part_ee_ff1, part_ee_ff2, part_ee_ff3;
191-
drop table part_ee_ff, part_gg2_2, part_gg2_1, part_gg2, part_gg1, part_gg;
192-
drop table part_aa_bb, part_cc_dd, part_null, list_parted;
189+
drop table range_parted, list_parted;
193190

194191
-- more tests for certain multi-level partitioning scenarios
195192
create table p (a int, b int) partition by range (a, b);
@@ -271,4 +268,4 @@ drop role someone_else;
271268
drop table key_desc, key_desc_1;
272269

273270
-- cleanup
274-
drop table p, p1, p11, p12, p2, p3, p4;
271+
drop table p;

src/test/regress/sql/update.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,4 @@ update range_parted set b = b - 1 where b = 10;
126126
update range_parted set b = b + 1 where b = 10;
127127

128128
-- cleanup
129-
drop table range_parted cascade;
129+
drop table range_parted;

0 commit comments

Comments
 (0)