Skip to content

Preserve client parameters in EXPLAIN rewrites #1771

Open

Description

@anna-poutanen

Prepared EXPLAIN queries lose client parameter metadata when the inner query
contains pgdog.unique_id(). The rewriter skips counting EXPLAIN's client
parameters before allocating generated placeholders. This branch counts the
inner query's parameters, preserving client binds and numbering generated
values after them.

Reproduction: enable rewrite_enabled through the admin connection, then prepare
the following SQL using the extended protocol on a normal database connection:

EXPLAIN (VERBOSE)
SELECT $2::text, pgdog.unique_id(), $1::bigint, pgdog.unique_id()

ParameterDescription should expose bigint and text for $1 and $2. Bind
[42, "first_bind"], then reuse the same statement with [73, "second_bind"].
Both sets of values should appear in their VERBOSE plans. Generated values
must occupy $3 and $4. Repeat with EXPLAIN (ANALYZE, VERBOSE).

On unchanged main, the real-client regression receives an empty parameter list
instead of [Int8, Text]. The patched branch preserves both parameters and their
values across repeated prepared-statement execution.

The production change adds EXPLAIN's inner query to the existing parameter
counting path. Tests cover reordered client parameters, multiple generated
values, ANALYZE, INSERT SELECT, and EXPLAIN without generated values. The
integration test checks single-database and sharded endpoints and restores the
configuration with RELOAD even if an assertion panics.

Branch: https://github.com/anna-poutanen/pgdog/tree/codex/explain-parameters
Base: 20f65c0
Commit: c73b2ea

Validation on this exact base: 2,774 workspace unit tests and all 202
enabled Rust integration tests passed. Formatting, workspace Clippy with
-D warnings, workspace build, and doc tests (20 passed) also passed. The
new regression fails at its intended assertion on unchanged main. Nine unit
tests and five integration tests remain excluded by their configured profiles.
The broader Linux/multi-language/Docker CI matrix has not been run locally.

Focused reproduction commands, after fixture setup and starting PgDog:

cargo nextest run -p pgdog --profile dev -E 'test(test_rewrite_explain_)'
cargo nextest run -p integration_tests_rust --profile integration -E 'test(=integration::explain::explain_unique_id_preserves_client_parameters)'

I have prepared this as a fork-branch contribution following CONTRIBUTING.md.
I would appreciate review of this focused fix.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions