Skip to content

Handle the special case of SELECT set_config(...)#1072

Merged
sgrif merged 3 commits into
mainfrom
sg-set-config
Jun 12, 2026
Merged

Handle the special case of SELECT set_config(...)#1072
sgrif merged 3 commits into
mainfrom
sg-set-config

Conversation

@sgrif

@sgrif sgrif commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

This intercepts queries that are in the simplest form of set_config, where it is a select statement containing exactly one call to that function, and all arguments are literals. Any queries like this could have easily been written using SET instead, but this is a form that is emitted by pg_dump, which results in sessions being left in a bad state if we don't handle it explicitly. So we have to handle it.

I took great care to avoid any allocations when we're checking if the select statement is in a form that we can handle, which was slightly more painful than I'd like since we need to check for both qualified an unqualified function names.

Eventually we need to handle this more broadly. set_config can appear anywhere in expression context (with undefined execution order, dear god), and can take any expression as an argument including cross-shard queries. When we have a cheaper and more walkable AST, we should at least warn when we encounter this function in any cases where we don't handle.

We can't just treat it exactly as SET, since the function does return a value and so the client may be expecting rows. I've left the implementation of this somewhat extensible so that we can at least handle selections with multiple calls to set_config down the road. Realistically, long term, we need to go through the whole AST and replace any calls to this with their value, while figuring out which config settings to apply when... somehow.

If we encounter a form that we can't parse (non-literals)

Ultimately the very existence of this function is something of an abomination, and I suspect we'll be avoiding dealing with more complex forms for as long as we possibly can.

Fixes #1055

This intercepts queries that are in the simplest form of `set_config`,
where it is a select statement containing exactly one call to that
function, and all arguments are literals. Any queries like this could
have easily been written using `SET` instead, but this is a form that is
emitted by pg_dump, which results in sessions being left in a bad state
if we don't handle it explicitly. So we have to handle it.

I took great care to avoid any allocations when we're checking if the
select statement is in a form that we can handle, which was slightly
more painful than I'd like since we need to check for both qualified an
unqualified function names.

Eventually we need to handle this more broadly. `set_config` can appear
anywhere in expression context (with undefined execution order, dear
god), and can take any expression as an argument including cross-shard
queries. When we have a cheaper and more walkable AST, we should at
least warn when we encounter this function in any cases where we don't
handle.

We can't *just* treat it exactly as `SET`, since the function does
return a value and so the client may be expecting rows. I've left the
implementation of this somewhat extensible so that we can at least
handle selections with multiple calls to `set_config` down the road.
Realistically, long term, we need to go through the whole AST and
replace any calls to this with their value, while figuring out which
config settings to apply when... somehow.

If we encounter a form that we can't parse (non-literals)

Ultimately the very existence of this function is something of an
abomination, and I suspect we'll be avoiding dealing with more complex
forms for as long as we possibly can.

Fixes #1055
.clone()
.into_iter()
.flatten()
.map(|_| Field::text(""))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the columns need to have names?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Columns do, result fields don't. I can double check, but I'm fairly certain this is the behavior of vanilla pg for selections that aren't columns and don't have aliases

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double checked vanillas behavior, it does set it to the function name. I thought for sure that was something psql did on the client side. Since it's the end of the week, I'm just hard coding it for now. I'll definitely remember to fix it on Monday

@codecov

codecov Bot commented Jun 12, 2026

Copy link
Copy Markdown

@blacksmith-sh

This comment has been minimized.

@sgrif sgrif merged commit 76df7f9 into main Jun 12, 2026
52 of 62 checks passed
@sgrif sgrif deleted the sg-set-config branch June 12, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Handle pg_catalog.set_config

2 participants