pg_plan_filter: Restrict Total Cost
pg_plan_filter is PostgreSQL extension to block execution of statements where query planner's estimate of the total cost exceeds a threshold. This is intended to give database administrators a way to restrict the contribution an individual query has on database load.
Enable the extension#
pg_plan_filter
can be enabled on a per connection basis:
or for all connections:
API#
plan_filter.statement_cost_limit
: restricts the maximum total cost for executed statements
plan_filter.limit_select_only
: restricts to select
statements
Note that limit_select_only = true
is not the same as read-only because select
statements may modfiy data e.g. through a function call.
Example#
To demonstrate total cost filtering, we'll compare how plan_filter.statement_cost_limit
treats queries that are under and over its cost limit. First, we set up a table with some data:
Next, we can review the explain plans for a single record select, and a whole table select.
Now we can choose a statement_cost_filter
value between the total cost for the single select (2.49) and the whole table select (135.0) so one statement will succeed and one will fail.
Resources#
- Official pg_plan_filter documentation