Prevent a rowtype from being included in itself.
authorTom Lane <[email protected]>
Mon, 28 Mar 2011 19:45:14 +0000 (15:45 -0400)
committerTom Lane <[email protected]>
Mon, 28 Mar 2011 19:45:14 +0000 (15:45 -0400)
Eventually we might be able to allow that, but it's not clear how many
places need to be fixed to prevent infinite recursion when there's a direct
or indirect inclusion of a rowtype in itself.  One such place is
CheckAttributeType(), which will recurse to stack overflow in cases such as
those exhibited in bug #5950 from Alex Perepelica.  If we were sure it was
the only such place, we could easily modify the code added by this patch to
stop the recursion without a complaint ... but it probably isn't the only
such place.  Hence, throw error until such time as someone is excited
enough about this type of usage to put work into making it safe.

Back-patch as far as 8.3.  8.2 doesn't have the recursive call in
CheckAttributeType in the first place, so I see no need to add code there
in the absence of clear evidence of a problem elsewhere.

src/backend/catalog/heap.c
src/backend/commands/tablecmds.c
src/include/catalog/heap.h
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index 9efef44778543b5f3e249ba6bc2c052886b0e80a..edc5427341ad09c056e3bfc81214577dd639c599 100644 (file)
@@ -388,7 +388,8 @@ CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind)
        for (i = 0; i < natts; i++)
        {
                CheckAttributeType(NameStr(tupdesc->attrs[i]->attname),
-                                                  tupdesc->attrs[i]->atttypid);
+                                                  tupdesc->attrs[i]->atttypid,
+                                                  NIL /* assume we're creating a new rowtype */);
        }
 }
 
@@ -396,14 +397,23 @@ CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind)
  *             CheckAttributeType
  *
  *             Verify that the proposed datatype of an attribute is legal.
- *             This is needed because there are types (and pseudo-types)
+ *             This is needed mainly because there are types (and pseudo-types)
  *             in the catalogs that we do not support as elements of real tuples.
+ *             We also check some other properties required of a table column.
+ *
+ * If the attribute is being proposed for addition to an existing table or
+ * composite type, pass a one-element list of the rowtype OID as
+ * containing_rowtypes.  When checking a to-be-created rowtype, it's
+ * sufficient to pass NIL, because there could not be any recursive reference
+ * to a not-yet-existing rowtype.
  * --------------------------------
  */
 void
-CheckAttributeType(const char *attname, Oid atttypid)
+CheckAttributeType(const char *attname, Oid atttypid,
+                                  List *containing_rowtypes)
 {
        char            att_typtype = get_typtype(atttypid);
+       Oid                     att_typelem;
 
        if (atttypid == UNKNOWNOID)
        {
@@ -439,6 +449,20 @@ CheckAttributeType(const char *attname, Oid atttypid)
                TupleDesc       tupdesc;
                int                     i;
 
+               /*
+                * Check for self-containment.  Eventually we might be able to allow
+                * this (just return without complaint, if so) but it's not clear how
+                * many other places would require anti-recursion defenses before it
+                * would be safe to allow tables to contain their own rowtype.
+                */
+               if (list_member_oid(containing_rowtypes, atttypid))
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
+                                        errmsg("composite type %s cannot be made a member of itself",
+                                                       format_type_be(atttypid))));
+
+               containing_rowtypes = lcons_oid(atttypid, containing_rowtypes);
+
                relation = relation_open(get_typ_typrelid(atttypid), AccessShareLock);
 
                tupdesc = RelationGetDescr(relation);
@@ -449,10 +473,21 @@ CheckAttributeType(const char *attname, Oid atttypid)
 
                        if (attr->attisdropped)
                                continue;
-                       CheckAttributeType(NameStr(attr->attname), attr->atttypid);
+                       CheckAttributeType(NameStr(attr->attname), attr->atttypid,
+                                                          containing_rowtypes);
                }
 
                relation_close(relation, AccessShareLock);
+
+               containing_rowtypes = list_delete_first(containing_rowtypes);
+       }
+       else if (OidIsValid((att_typelem = get_element_type(atttypid))))
+       {
+               /*
+                * Must recurse into array types, too, in case they are composite.
+                */
+               CheckAttributeType(attname, att_typelem,
+                                                  containing_rowtypes);
        }
 }
 
index 14757befdff06a127032a52729e550c3af42694c..4e3e04d077bb485b92bf6364df5fefed91ced765 100644 (file)
@@ -3119,7 +3119,8 @@ ATExecAddColumn(AlteredTableInfo *tab, Relation rel,
        typeOid = HeapTupleGetOid(typeTuple);
 
        /* make sure datatype is legal for a column */
-       CheckAttributeType(colDef->colname, typeOid);
+       CheckAttributeType(colDef->colname, typeOid,
+                                          list_make1_oid(rel->rd_rel->reltype));
 
        attributeTuple = heap_addheader(Natts_pg_attribute,
                                                                        false,
@@ -4818,7 +4819,8 @@ ATPrepAlterColumnType(List **wqueue,
        targettype = typenameTypeId(NULL, typename, &targettypmod);
 
        /* make sure datatype is legal for a column */
-       CheckAttributeType(colName, targettype);
+       CheckAttributeType(colName, targettype,
+                                          list_make1_oid(rel->rd_rel->reltype));
 
        /*
         * Set up an expression to transform the old data value to the new type.
index 727af295c769f47f4e96a34863b8080faa464eb0..678c11e7f6eafd9882fc23f8bc286becf3d5240a 100644 (file)
@@ -98,6 +98,7 @@ extern Form_pg_attribute SystemAttributeByName(const char *attname,
 
 extern void CheckAttributeNamesTypes(TupleDesc tupdesc, char relkind);
 
-extern void CheckAttributeType(const char *attname, Oid atttypid);
+extern void CheckAttributeType(const char *attname, Oid atttypid,
+                                                          List *containing_rowtypes);
 
 #endif   /* HEAP_H */
index 51d5afa81fd9a771576c2e42e27a7c77db3c071d..a6e4eeaaa3be9e0b695419ae3de88799da539607 100644 (file)
@@ -1384,6 +1384,18 @@ select * from another;
 (3 rows)
 
 drop table another;
+-- disallow recursive containment of row types
+create temp table recur1 (f1 int);
+alter table recur1 add column f2 recur1; -- fails
+ERROR:  composite type recur1 cannot be made a member of itself
+alter table recur1 add column f2 recur1[]; -- fails
+ERROR:  composite type recur1 cannot be made a member of itself
+create temp table recur2 (f1 int, f2 recur1);
+alter table recur1 add column f2 recur2; -- fails
+ERROR:  composite type recur1 cannot be made a member of itself
+alter table recur1 add column f2 int;
+alter table recur1 alter column f2 type recur2; -- fails
+ERROR:  composite type recur1 cannot be made a member of itself
 --
 -- alter function
 --
index 81cc70612d5ae5194edbf16413ec761aa2b2d713..c6ce7ae4db70857bb2d138bed10c12d75c2d313a 100644 (file)
@@ -1045,6 +1045,15 @@ select * from another;
 
 drop table another;
 
+-- disallow recursive containment of row types
+create temp table recur1 (f1 int);
+alter table recur1 add column f2 recur1; -- fails
+alter table recur1 add column f2 recur1[]; -- fails
+create temp table recur2 (f1 int, f2 recur1);
+alter table recur1 add column f2 recur2; -- fails
+alter table recur1 add column f2 int;
+alter table recur1 alter column f2 type recur2; -- fails
+
 --
 -- alter function
 --