]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Don't allow composite type to be member of itself via multirange
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Thu, 23 Apr 2026 18:28:11 +0000 (21:28 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Thu, 23 Apr 2026 18:33:25 +0000 (21:33 +0300)
CheckAttributeType() checks that a composite type is not made a member
of itself with ALTER TABLE ADD COLUMN or ALTER TYPE ADD ATTRIBUTE,
even indirectly via a domain, array, another composite type or a range
type. But it missed checking for multiranges. That was a simple
oversight when multiranges were added.

Reviewed-by: Chao Li <li.evan.chao@gmail.com>
Discussion: https://www.postgresql.org/message-id/93ce56cd-02a6-4db1-8224-c8999372facc@iki.fi
Backpatch-through: 14

src/backend/catalog/heap.c
src/test/regress/expected/multirangetypes.out
src/test/regress/sql/multirangetypes.sql

index fcaabecf37a482c372e9da62078f7b49766c7960..0653194c78d8e9b25e292806dbe821cb1e00cff8 100644 (file)
@@ -647,6 +647,16 @@ CheckAttributeType(const char *attname,
                                                   containing_rowtypes,
                                                   flags);
        }
+       else if (att_typtype == TYPTYPE_MULTIRANGE)
+       {
+               /*
+                * If it's a multirange, recurse to check its plain range type.
+                */
+               CheckAttributeType(attname, get_multirange_range(atttypid),
+                                                  InvalidOid,  /* range types are not collatable */
+                                                  containing_rowtypes,
+                                                  flags);
+       }
        else if (OidIsValid((att_typelem = get_element_type(atttypid))))
        {
                /*
index ac2eb84c3af90db38a14609749a5607653d0b26e..c42ab672ce8374a13552092a256243a9e2b36458 100644 (file)
@@ -3236,6 +3236,9 @@ select *, row_to_json(upper(t)) as u from
  {["(5,6)","(7,8)")} | {"a":7,"b":8}
 (2 rows)
 
+-- this must be rejected to avoid self-inclusion issues:
+alter type two_ints add attribute c two_ints_multirange;
+ERROR:  composite type two_ints cannot be made a member of itself
 drop type two_ints cascade;
 NOTICE:  drop cascades to type two_ints_range
 --
index 1abcaeddb5c490798f58e29fe8211e694d8108cb..8a6b51d4e1d6e20118810f0ef04d65a9a4fe4b4f 100644 (file)
@@ -787,6 +787,9 @@ select *, row_to_json(upper(t)) as u from
   (values (two_ints_multirange(two_ints_range(row(1,2), row(3,4)))),
           (two_ints_multirange(two_ints_range(row(5,6), row(7,8))))) v(t);
 
+-- this must be rejected to avoid self-inclusion issues:
+alter type two_ints add attribute c two_ints_multirange;
+
 drop type two_ints cascade;
 
 --