Skip to content

A $N sequence inside a string literal is counted as a parameter placeholder when the client declares no parameter types at Parse, so ParameterDescription overstates the statement's parameter count #11029

Closed

Description

@ruispereira

SEVERITY: MEDIUM

Summary

When a Parse declares zero parameter types, which asyncpg and node-postgres do, parseCommand() recovers
the count by scanning the raw query text for $N with a regular expression and declares that many
varchar parameters. The scan does not respect string literals, dollar-quoted bodies or comments, so a
literal containing a dollar sign followed by digits is counted. The clients this fallback exists for
are exactly the ones that read ParameterDescription and validate the caller's argument count against
it, so they refuse a statement that needs no arguments.

Where (HEAD 21d2a0e)

  • postgresw/src/main/java/com/arcadedb/postgres/PostgresNetworkExecutor.java:4692, detectParameterPlaceholders(), the regular expression scan over the raw text
  • postgresw/src/main/java/com/arcadedb/postgres/PostgresNetworkExecutor.java:3407, its only call site, the paramCount == 0 arm, whose comment names asyncpg and node-postgres
  • postgresw/src/main/java/com/arcadedb/postgres/PostgresNetworkExecutor.java:4672, writeParameterDescription()

Repro

A Parse declaring zero parameter types for a statement with no parameters and one literal. The text is
"SELECT 'price is $1 today' AS v".

PROBE 3: $N inside a string literal, Parse declaring 0 parameter types
### Parse(0 declared types) + Describe(S) -> [1, t, T, Z]
    t count=1 oids=[1043]
    T fields=1 [v tbl=0 attnum=0 oid=1043 len=-1 mod=-1 fmt=0]
-- real PostgreSQL: ParameterDescription count=0 --

### Bind(0 values) + Execute -> [2, D, C, Z]
    D 'price is $1 today'
    C tag=SELECT 1

The row is correct; the parameter count the client was told is not.

Refutation attempted

Does it break execution? Not on its own: the literal is untouched and the right row comes back. The
damage is entirely in the metadata and lands on a client that trusts ParameterDescription. Hence MEDIUM.

Does the engine mistake the literal for a parameter too? No. BaseExpression resolves $N only for an
identifier, and this $1 is inside a string literal, so it is never a candidate. The defect is confined
to the protocol's own count.

Harmless because clients declaring no types also do not read the description? The call site's comment
names asyncpg, and asyncpg does read it and does validate argument count against it. Combined with the
missing Bind count check reported above, the two cancel for a client that ignores the description and
compound for one that honours it.

Suggested fix

Scan with the literal awareness the rest of the module already applies to query text, skipping
single-quoted literals, dollar-quoted bodies, double-quoted identifiers and comments, so only a $N
appearing as an expression is counted. PostgresQuotedIdentifierRewriter already walks the text with
literal awareness and is the natural place to borrow the scanner from.

The guarantee to ask for: the count recovered from the text equals the count the SQL parser would
report for the same text, so a $N inside any quoted construct is never counted. Tying the recovered
count to the parser rather than to a regular expression stops the next quoting form from reopening it.

Scope / confidence

Executed: probe 3, output verbatim. Read: detectParameterPlaceholders(), its single call site,
writeParameterDescription(), and BaseExpression's resolution to confirm the literal is not mistaken for
a parameter at execution. I did not run asyncpg; the asyncpg consequence is stated from its documented
argument validation and from the call site's own comment naming it, and is the one part of this finding
I did not execute.

Dedup

Checked 10870, 10721, 8573, 10695 (a statement whose text merely contains pg_catalog answered without
running, the same class of raw-text matching that ignores string literals, elsewhere), 10882 (the
pg_type element-join recogniser still matching raw query text, string literals included). 10882 is the
closest neighbour and is the same root cause in a different scanner; it is CLOSED and covered the
catalog recogniser, not the parameter scan. Worth naming 10882 in the issue as the sibling.


Activity

  1. added
    severity:minor
    on Oct 30, 2026
  2. added this to the 26.11.1 milestone on Oct 31, 2026
  3. added
    in progress
    on Nov 2, 2026
  4. self-assigned this
    on Nov 2, 2026
  5. added a commit that references this issue on Nov 2, 2026
  6. removed
    in progress
    on Nov 2, 2026
  7. lvca commented on Nov 2, 2026

    @lvca
    Member

    This was already fixed by #11123 (issue #11018): the parameter count is now read the way the SQL parser reads the text, so a $N inside a string literal, a quoted identifier or a comment is no longer counted. SELECT 'price is $1 today' AS v now describes 0 parameters. #11148 adds a regression test for exactly this case. Available from 26.11.1.


    ❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  8. added a commit that references this issue on Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugpostgresql-wireseverity:minor

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions