Skip to content

Use set_config instead of SET LOCAL to avoid Postgres warnings - #294

Open
behruzsaifov wants to merge 1 commit into
madelson:masterfrom
behruzsaifov:postgres-set-config-instead-of-set-local
Open

behruzsaifov wants to merge 1 commit into
madelson:masterfrom
behruzsaifov:postgres-set-config-instead-of-set-local

Conversation

@behruzsaifov

@behruzsaifov behruzsaifov commented Oct 21, 2026 •

Copy link
Copy Markdown

Fixes #289.

When the lock is acquired outside a transaction, SET LOCAL makes Postgres log SET LOCAL can only be used in transaction blocks on every acquire, even though it works for the rest of the multi-statement command (#90).

This PR replaces the two SET LOCAL statements in CreateAcquireCommand with set_config(..., true), which behaves the same way but does not log the warning. The calls are wrapped in a DO block so that they don't return a result set and ExecuteScalar still reads the lock result.

RestoreTimeoutSettingsIfNeededAsync is unchanged: it only runs inside a transaction, where SET LOCAL does not warn.

Test

As requested, PostgresBehaviorTest.TestSetConfigIsLocalBehavesLikeSetLocalOutsideOfTransaction runs the repro from #90 (comment) with both SET LOCAL and set_config(..., true), and checks that:

  • the value applies to the rest of the command (12345ms, 'x');
  • it does not leak into the next command (0);
  • only SET LOCAL produces the warning.

I ran the Postgres tests locally (PostgreSQL 18, Debug / net8.0). The new test passes. The tests that fail on my machine fail on master too: TestDoesNotDetectConnectionBreakViaState (expects exactly NpgsqlException, PostgreSQL 18 returns PostgresException), plus cross-process and too many clients failures from my local setup.

SET LOCAL outside a transaction block makes Postgres log "SET LOCAL can only
be used in transaction blocks" on every lock acquisition, even though it works
for the rest of the multi-statement command (madelson#90). set_config(..., true)
behaves the same way but does not log the warning. The calls are wrapped in a
DO block so that the acquire command still returns the lock result as its
first result set.

Adds a PostgresBehaviorTest case showing that set_config(..., true) behaves
like SET LOCAL in the repro from madelson#90.

Fixes madelson#289
@behruzsaifov
behruzsaifov force-pushed the postgres-set-config-instead-of-set-local branch from b7abb7c to 507774e Compare October 21, 2026 10:07
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.

Postgres: use set_config(..., true) instead of SET LOCAL to avoid "SET LOCAL can only be used in transaction blocks" warnings (follow-up to #90)

1 participant