Skip to content

Commit

Permalink
sql: fix pg_proc virtual oid index
Browse files Browse the repository at this point in the history
Previously, the pg_proc virtual oid index was broken for user-defined
functions.

Release note: None (no release with this bug)
  • Loading branch information
jordanlewis committed Jan 6, 2023
1 parent 39ee43d commit 7221769
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
7 changes: 7 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/udf
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ FROM pg_catalog.pg_proc WHERE proname IN ('proc_f', 'proc_f_2');
100119 proc_f 105 1546506610 14 true true false i 2 25 25 20 {i,i} {"",b} SELECT 'hello';
100121 proc_f_2 120 1546506610 14 false false false v 1 25 25 {i} NULL SELECT 'hello';

# Ensure that the pg_proc virtual index works properly.

query TT
SELECT oid, proname FROM pg_proc WHERE oid = 'sc.proc_f_2'::regproc
----
100121 proc_f_2

statement ok
USE defaultdb;

Expand Down
25 changes: 14 additions & 11 deletions pkg/sql/pg_catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2568,21 +2568,16 @@ https://www.postgresql.org/docs/9.5/catalog-pg-proc.html`,
coid := tree.MustBeDOid(unwrappedConstraint)
ooid := coid.Oid

name, overload, err := p.ResolveFunctionByOID(ctx, ooid)
if err != nil {
if errors.Is(err, tree.ErrFunctionUndefined) {
return false, nil //nolint:returnerrcheck
}
return false, err
}

if funcdesc.IsOIDUserDefinedFunc(ooid) {
fnDesc, err := p.Descriptors().ByID(p.Txn()).WithoutNonPublic().Get().Function(ctx, descpb.ID(overload.Oid))
fnDesc, err := p.Descriptors().ByID(p.Txn()).WithoutNonPublic().Get().Function(ctx, funcdesc.UserDefinedFunctionOIDToID(ooid))
if err != nil {
if errors.Is(err, tree.ErrFunctionUndefined) {
return false, nil //nolint:returnerrcheck
}
return false, err
}

scDesc, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Schema(ctx, descpb.ID(ooid))
scDesc, err := p.Descriptors().ByIDWithLeased(p.Txn()).WithoutNonPublic().Get().Schema(ctx, fnDesc.GetParentSchemaID())
if err != nil {
return false, err
}
Expand All @@ -2597,7 +2592,15 @@ https://www.postgresql.org/docs/9.5/catalog-pg-proc.html`,
return true, nil

} else {
err := addPgProcBuiltinRow(tree.NewDOid(catconstants.PgCatalogID), name, addRow)
name, _, err := p.ResolveFunctionByOID(ctx, ooid)
if err != nil {
if errors.Is(err, tree.ErrFunctionUndefined) {
return false, nil //nolint:returnerrcheck
}
return false, err
}

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

0 comments on commit 7221769

Please sign in to comment.