Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: fix current_setting(..., true) for custom options #88139

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/pg_builtins
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,34 @@ SELECT current_setting('statement_timeout'), current_setting('search_path')
query error unrecognized configuration parameter
SELECT pg_catalog.current_setting('woo', false)

# Check that current_setting handles custom settings correctly.
query T
SELECT current_setting('my.custom', true)
----
NULL

statement ok
PREPARE check_custom AS SELECT current_setting('my.custom', true)

query T
EXECUTE check_custom
----
NULL

statement ok
BEGIN;
SET LOCAL my.custom = 'foo'

# Check that the existence of my.custom is checked depending on the execution
# context, and not at PREPARE time.
query T
EXECUTE check_custom
----
foo

statement ok
COMMIT

# check error on unsupported session var.
query error configuration setting.*not supported
SELECT current_setting('vacuum_cost_delay', false)
Expand Down
14 changes: 14 additions & 0 deletions pkg/sql/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ type sessionVar struct {
// either by SHOW or in the pg_catalog table.
Get func(evalCtx *extendedEvalContext, kv *kv.Txn) (string, error)

// Exists returns true if this custom session option exists in the current
// context. It's needed to support the current_setting builtin for custom
// options. It is only defined for custom options.
Exists func(evalCtx *extendedEvalContext, kv *kv.Txn) bool

// GetFromSessionData returns a string representation of a given variable to
// be used by BufferParamStatus. This is only required if the variable
// is expected to send updates through ParamStatusUpdate in pgwire.
Expand Down Expand Up @@ -2607,6 +2612,10 @@ func getCustomOptionSessionVar(varName string) (sv sessionVar, isCustom bool) {
}
return v, nil
},
Exists: func(evalCtx *extendedEvalContext, _ *kv.Txn) bool {
_, ok := evalCtx.SessionData().CustomOptions[varName]
return ok
},
Set: func(ctx context.Context, m sessionDataMutator, val string) error {
// TODO(#72026): do some memory accounting.
m.SetCustomOption(varName, val)
Expand All @@ -2629,6 +2638,11 @@ func (p *planner) GetSessionVar(
if err != nil || !ok {
return ok, "", err
}
if existsFn := v.Exists; existsFn != nil {
if missingOk && !existsFn(&p.extendedEvalCtx, p.Txn()) {
return false, "", nil
}
}
val, err := v.Get(&p.extendedEvalCtx, p.Txn())
return true, val, err
}
Expand Down