Slow function and prepared statement execution

A prepared statement is a valuable technique that rejects SQL injection into your applications as well as allows you to bypass regular query parsing in order to save overhead. You might use one like the following:

PREPARE getvals (int) AS SELECT * FROM t WHERE t.v=$1;
EXECUTE getvals(5);

This returns everything in the rows with a matching v value. The PREPARE saves the output from the query parsing and planning stage. You might think that this will always be a win over directly executing the query if it's being executed more than once, because that overhead will then be amortized over more statements.

This isn't necessarily true. When a statement is prepared, the query optimizer can only produce ...

Get PostgreSQL 10 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.