Make EXPLAIN generate a generic plan for a parameterized query

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Make EXPLAIN generate a generic plan for a parameterized query
Date: 2022-10-11 12:37:25
Message-ID: 0a29b954b10b57f0d135fe12aa0909bd41883eb0.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Today you get

test=> EXPLAIN SELECT * FROM tab WHERE col = $1;
ERROR: there is no parameter $1

which makes sense. Nonetheless, it would be great to get a generic plan
for such a query. Sometimes you don't have the parameters (if you grab
the statement from "pg_stat_statements", or if it is from an error message
in the log, and you didn't enable "log_parameter_max_length_on_error").
Sometimes it is just very painful to substitute the 25 parameters from
the detail message.

With the attached patch you can get the following:

test=> SET plan_cache_mode = force_generic_plan;
SET
test=> EXPLAIN (COSTS OFF) SELECT * FROM pg_proc WHERE oid = $1;
QUERY PLAN
═══════════════════════════════════════════════
Index Scan using pg_proc_oid_index on pg_proc
Index Cond: (oid = $1)
(2 rows)

That's not the same as a full-fledged EXPLAIN (ANALYZE, BUFFERS),
but it can definitely be helpful.

I tied that behavior to the setting of "plan_cache_mode" where you
are guaranteed to get a generic plan; I couldn't think of a better way.

Yours,
Laurenz Albe

Attachment Content-Type Size
0001-Add-EXPLAIN-support-for-parameterized-statements.patch text/x-patch 6.6 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Önder Kalacı 2022-10-11 12:44:06 Re: [PATCH] Use indexes on the subscriber when REPLICA IDENTITY is full on the publisher
Previous Message jiye 2022-10-11 12:31:53 Re:Re: Is there any plan to support online schem change in postgresql?