Skip to content

Commit

Permalink
sql: add composite types to pg_class and pg_attribute
Browse files Browse the repository at this point in the history
Previously, user-defined composite types were not populated
in 2 of `pg_catalog`'s tables: `pg_class` and `pg_attribute`
(where the former's row entries pertain to the type and the latter's
pertain to the "columns" of the type). This patch addresses this
PostgreSQL-incompatible behavior by populating such tables with
user-defined composite types.

This patch also ensures that the `typrelid` column in the `pg_type`
table has the proper oid for composite types.

Epic: none
Fixes: #109675

Release note (sql change): `pg_catalog`'s `pg_class` and `pg_attribute`
tables now have context of user-defined composite types (in accordance
with PostgreSQL's corresponding `pg_catalog` tables). In addition, the
`typrelid` column in the `pg_type` table has the proper oid for composite
types.
  • Loading branch information
annrpom committed Oct 18, 2023
1 parent e431569 commit f468116
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 63 deletions.
3 changes: 2 additions & 1 deletion pkg/sql/crdb_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -3767,7 +3767,8 @@ CREATE TABLE crdb_internal.create_statements (
tree.MakeDBool(tree.DBool(table.IsVirtualTable())),
tree.MakeDBool(tree.DBool(table.IsTemporary())),
)
})
},
nil)

func showAlterStatement(
ctx context.Context,
Expand Down
79 changes: 78 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -2007,7 +2007,7 @@ oid typname typcategory typispreferred typisdefined typdel
2207 _regprocedure A false true , 0 2202 0
2210 _regclass A false true , 0 2205 0
2211 _regtype A false true , 0 2206 0
2249 record P false true , 0 0 2287
2249 record P false true , 2249 0 2287
2277 anyarray P false true , 0 0 0
2278 void P false true , 0 0 0
2283 anyelement P false true , 0 0 2277
Expand Down Expand Up @@ -5001,3 +5001,80 @@ oid lanname lanowner lanispl lanpltrusted lanplcallfoid laninline la
12 internal 2310524507 false false 0 0 0 NULL
14 sql 2310524507 false true 0 0 0 NULL
14024 plpgsql 2310524507 true true 0 0 0 NULL

subtest pg_class_composite_types

statement ok
CREATE TYPE u AS (ufoo int, ubar int);

query TTI colnames,rowsort
SELECT
relname,
relkind,
relnatts
FROM
pg_catalog.pg_class
WHERE
relname = 'u';
----
relname relkind relnatts
u c 2

# query that utilizes the "virtual index"
query TTI colnames,rowsort
SELECT
relname,
relkind,
relnatts
FROM
pg_catalog.pg_class
WHERE
oid = 'u'::regtype::oid;
----
relname relkind relnatts
u c 2

subtest end

subtest pg_attribute_composite_types

query TII colnames,rowsort
SELECT
attname,
attlen,
attnum
FROM
pg_catalog.pg_attribute
WHERE
attname IN ('ufoo', 'ubar');
----
attname attlen attnum
ufoo 8 1
ubar 8 2

# query that utilizes the "virtual index"
query TII colnames,rowsort
SELECT
attname,
attlen,
attnum
FROM
pg_catalog.pg_attribute
WHERE
attrelid = 'u'::regtype::oid;
----
attname attlen attnum
ufoo 8 1
ubar 8 2

subtest end

subtest pg_type_composite_types

query TO colnames,rowsort
SELECT typname, typrelid FROM pg_catalog.pg_type WHERE typrelid = 'u'::regtype::oid;
----
typname typrelid
u 100207

subtest end
Loading

0 comments on commit f468116

Please sign in to comment.