Skip to content

Commit

Permalink
sql: use crdb_internal for pg_proc.pronamespace when needed
Browse files Browse the repository at this point in the history
Release note (bug fix): The pronamespace column of the pg_proc table
now correctly reports the crdb_internal schema for built-in functions
that have the "crdb_internal" prefix.
  • Loading branch information
rafiss committed Jan 13, 2023
1 parent a3d5366 commit 992fd5d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 16 deletions.
37 changes: 25 additions & 12 deletions pkg/sql/logictest/testdata/logic_test/pg_catalog
Original file line number Diff line number Diff line change
Expand Up @@ -2242,20 +2242,21 @@ oid typname typnamespace typowner typlen typbyval typtype

## pg_catalog.pg_proc

query TOOOTTO colnames
SELECT proname, pronamespace, proowner, prolang, procost, prorows, provariadic
FROM pg_catalog.pg_proc
query TOTOOTTO colnames
SELECT proname, pronamespace, nspname, proowner, prolang, procost, prorows, provariadic
FROM pg_catalog.pg_proc p
JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace
WHERE proname='substring'
----
proname pronamespace proowner prolang procost prorows provariadic
substring 4294967129 NULL 0 NULL NULL 0
substring 4294967129 NULL 0 NULL NULL 0
substring 4294967129 NULL 0 NULL NULL 0
substring 4294967129 NULL 0 NULL NULL 0
substring 4294967129 NULL 0 NULL NULL 0
substring 4294967129 NULL 0 NULL NULL 0
substring 4294967129 NULL 0 NULL NULL 0
substring 4294967129 NULL 0 NULL NULL 0
proname pronamespace nspname proowner prolang procost prorows provariadic
substring 4294967129 pg_catalog NULL 0 NULL NULL 0
substring 4294967129 pg_catalog NULL 0 NULL NULL 0
substring 4294967129 pg_catalog NULL 0 NULL NULL 0
substring 4294967129 pg_catalog NULL 0 NULL NULL 0
substring 4294967129 pg_catalog NULL 0 NULL NULL 0
substring 4294967129 pg_catalog NULL 0 NULL NULL 0
substring 4294967129 pg_catalog NULL 0 NULL NULL 0
substring 4294967129 pg_catalog NULL 0 NULL NULL 0

query TTBBBB colnames
SELECT proname, protransform, proisagg, proiswindow, prosecdef, proleakproof
Expand Down Expand Up @@ -2317,6 +2318,18 @@ substring NULL substring NULL NULL NULL
substring NULL substring NULL NULL NULL
substring NULL substring NULL NULL NULL

query TTOTOT colnames
SELECT proname, prosrc, pronamespace, nspname, prorettype, proargtypes
FROM pg_catalog.pg_proc p
JOIN pg_catalog.pg_namespace n ON p.pronamespace = n.oid
WHERE proname='pb_to_json'
ORDER BY p.oid
----
proname prosrc pronamespace nspname prorettype proargtypes
pb_to_json pb_to_json 4294967295 crdb_internal 3802 25 17
pb_to_json pb_to_json 4294967295 crdb_internal 3802 25 17 16
pb_to_json pb_to_json 4294967295 crdb_internal 3802 25 17 16 16

query TOIOTTB colnames
SELECT proname, provariadic, pronargs, prorettype, proargtypes, proargmodes, proisstrict
FROM pg_catalog.pg_proc
Expand Down
13 changes: 9 additions & 4 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2328,10 +2328,16 @@ https://www.postgresql.org/docs/9.6/view-pg-prepared-statements.html`,
},
}

func addPgProcBuiltinRow(nspOid *tree.DOid, name string, addRow func(...tree.Datum) error) error {
func addPgProcBuiltinRow(name string, addRow func(...tree.Datum) error) error {
props, overloads := builtinsregistry.GetBuiltinProperties(name)
isAggregate := props.Class == tree.AggregateClass
isWindow := props.Class == tree.WindowClass
nspOid := tree.NewDOid(catconstants.PgCatalogID)
if strings.HasPrefix(name, "crdb_internal.") {
nspOid = tree.NewDOid(catconstants.CrdbInternalID)
name = name[len("crdb_internal."):]
}

for _, builtin := range overloads {
dName := tree.NewDName(name)
dSrc := tree.NewDString(name)
Expand Down Expand Up @@ -2524,7 +2530,6 @@ https://www.postgresql.org/docs/9.5/catalog-pg-proc.html`,

err := forEachDatabaseDesc(ctx, p, dbContext, false, /* requiresPrivileges */
func(db catalog.DatabaseDescriptor) error {
nspOid := tree.NewDOid(catconstants.PgCatalogID)
for _, name := range builtins.AllBuiltinNames() {
// parser.Builtins contains duplicate uppercase and lowercase keys.
// Only return the lowercase ones for compatibility with postgres.
Expand All @@ -2536,7 +2541,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-proc.html`,
if unicode.IsUpper(first) {
continue
}
err := addPgProcBuiltinRow(nspOid, name, addRow)
err := addPgProcBuiltinRow(name, addRow)
if err != nil {
return err
}
Expand Down Expand Up @@ -2598,7 +2603,7 @@ https://www.postgresql.org/docs/9.5/catalog-pg-proc.html`,
return true, nil

} else {
err := addPgProcBuiltinRow(tree.NewDOid(catconstants.PgCatalogID), name, addRow)
err := addPgProcBuiltinRow(name, addRow)
if err != nil {
return false, err
}
Expand Down

0 comments on commit 992fd5d

Please sign in to comment.