Skip to content

Commit 95a0a8d

Browse files
committed
Fix handling of container types in find_composite_type_dependencies.
find_composite_type_dependencies correctly found columns that are of the specified type, and columns that are of arrays of that type, but not columns that are domains or ranges over the given type, its array type, etc. The most general way to handle this seems to be to assume that any type that is directly dependent on the specified type can be treated as a container type, and processed recursively (allowing us to handle nested cases such as ranges over domains over arrays ...). Since a type's array type already has such a dependency, we can drop the existing special case for the array type. The very similar logic in get_rels_with_domain was likewise a few bricks shy of a load, as it supposed that a directly dependent type could *only* be a sub-domain. This is already wrong for ranges over domains, and it'll someday be wrong for arrays over domains. Add test cases illustrating the problems, and back-patch to all supported branches. Discussion: https://postgr.es/m/15268.1502309024@sss.pgh.pa.us
1 parent e829db0 commit 95a0a8d

File tree

4 files changed

+97
-35
lines changed

4 files changed

+97
-35
lines changed

src/backend/commands/tablecmds.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4249,13 +4249,18 @@ ATTypedTableRecursion(List **wqueue, Relation rel, AlterTableCmd *cmd,
42494249
/*
42504250
* find_composite_type_dependencies
42514251
*
4252-
* Check to see if a composite type is being used as a column in some
4253-
* other table (possibly nested several levels deep in composite types!).
4252+
* Check to see if the type "typeOid" is being used as a column in some table
4253+
* (possibly nested several levels deep in composite types, arrays, etc!).
42544254
* Eventually, we'd like to propagate the check or rewrite operation
4255-
* into other such tables, but for now, just error out if we find any.
4255+
* into such tables, but for now, just error out if we find any.
42564256
*
4257-
* Caller should provide either a table name or a type name (not both) to
4258-
* report in the error message, if any.
4257+
* Caller should provide either the associated relation of a rowtype,
4258+
* or a type name (not both) for use in the error message, if any.
4259+
*
4260+
* Note that "typeOid" is not necessarily a composite type; it could also be
4261+
* another container type such as an array or range, or a domain over one of
4262+
* these things. The name of this function is therefore somewhat historical,
4263+
* but it's not worth changing.
42594264
*
42604265
* We assume that functions and views depending on the type are not reasons
42614266
* to reject the ALTER. (How safe is this really?)
@@ -4268,11 +4273,13 @@ find_composite_type_dependencies(Oid typeOid, Relation origRelation,
42684273
ScanKeyData key[2];
42694274
SysScanDesc depScan;
42704275
HeapTuple depTup;
4271-
Oid arrayOid;
4276+
4277+
/* since this function recurses, it could be driven to stack overflow */
4278+
check_stack_depth();
42724279

42734280
/*
4274-
* We scan pg_depend to find those things that depend on the rowtype. (We
4275-
* assume we can ignore refobjsubid for a rowtype.)
4281+
* We scan pg_depend to find those things that depend on the given type.
4282+
* (We assume we can ignore refobjsubid for a type.)
42764283
*/
42774284
depRel = heap_open(DependRelationId, AccessShareLock);
42784285

@@ -4294,8 +4301,22 @@ find_composite_type_dependencies(Oid typeOid, Relation origRelation,
42944301
Relation rel;
42954302
Form_pg_attribute att;
42964303

4297-
/* Ignore dependees that aren't user columns of relations */
4298-
/* (we assume system columns are never of rowtypes) */
4304+
/* Check for directly dependent types */
4305+
if (pg_depend->classid == TypeRelationId)
4306+
{
4307+
/*
4308+
* This must be an array, domain, or range containing the given
4309+
* type, so recursively check for uses of this type. Note that
4310+
* any error message will mention the original type not the
4311+
* container; this is intentional.
4312+
*/
4313+
find_composite_type_dependencies(pg_depend->objid,
4314+
origRelation, origTypeName);
4315+
continue;
4316+
}
4317+
4318+
/* Else, ignore dependees that aren't user columns of relations */
4319+
/* (we assume system columns are never of interesting types) */
42994320
if (pg_depend->classid != RelationRelationId ||
43004321
pg_depend->objsubid <= 0)
43014322
continue;
@@ -4351,14 +4372,6 @@ find_composite_type_dependencies(Oid typeOid, Relation origRelation,
43514372
systable_endscan(depScan);
43524373

43534374
relation_close(depRel, AccessShareLock);
4354-
4355-
/*
4356-
* If there's an array type for the rowtype, must check for uses of it,
4357-
* too.
4358-
*/
4359-
arrayOid = get_array_type(typeOid);
4360-
if (OidIsValid(arrayOid))
4361-
find_composite_type_dependencies(arrayOid, origRelation, origTypeName);
43624375
}
43634376

43644377

src/backend/commands/typecmds.c

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2724,10 +2724,9 @@ validateDomainConstraint(Oid domainoid, char *ccbin)
27242724
* risk by using the weakest suitable lock (ShareLock for most callers).
27252725
*
27262726
* XXX the API for this is not sufficient to support checking domain values
2727-
* that are inside composite types or arrays. Currently we just error out
2728-
* if a composite type containing the target domain is stored anywhere.
2729-
* There are not currently arrays of domains; if there were, we could take
2730-
* the same approach, but it'd be nicer to fix it properly.
2727+
* that are inside container types, such as composite types, arrays, or
2728+
* ranges. Currently we just error out if a container type containing the
2729+
* target domain is stored anywhere.
27312730
*
27322731
* Generally used for retrieving a list of tests when adding
27332732
* new constraints to a domain.
@@ -2736,13 +2735,17 @@ static List *
27362735
get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
27372736
{
27382737
List *result = NIL;
2738+
char *domainTypeName = format_type_be(domainOid);
27392739
Relation depRel;
27402740
ScanKeyData key[2];
27412741
SysScanDesc depScan;
27422742
HeapTuple depTup;
27432743

27442744
Assert(lockmode != NoLock);
27452745

2746+
/* since this function recurses, it could be driven to stack overflow */
2747+
check_stack_depth();
2748+
27462749
/*
27472750
* We scan pg_depend to find those things that depend on the domain. (We
27482751
* assume we can ignore refobjsubid for a domain.)
@@ -2769,20 +2772,32 @@ get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
27692772
Form_pg_attribute pg_att;
27702773
int ptr;
27712774

2772-
/* Check for directly dependent types --- must be domains */
2775+
/* Check for directly dependent types */
27732776
if (pg_depend->classid == TypeRelationId)
27742777
{
2775-
Assert(get_typtype(pg_depend->objid) == TYPTYPE_DOMAIN);
2776-
2777-
/*
2778-
* Recursively add dependent columns to the output list. This is
2779-
* a bit inefficient since we may fail to combine RelToCheck
2780-
* entries when attributes of the same rel have different derived
2781-
* domain types, but it's probably not worth improving.
2782-
*/
2783-
result = list_concat(result,
2784-
get_rels_with_domain(pg_depend->objid,
2785-
lockmode));
2778+
if (get_typtype(pg_depend->objid) == TYPTYPE_DOMAIN)
2779+
{
2780+
/*
2781+
* This is a sub-domain, so recursively add dependent columns
2782+
* to the output list. This is a bit inefficient since we may
2783+
* fail to combine RelToCheck entries when attributes of the
2784+
* same rel have different derived domain types, but it's
2785+
* probably not worth improving.
2786+
*/
2787+
result = list_concat(result,
2788+
get_rels_with_domain(pg_depend->objid,
2789+
lockmode));
2790+
}
2791+
else
2792+
{
2793+
/*
2794+
* Otherwise, it is some container type using the domain, so
2795+
* fail if there are any columns of this type.
2796+
*/
2797+
find_composite_type_dependencies(pg_depend->objid,
2798+
NULL,
2799+
domainTypeName);
2800+
}
27862801
continue;
27872802
}
27882803

@@ -2819,7 +2834,7 @@ get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
28192834
if (OidIsValid(rel->rd_rel->reltype))
28202835
find_composite_type_dependencies(rel->rd_rel->reltype,
28212836
NULL,
2822-
format_type_be(domainOid));
2837+
domainTypeName);
28232838

28242839
/*
28252840
* Otherwise, we can ignore relations except those with both

src/test/regress/expected/domain.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,11 +661,28 @@ insert into ddtest2 values(row(-1));
661661
alter domain posint add constraint c1 check(value >= 0);
662662
ERROR: cannot alter type "posint" because column "ddtest2.f1" uses it
663663
drop table ddtest2;
664+
-- Likewise for domains within arrays of composite
664665
create table ddtest2(f1 ddtest1[]);
665666
insert into ddtest2 values('{(-1)}');
666667
alter domain posint add constraint c1 check(value >= 0);
667668
ERROR: cannot alter type "posint" because column "ddtest2.f1" uses it
668669
drop table ddtest2;
670+
-- Likewise for domains within domains over array of composite
671+
create domain ddtest1d as ddtest1[];
672+
create table ddtest2(f1 ddtest1d);
673+
insert into ddtest2 values('{(-1)}');
674+
alter domain posint add constraint c1 check(value >= 0);
675+
ERROR: cannot alter type "posint" because column "ddtest2.f1" uses it
676+
drop table ddtest2;
677+
drop domain ddtest1d;
678+
-- Doesn't work for ranges, either
679+
create type rposint as range (subtype = posint);
680+
create table ddtest2(f1 rposint);
681+
insert into ddtest2 values('(-1,3]');
682+
alter domain posint add constraint c1 check(value >= 0);
683+
ERROR: cannot alter type "posint" because column "ddtest2.f1" uses it
684+
drop table ddtest2;
685+
drop type rposint;
669686
alter domain posint add constraint c1 check(value >= 0);
670687
create domain posint2 as posint check (value % 2 = 0);
671688
create table ddtest2(f1 posint2);

src/test/regress/sql/domain.sql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,28 @@ insert into ddtest2 values(row(-1));
451451
alter domain posint add constraint c1 check(value >= 0);
452452
drop table ddtest2;
453453

454+
-- Likewise for domains within arrays of composite
454455
create table ddtest2(f1 ddtest1[]);
455456
insert into ddtest2 values('{(-1)}');
456457
alter domain posint add constraint c1 check(value >= 0);
457458
drop table ddtest2;
458459

460+
-- Likewise for domains within domains over array of composite
461+
create domain ddtest1d as ddtest1[];
462+
create table ddtest2(f1 ddtest1d);
463+
insert into ddtest2 values('{(-1)}');
464+
alter domain posint add constraint c1 check(value >= 0);
465+
drop table ddtest2;
466+
drop domain ddtest1d;
467+
468+
-- Doesn't work for ranges, either
469+
create type rposint as range (subtype = posint);
470+
create table ddtest2(f1 rposint);
471+
insert into ddtest2 values('(-1,3]');
472+
alter domain posint add constraint c1 check(value >= 0);
473+
drop table ddtest2;
474+
drop type rposint;
475+
459476
alter domain posint add constraint c1 check(value >= 0);
460477

461478
create domain posint2 as posint check (value % 2 = 0);

0 commit comments

Comments
 (0)