]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Prevent a rowtype from being included in itself via a range.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 23 Dec 2019 17:08:24 +0000 (12:08 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 23 Dec 2019 17:08:24 +0000 (12:08 -0500)
We probably should have thought of this case when ranges were added,
but we didn't.  (It's not the fault of commit eb51af71f, because
ranges didn't exist then.)

It's an old bug, so back-patch to all supported branches.

Discussion: https://postgr.es/m/7782.1577051475@sss.pgh.pa.us

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

index 5cb87c105a66ca4d4327f63695e83c5495508fdd..910e73f7eab7a5db74c504e2fae49fb01f9baf78 100644 (file)
@@ -563,6 +563,15 @@ CheckAttributeType(const char *attname,
 
                containing_rowtypes = list_delete_first(containing_rowtypes);
        }
+       else if (att_typtype == TYPTYPE_RANGE)
+       {
+               /*
+                * If it's a range, recurse to check its subtype.
+                */
+               CheckAttributeType(attname, get_range_subtype(atttypid), attcollation,
+                                                  containing_rowtypes,
+                                                  allow_system_table_mods);
+       }
        else if (OidIsValid((att_typelem = get_element_type(atttypid))))
        {
                /*
index 130604b37d01cc15586d4f4b7b5aff28178a39f9..1e3e8b038a5fe41f6737b38fd3962ada1e1ec313 100644 (file)
@@ -1361,6 +1361,25 @@ select array[1,3] <@ arrayrange(array[1,2], array[2,1]);
  t
 (1 row)
 
+--
+-- Ranges of composites
+--
+create type two_ints as (a int, b int);
+create type two_ints_range as range (subtype = two_ints);
+select *, row_to_json(upper(t)) as u from
+  (values (two_ints_range(row(1,2), row(3,4))),
+          (two_ints_range(row(5,6), row(7,8)))) v(t);
+         t         |       u       
+-------------------+---------------
+ ["(1,2)","(3,4)") | {"a":3,"b":4}
+ ["(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_range;
+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
 --
 -- Check behavior when subtype lacks a hash function
 --
index 0793a8235a164be96b82a17c845dc1a226f6ba73..b8030e5aca79a5157cd969a29f419cfc12e6a262 100644 (file)
@@ -451,6 +451,22 @@ select arrayrange(ARRAY[2,1], ARRAY[1,2]);  -- fail
 select array[1,1] <@ arrayrange(array[1,2], array[2,1]);
 select array[1,3] <@ arrayrange(array[1,2], array[2,1]);
 
+--
+-- Ranges of composites
+--
+
+create type two_ints as (a int, b int);
+create type two_ints_range as range (subtype = two_ints);
+
+select *, row_to_json(upper(t)) as u from
+  (values (two_ints_range(row(1,2), row(3,4))),
+          (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_range;
+
+drop type two_ints cascade;
+
 --
 -- Check behavior when subtype lacks a hash function
 --