This topic describes the global plan cache (GPC) feature of PolarDB for PostgreSQL.
Background information
In previous versions of PolarDB, the plan cache was bound to prepared statements. This approach had two drawbacks:
Plan caches were isolated to individual connections and could not be shared.
Each connection maintained its own plan cache, which resulted in high memory usage.
PolarDB for PostgreSQL introduces the GPC feature to address these issues by allowing different connections to share the same plan cache.
Plans can be shared between different prepared statements and connections. For applications with many different SQL statements, GPC can significantly reduce memory usage and lower the risk of out-of-memory (OOM) errors. This efficient plan caching mechanism also reduces the cost of generating execution plans, which improves performance.
Plans can be shared only if their query keys are the same. A query key consists of the following parts:
The query text.
The database ID.
The object search path.
The user ID.
Applicability
The feature is available for the following versions of PolarDB for PostgreSQL:
PostgreSQL 18 (minor engine version 2.0.18.0.1.0 or later)
PostgreSQL 17 (minor engine version 2.0.17.2.1.0 or later)
PostgreSQL 16 (minor engine version 2.0.16.3.1.1 or later)
PostgreSQL 15 (minor engine version 2.0.15.7.1.1 or later)
PostgreSQL 14 (minor engine version 2.0.14.9.15.0 or later)
PostgreSQL 11 (minor engine version 2.0.11.9.28.0 or later)
NoteYou can view the minor engine version number in the console or run the
SHOW polardb_version;statement. If your cluster does not meet the minor engine version requirement, upgrade the minor engine version.The GPC feature is enabled by default on clusters that meet the version requirements.
Limits
GPC supports only prepared statements. Plan caching is not supported in PL/SQL scenarios.
Only
SELECT,INSERT,UPDATE, andDELETEstatements are supported.Temporary tables are not supported.
Parameters
Parameter | Description |
polar_gpc_mem | Sets the memory size for GPC. Unit: MB. The default value is 30 MB. The value cannot exceed the size of Note
|
polar_enable_gpc_level | The level at which the GPC feature is enabled. This parameter can be modified dynamically. Valid values:
Note
|
polar_gpc_clean_timeout | The time interval for clearing infrequently used GPC entries. This parameter can be modified dynamically. Unit: seconds. The default value is 1800 seconds. The value can be from 0 seconds to 24 hours. |
polar_worker.gpc_clear_interval | The time interval for clearing invalid GPC entries. This parameter can be modified dynamically. Unit: seconds. The default value is 60 seconds. The maximum value is (2^32 - 1)/1000. |
polar_gpc_clean_max | The number of GPC entries to clear at a time. This parameter can be modified dynamically. The default value is 100. The value range is 10 to 10000. |
polar_gpc_partitions | The number of hash tables used to store GPC entries. The default value is 32. The value range is 1 to 1024. Note Restart the cluster for the parameter modification to take effect. |
polar_gpc_entries | The maximum number of entries in each hash table. The default value is 1024. The value range is 1 to 10000. Note Restart the cluster for the parameter modification to take effect. |
Usage guide
The monitoring views and functions for the GPC feature are included in the polar_gpc extension. You can run the following command to create this extension.
CREATE EXTENSION IF NOT EXISTS polar_gpc;