Global Plan Cache

Updated at:
Copy as MD

In high-concurrency workloads, each connection maintains its own plan cache for prepared statements. This isolates memory per connection, increasing total memory usage and the risk of Out-of-Memory (OOM) errors—with no way to reuse plans across sessions.

Global Plan Cache (GPC) eliminates this overhead by storing execution plans in shared memory. Connections that issue queries with the same query key share a single cached plan, reducing both memory consumption and plan generation cost.

GPC is supported on PolarDB for PostgreSQL (Compatible with Oracle) clusters running PostgreSQL 11 (revision version 1.1.30 or later) and is enabled by default. To check your cluster's minor version, run SHOW polar_version;.

How GPC works

A query key determines plan sharing. Two queries share a cached plan only when all four components of their query keys match:

ComponentDescription
Query statementThe SQL text of the prepared statement
Database IDThe database in which the statement runs
Object search pathThe schema search path at execution time
User IDThe role executing the statement

When shared memory is full, new plans fall back to local disk. After GPC cleans up infrequently used or invalid entries, those on-disk plans may be promoted back to shared memory.

Limitations

  • GPC applies only to prepared statements. PL/SQL scenarios are not supported.

  • Only SELECT, INSERT, UPDATE, and DELETE statements are supported.

  • Temporary tables are not supported.

Parameters

ParameterDescriptionDefaultValid valuesRestart required
polar_gpc_memMemory allocated to GPC, in MB. Set to 0 or a negative value to disable GPC. Must not exceed the shared buffer capacity.30Any value within shared buffer capacityYes
polar_enable_gpc_levelNode types on which GPC is active. 0: disabled. 1: read-only nodes only. 2: read-write and read-only nodes. GPC is active only when polar_gpc_mem > 0 and this parameter is 1 or 2. If polar_gpc_mem > 0 but this parameter is 0, queries that already used GPC continue to do so, but new queries cannot enter GPC.00, 1, 2No
polar_gpc_clean_timeoutInterval, in seconds, at which infrequently used GPC entries are evicted.18000–86400No
polar_worker.gpc_clear_intervalInterval, in seconds, at which invalid GPC entries are evicted.600–4294967No
polar_gpc_clean_maxMaximum number of GPC entries deleted per cleanup batch.10010–10000No
polar_gpc_partitionsNumber of hash tables used to store GPC entries.321–1024Yes
polar_gpc_entriesMaximum number of entries per hash table.10241–10000Yes
Changes to polar_gpc_mem, polar_gpc_partitions, and polar_gpc_entries require a cluster restart to take effect.

Monitor GPC

Overall statistics

Query the polar_stat_gpc view for aggregate GPC metrics:

SELECT * FROM polar_stat_gpc;
FieldDescription
getTotal cache lookup attempts
hitSuccessful cache hits
storePlans successfully added to GPC
store_failedPlans that failed to cache due to insufficient memory. A consistently high value indicates that polar_gpc_mem may need to be increased.
store_existsCache attempts skipped because another session had already written the same plan

Use the hit rate (hit / get) to assess GPC effectiveness. If store_failed is growing, increase polar_gpc_mem.

Per-plan details

Query the polar_gpc_plan view for usage and validity of each cached plan:

SELECT * FROM polar_gpc_plan;
FieldDescription
plan_idQuery plan ID
stmt_namePrepared statement name
querySQL statement
used_cntNumber of times this plan has been used
last_use_timeTimestamp of the most recent use
is_validWhether the plan is still valid

Per-plan memory context

Query the polar_gpc_plan_mcxt view for memory details of each cached plan:

SELECT * FROM polar_gpc_plan_mcxt;
FieldDescription
plan_idQuery plan ID
mcxt_nameMemoryContext name
totalspaceTotal allocated memory
freespaceAvailable memory within the context
usedMemory in use
nblocksNumber of memory blocks

Per-plan query key

Query the polar_gpc_plan_key view to inspect the query key of each cached plan:

SELECT * FROM polar_gpc_plan_key;
FieldDescription
plan_idPlan ID
querySQL statement
dbidDatabase ID
pidProcess ID
num_paramsNumber of query parameters
search_pathObject search path
role_idUser ID

Diagnose uncacheable statements

Query the polar_prepared_statement view to obtain information about all prepared statements of the GPC:

SELECT * FROM polar_prepared_statement;
FieldDescription
is_savedWhether the plan has been saved to GPC
is_validWhether the plan is valid
cacheableWhether the plan is eligible for GPC caching

Filter on cacheable = false to identify prepared statements that cannot enter GPC—for example, those involving temporary tables or unsupported statement types. This is the most direct way to confirm why a specific statement is not being cached.

Manage GPC

GPC is cleaned up automatically based on polar_worker.gpc_clear_interval and polar_gpc_clean_timeout. Use the following functions to trigger cleanup manually.

Delete invalid plans

SELECT polar_gpc_evict_invalid_gpc();

Removes plans marked as invalid—for example, after a DDL change invalidates a cached plan. The system also runs this cleanup automatically at the polar_worker.gpc_clear_interval interval.

Delete infrequently used plans

SELECT polar_gpc_evict_live_gpc();

Removes plans that have not been used within the polar_gpc_clean_timeout window. The system also runs this cleanup automatically at the polar_worker.gpc_clear_interval interval.