Skip to content
Crunchy Data· @crunchydata · X·· 2d ago

Replaying Simple SELECTs from pg_stat_statements with psql's \bind

Key Takeaway

Queries captured by pg_stat_statements can be re-run from psql by passing arguments with the \bind command when they are simple SELECT statements. Statements with IN lists don't work because pg_stat_statements collapses them; use EXPLAIN (GENERIC_PLAN) instead.

Why It Matters

The approach works for parameterized replay of simple SELECTs sampled from pg_stat_statements; statements with IN lists must fall back to EXPLAIN (GENERIC_PLAN) since their parameters can't be matched.

Technical Breakdown · Distilled from the original source

How it works

  • Take a normalized simple SELECT from pg_stat_statements and pass its arguments via psql's \bind command to execute it directly, without manually substituting placeholders.
  • This requires the statement to be simple enough that parameter positions and count line up with the values supplied to \bind.

Limits and fallback

  • Statements containing IN lists don't work, because pg_stat_statements collapses them, so the parameters can't be restored as-is.
  • For those, use EXPLAIN (GENERIC_PLAN) to inspect the plan in generic form instead of replaying the actual query.
Source: Crunchy Data (@crunchydata)· x.com

PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.

View on X

Related PostgreSQL Reference