Skip to content

query! macros fail against CockroachDB in 0.9.0: SET inside DO block not supported #4274

@BastienClement

Description

@BastienClement

I have found these related issues/pull requests

This was introduced in #3541 (force generic plans for nullability inference).

Description

Since 0.9.0, every compile-time query macro (query!, query_as!, query_scalar!) fails when run against a CockroachDB database. On each new describe connection, sqlx-macros-core runs a DO $$ … $$ block containing a SET SESSION statement, and CockroachDB does not implement SET inside a function/DO body. This breaks compile-time-checked queries against CockroachDB entirely.

This is a regression from 0.8.x, which had no such statement. It is significant, as this makes compile-time checked SQL unusable!

Error

error: error returned from database: unimplemented: SET usage inside a function definition
(SQLSTATE 0A000)

Root cause

sqlx-macros-core/src/database/mod.rs, in CachingDescribeBlocking::describe (the hash_map::Entry::Vacant arm), runs the following on every newly opened Postgres describe connection:

DO $$
BEGIN
    IF EXISTS (SELECT 1 FROM pg_settings WHERE name = 'plan_cache_mode') THEN
        SET SESSION plan_cache_mode = 'force_generic_plan';
    END IF;
END $$;

CockroachDB rejects SET inside a routine/DO body at parse/build time, so the IF EXISTS (… 'plan_cache_mode') guard does not help — the block is rejected before it ever runs, regardless of whether plan_cache_mode exists. The failure happens during connection setup, before any user query is described, so it is independent of the query and the schema.

Proposed fix

Skip the plan_cache_mode setup for CockroachDB. sqlx already detects CockroachDB elsewhere — is_explain_available() in sqlx-postgres/src/connection/describe.rs checks for the crdb_version parameter status. The same detection could gate this block.

Reproduction steps

Point DATABASE_URL at a CockroachDB instance and compile any crate containing a query! macro. Or run the block above directly against CockroachDB.

SQLx version

0.9.0

Enabled SQLx features

postgres

Database server and version

CockroachDB v26.2

Operating system

Linux

Rust version

1.95.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions