Skip to content

LIKE wildcards break psycopg parameter parsing #1

@amiller

Description

@amiller

Bug

SQL queries with LIKE '%...' patterns fail because psycopg interprets % in the SQL string as parameter placeholders.

Reproduction

Query: "Is there any green juice videos"

The query agent generates:

SELECT * FROM watch_history WHERE LOWER(title) LIKE '%green juice%'

psycopg rejects this with:

only '%s', '%b', '%t' are allowed as placeholders, got '%g'

The %g in %green is parsed as a psycopg format specifier instead of a SQL LIKE wildcard.

Root cause

db.execute() passes SQL through psycopg's parameter substitution. Literal % characters in SQL need to be escaped as %%, or (better) the LIKE pattern should be passed as a parameter:

SELECT * FROM watch_history WHERE LOWER(title) LIKE %s
-- params: ['%green juice%']

The query agent inlines string values directly in SQL instead of using parameterized queries.

Possible fixes

  1. Agent prompt fix: instruct query agents to always use %s parameterized queries for string values
  2. Engine fix: escape % in SQL before passing to psycopg (risky — could break intentional %s params)
  3. Engine fix: switch psycopg to ClientCursor with mogrify() or use a different param style

Impact

Any LIKE/ILIKE query with a pattern starting with a letter after % fails silently — the agent gets an error and reports "technical error" to the user instead of results.

Found via trace logging on the live CVM deployment.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No 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