Skip to content

Commit

Permalink
Merge #88009
Browse files Browse the repository at this point in the history
88009: sql: use schema desc ID for OIDs in pg_catalog r=rafiss a=knz

Needed for #87606.

Prior to this patch, a synthetic OID was used for
"namespace" (schema) references in `pg_catalog`.

This was making it difficult/impossible to retrieve schema comments using a non-`root` SQL query, e.g. via

```sql
SELECT nspname AS schema,
       coalesce(pc.comment, sc.comment) as description
  FROM pg_catalog.pg_namespace t
LEFT OUTER JOIN system.comments sc
    ON t.oid = sc.object_id AND sc.type = 4
LEFT OUTER JOIN crdb_internal.predefined_comments pc
    ON t.oid = pc.object_id AND pc.type = 4
```

This patch fixes it.

Release note: None

Co-authored-by: Raphael 'kena' Poss <[email protected]>
  • Loading branch information
craig[bot] and knz committed Sep 16, 2022
2 parents b01ebf7 + 0140d75 commit 4b2ee12
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 114 deletions.
8 changes: 6 additions & 2 deletions pkg/sql/information_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,9 @@ https://www.postgresql.org/docs/9.5/infoschema-check-constraints.html`,
// uses the format <namespace_oid>_<table_oid>_<col_idx>_not_null.
// We might as well do the same.
conNameStr := tree.NewDString(fmt.Sprintf(
"%s_%s_%d_not_null", h.NamespaceOid(db.GetID(), scName), tableOid(table.GetID()), column.Ordinal()+1,
"%s_%s_%d_not_null",
h.NamespaceOid(db, scName),
tableOid(table.GetID()), column.Ordinal()+1,
))
chkExprStr := tree.NewDString(fmt.Sprintf(
"%s IS NOT NULL", column.GetName(),
Expand Down Expand Up @@ -1308,7 +1310,9 @@ https://www.postgresql.org/docs/9.5/infoschema-table-constraints.html`,
}
// NOT NULL column constraints are implemented as a CHECK in postgres.
conNameStr := tree.NewDString(fmt.Sprintf(
"%s_%s_%d_not_null", h.NamespaceOid(db.GetID(), scName), tableOid(table.GetID()), col.Ordinal()+1,
"%s_%s_%d_not_null",
h.NamespaceOid(db, scName),
tableOid(table.GetID()), col.Ordinal()+1,
))
if err := addRow(
dbNameStr, // constraint_catalog
Expand Down
34 changes: 17 additions & 17 deletions pkg/sql/logictest/testdata/logic_test/information_schema
Original file line number Diff line number Diff line change
Expand Up @@ -1983,25 +1983,25 @@ SELECT *
FROM information_schema.table_constraints
ORDER BY TABLE_NAME, CONSTRAINT_TYPE, CONSTRAINT_NAME
----
constraint_catalog constraint_schema constraint_name table_catalog table_schema table_name constraint_type is_deferrable initially_deferred
constraint_db public 3864823197_118_1_not_null constraint_db public t1 CHECK NO NO
constraint_db public c2 constraint_db public t1 CHECK NO NO
constraint_db public check_a constraint_db public t1 CHECK NO NO
constraint_db public t1_pkey constraint_db public t1 PRIMARY KEY NO NO
constraint_db public t1_a_key constraint_db public t1 UNIQUE NO NO
constraint_db public 3864823197_119_2_not_null constraint_db public t2 CHECK NO NO
constraint_db public fk constraint_db public t2 FOREIGN KEY NO NO
constraint_db public t2_pkey constraint_db public t2 PRIMARY KEY NO NO
constraint_catalog constraint_schema constraint_name table_catalog table_schema table_name constraint_type is_deferrable initially_deferred
constraint_db public 117_118_1_not_null constraint_db public t1 CHECK NO NO
constraint_db public c2 constraint_db public t1 CHECK NO NO
constraint_db public check_a constraint_db public t1 CHECK NO NO
constraint_db public t1_pkey constraint_db public t1 PRIMARY KEY NO NO
constraint_db public t1_a_key constraint_db public t1 UNIQUE NO NO
constraint_db public 117_119_2_not_null constraint_db public t2 CHECK NO NO
constraint_db public fk constraint_db public t2 FOREIGN KEY NO NO
constraint_db public t2_pkey constraint_db public t2 PRIMARY KEY NO NO

query TTTT colnames
SELECT *
FROM information_schema.check_constraints
ORDER BY CONSTRAINT_CATALOG, CONSTRAINT_NAME
----
constraint_catalog constraint_schema constraint_name check_clause
constraint_db public 3864823197_118_1_not_null p IS NOT NULL
constraint_db public c2 ((a < 99:::INT8))
constraint_db public check_a ((a > 4:::INT8))
constraint_catalog constraint_schema constraint_name check_clause
constraint_db public 117_118_1_not_null p IS NOT NULL
constraint_db public c2 ((a < 99:::INT8))
constraint_db public check_a ((a > 4:::INT8))

query TTTTTTT colnames
SELECT *
Expand All @@ -2026,10 +2026,10 @@ USING (constraint_catalog, constraint_schema, constraint_name)
WHERE tc.table_schema in ('public')
ORDER BY tc.table_schema, tc.table_name, cc.constraint_name
----
table_schema table_name constraint_name check_clause
public t1 3864823197_118_1_not_null p IS NOT NULL
public t1 c2 ((a < 99:::INT8))
public t1 check_a ((a > 4:::INT8))
table_schema table_name constraint_name check_clause
public t1 117_118_1_not_null p IS NOT NULL
public t1 c2 ((a < 99:::INT8))
public t1 check_a ((a > 4:::INT8))

statement ok
DROP DATABASE constraint_db CASCADE
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/orms
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ SELECT
typdefaultbin
FROM pg_type WHERE typname = 'regression_66576'
----
regression_66576 4101115737 c C false 0 -1 0 -1 NULL
regression_66576 105 c C false 0 -1 0 -1 NULL

query T
SELECT reltype FROM pg_class WHERE relname = 'regression_65576'
Expand Down
Loading

0 comments on commit 4b2ee12

Please sign in to comment.