by rossmohax · 3y ago · 0 comments
- every query has to be planned
- your DB driver can't prepare query as they are all different
- collecting per query stats becomes nightmare if number of arguments per query varies in wide range. metrics cardinality is a problem.
Correct way to handle it is pass all args as single parameter of type array and use `= ANY($1)` or if there are multiple columns build a virtual table and join:
SELECT a,b FROM table
NATURAL JOIN ROWS FROM (
unnest($1::type_of_a[]),
unnest($2::type_of_b[])
) t(a,b)I thought PG planned every query anyway, it does not do plan caching.
I don't even think PG caches plans for functions/sprocs. Back in the day with MSSQL before it had full plan caching based on statement text you could at least make sprocs for common queries and get plan reuse which had a large impact on perf in many apps.
I agree with the original commenter about ANY as well: using IN for dynamic lists of parameters makes viewing useful information in e.g pg_stat_statements impossible, though it's possible there's been some recent work around normalizing these.