Fix query macros against CockroachDB by gating plan_cache_mode on EXPLAIN support#4286
Open
BastienClement wants to merge 1 commit into
Open
Fix query macros against CockroachDB by gating plan_cache_mode on EXPLAIN support#4286BastienClement wants to merge 1 commit into
BastienClement wants to merge 1 commit into
Conversation
The query macros set `plan_cache_mode = 'force_generic_plan'` on their describe connection (transact-rs#3541) to keep nullability inference from being skewed by the NULL placeholder arguments. It was issued inside a `DO` block, which CockroachDB rejects at parse time -- `SET` inside a function body, SQLSTATE 0A000 -- breaking the query macros against CockroachDB since 0.9.0 (transact-rs#4274). The setting only benefits the EXPLAIN-based inference, which already runs only when `is_explain_available()` is true (false for CockroachDB, Materialize and QuestDB). Gate the setting on that same check so it is never issued on a database that cannot use it, replacing the `pg_settings` probe with a `server_version_num() >= 12` guard.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since 0.9.0 the compile-time query macros (
query!,query_as!,query_scalar!) fail against CockroachDB:#3541 prepares the macros' describe connection by running
SET plan_cache_mode = 'force_generic_plan'inside aDO $$ … $$block.CockroachDB rejects
SETinside a function/DObody at parse time, so theblock fails before its
IF EXISTSguard runs. Closes #4274.Fix
plan_cache_mode = force_generic_planexists solely to keep EXPLAIN-basednullability inference from being skewed by the
NULLplaceholder argumentsbound during
describe. That inference already runs only whenis_explain_available()is true — false for CockroachDB, Materialize andQuestDB (detected via their
crdb_version/mz_version/questdb_versionparameter statuses).
The setting is moved out of the generic macro layer into
PgConnection,behind that same
is_explain_available()check (plus aserver_version_num() >= 12guard in place of the oldpg_settingsprobe).The macro layer now calls a per-database
DatabaseExt::prepare_describe_connectionhook (a no-op for MySQL/SQLite). The setting is therefore skipped, by
construction, on every database that cannot use it — fixing CockroachDB and
pre-empting the same breakage on Materialize/QuestDB — while behaving exactly
as before on PostgreSQL.
Behaviour on standard PostgreSQL
Unchanged: the dedicated describe connection still gets
force_generic_plan(verified by the existing
describetests).describe()itself is untouched,so no reused connection is affected.
Testing
tests/postgres/describe.rssuite passes, plus a newit_forces_generic_plan_for_describeregression test.query!macro fails with SQLSTATE 0A000; afterwards it compiles and
plan_cache_modeis left untouched. CockroachDB is not in the CI matrix, so that half cannot
be covered by an automated test; the fix is structural (gated on the
pre-existing, already-tested CockroachDB detection).
cargo clippy -- -D warningsandrustfmt --checkare clean.