The pgpro_result_cache extension caches query results in shared memory using hints, improving performance for subsequent query executions. Cached results are not persisted over server restarts.
The pgpro_result_cache extension is provided
with Postgres Pro Enterprise as a separate pre-built
package pgpro-result-cache-ent-18
(for the detailed installation instructions, see
Chapter 17).
To enable pgpro_result_cache, complete the following steps:
Add the library name to the
shared_preload_libraries variable in the
postgresql.conf file:
shared_preload_libraries = 'pgpro_result_cache'
Restart the database server for the changes to take effect.
To verify that the pgpro_result_cache library has been installed correctly, run the following command:
SHOW shared_preload_libraries;
Create the pgpro_result_cache extension using the following query:
CREATE EXTENSION pgpro_result_cache;
It is essential that the library is preloaded during server startup because pgpro_result_cache has a shared memory cache that can be initialized only during startup. The pgpro_result_cache extension should be created in each database where query caching is required.
Enable the extension using the pgpro_result_cache.enable parameter.
SET pgpro_result_cache.enable = ON;
Specify any of supported hints in the special comment that
begins with /*+ and ends with */.
Note that pgpro_result_cache reads only the
first block of comments. If multiple hints are specified, only the last
hint applies.
result_cache Hint Usage #
Use the result_cache hint to instruct
pgpro_result_cache to process a query.
If a valid cached result set for this query already exists in shared memory, it is returned immediately without query execution and the hit count increments. Otherwise, the query is executed and the result set is saved to shared memory with the current timestamp.
-- Caches the current time SELECT /*+result_cache*/ now(); -- Returns the previously saved time from cache SELECT /*+result_cache*/ now();
The result_cache hint has two optional arguments with
the following keywords:
offset: Defines the number of rows to skip in
the cached result set before beginning to return rows.
limit: Restricts the number of rows returned
from the cached result set. If the result set contains fewer
rows, all of them are returned.
You can specify one of these arguments or both of them in any order. Argument values must be integers. Parameters and expressions are not supported.
These arguments are similar to the
OFFSET and LIMIT
clauses used in queries but apply to cached result sets rather than to
rows retrieved by queries. These queries, in turn, can have their
own OFFSET and LIMIT clauses.
CREATE TABLE t as SELECT generate_series(1,20) x; -- Caches values from 1 to 20, returns values from 1 to 10 SELECT /*+result_cache(limit 10)*/ x FROM t; -- Returns values from 6 to 20 SELECT /*+result_cache(offset 5)*/ x FROM t; -- Both queries return values from 6 to 15 SELECT /*+result_cache(offset 5 limit 10)*/ x FROM t; SELECT /*+result_cache(limit 10 offset 5)*/ x FROM t; -- Caches values from 6 to 20, returns values from 11 to 20 SELECT /*+result_cache(offset 5 limit 10)*/ x FROM t OFFSET 5; -- Caches values from 1 to 10, returns values from 6 to 10 SELECT /*+result_cache(offset 5 limit 10)*/ x FROM t LIMIT 10;
no_result_cache Hint Usage #
Use the no_result_cache hint to instruct
pgpro_result_cache not to process a query,
that is, neither cache query results nor use existing cached results.
For instance, this hint is useful when you set the
pgpro_result_cache.enforced parameter to on
for debugging purposes to cache all queries automatically. This hint
allows excluding individual queries from caching.
SET pgpro_result_cache.enforced = on; -- Caches the current time SELECT now(); -- Returns the previously saved time from cache SELECT now(); -- Returns the current time SELECT /*+no_result_cache*/ now();
All cached result sets are stored in shared memory. You can get information about these result sets using the pgpro_result_cache_data view.
The pgpro_result_cache.max_memory_size parameter sets the maximum size of shared cache memory. The pgpro_result_cache.max_entries parameter specifies the maximum number of cached result sets. If any of these limits is reached when adding a new value, pgpro_result_cache removes result sets from cache memory using the Least Recently Used (LRU) algorithm.
You can also specify the lifetime (Time To Live, TTL) of cached result sets using the pgpro_result_cache.ttl parameter. A result set is considered valid and stored in cache memory until its TTL expires.
By default, automatic cache invalidation is disabled. Note the following specifics of the extension work:
The extension caches results of non-immutable functions causing subsequent calls to return identical output.
The extension captures a result set at transaction time and then does not track updates of cached data even if the transaction is rolled back or not committed. Subsequent queries will return the original cached data even if the underlying data was changed by later transactions.
Cached results bypass the row-level security policy. Ensure that sensitive queries are excluded from caching.
A cached result set is identified by a combination of the
database_id, query_id,
const_hash, params_hash,
and query_string attributes. database_id
and query_id are the attributes assigned by the
Postgres Pro Enterprise server.
const_hash represents a hash digest of all constants
contained in the query. Constants with the same value but different types,
for example, 1 and '1', will produce
different hash values. 0 means there
are no constants.
params_hash stores a hash digest of all parameter
values used in the query. 0 means no parameters were
defined.
The pgpro_result_cache extension supports
automatic invalidation
to ensure consistency of cached results by tracking database changes
and applying specific restrictions. By default, automatic invalidation is
disabled. To enable it, set the pgpro_result_cache.consistent
parameter to on. Changing this parameter resets the cache.
When automatic invalidation is activated, the extension works as follows:
Caches only queries that do not make changes to a database. Does not serve transactions (neither read nor write) that have uncommitted changes.
On each DML operation, such as INSERT,
UPDATE, DELETE, or TRUNCATE,
invalidates cached results associated with modified relations.
On each DDL operation, such as ALTER TABLE or
DROP TABLE, invalidates cached results associated
with modified relations.
Does not cache queries that use temporary tables.
Does not cache queries that use unlogged tables.
Does not cache queries that use custom functions or types.
Does not cache queries that use non-immutable functions.
To enable automatic invalidation at the cluster level, set the
pgpro_result_cache.consistent and
pgpro_result_cache.wal parameters to on
on the primary node and all standby nodes. In this case, the primary and
each standby store their own cached result sets. Each DML
or DDL operation on the primary causes automatic cache
invalidation for the corresponding relations on standbys using WAL files.
Automatic invalidation does not work for table partitions if their data was changed implicitly.
You can use hints of pgpro_result_cache together with hints supported by the pg_hint_plan extension. Each extension reads its supported hints and ignores other hints.
/*+result_cache SeqScan(t1)*/ SELECT * FROM table1 t1 JOIN table table2 t2 ON (t1.key = t2.key);
pgpro_result_cache_data View #
The pgpro_result_cache_data view shows all cached
result sets. The columns of the view
are shown in Table G.84.
Table G.84. pgpro_result_cache_data Columns
| Name | Type | Description |
|---|---|---|
dbid | oid | ID of the database where the query is executed |
query_id | bigint | Standard query ID |
const_hash | bigint | Hash of non-parameterized constants |
params_hash | bigint | Hash of the parameters used to execute the query |
created | timestamp | First caching timestamp |
exec_time_ms | real | Query execution time, in milliseconds |
hits | int | Execution counter |
rows_count | int | Number of rows in the cached result set |
data_size | bigint | Total size of the result set in cache memory, in bytes |
query_string | text | Query text in cache-readable format, without comments or EXPLAIN (ANALYZE) prefixes |
pgpro_result_cache_stat View #
The pgpro_result_cache_stat view shows cache counters.
The columns of the view
are shown in Table G.85.
Table G.85. pgpro_result_cache_stat Columns
| Name | Type | Description |
|---|---|---|
free_kb | bigint | Available cache memory, in kilobytes, limited by pgpro_result_cache.max_memory_size |
entries | bigint | Current number of cached entries, including expired ones (if not vacuumed), limited by pgpro_result_cache.max_entries |
hits | bigint | Number of successful cache retrievals |
inserts | bigint | Number of new entries, including replacements of expired ones |
evicts | bigint | Entries removed by the Least Recently Used (LRU) algorithm due to the pgpro_result_cache.max_entries limit |
cleanups | bigint | Entries removed by the Least Recently Used (LRU) algorithm due to the pgpro_result_cache.max_memory_size limit |
not_cached | bigint | Queries excluded from caching (too fast, too large, other conditions) |
Only superusers can call the functions listed below.
pgpro_result_cache_reset() returns bool
#
Clears the cache and resets all result cache counters. Returns
true if cached result sets were removed and
false if the cache was empty.
pgpro_result_cache.enable (boolean)
#
Enables the pgpro_result_cache functionality.
The default value is off.
Only superusers can change this setting.
pgpro_result_cache.max_memory_size (integer)
#
Sets the size of shared memory used for result set caching, in kilobytes.
The default value is 64kB. This parameter can only
be set in the postgresql.conf file or using the
ALTER SYSTEM command.
pgpro_result_cache.max_entries (integer)
#
Sets the maximum number of cached result sets.
The default value is 128.
This parameter can only be set at server start.
pgpro_result_cache.max_entry_size (integer)
#
Sets the maximum memory consumption by a single result set, in kilobytes.
The default value is 16kB. Only superusers can change
this setting. Must not exceed
.
The query text is stored in memory along with the result data, so this
parameter value should be large enough to fit both. If set at runtime,
it is applied only to new allocations and the cached data is not
evicted automatically.
pgpro_result_cache.max_memory_size / 2
pgpro_result_cache.ttl (integer)
#
Sets the lifetime of a cache entry, in seconds.
The default value is -1, which means no lifetime limit.
Only superusers can change this setting.
pgpro_result_cache.min_exec_time (integer)
#
Sets the minimum execution time for a query, in milliseconds.
The default value is -1, which means no time limit.
A positive integer means that queries with execution time less than
this value will not be stored in cache. Only superusers can change
this setting.
pgpro_result_cache.consistent (boolean)
#
Enables automatic invalidation
of cached results. The default value is off. This
parameter can only be set in the postgresql.conf
file or using the ALTER SYSTEM command. Changing
this parameter resets the cache.
pgpro_result_cache.wal (boolean)
#
Enables the use of WAL files for
automatic cache
invalidation at the cluster level. For this invalidation to work
correctly, you should set both pgpro_result_cache.consistent
and pgpro_result_cache.wal parameters to on
on the primary node and all standby nodes. The
pgpro_result_cache.wal parameter's default value
is off. This parameter can only
be set in the postgresql.conf file or using the
ALTER SYSTEM command.
pgpro_result_cache.enforced (boolean)
#
Enables pgpro_result_cache to cache result
sets for all queries automatically. To exclude specific queries from
caching, you can use the
no_result_cache
hint. The default parameter value is off.
Only superusers can change this setting. Changing this parameter resets the
cache. This parameter is intended for debugging purposes and is not
recommended for production.
pgpro_result_cache.invisible (boolean)
#
Hides pgpro_result_cache information from the
EXPLAIN output. The default value is off.
Only superusers can change this setting. Changing this parameter resets
the cache. This parameter is intended for debugging purposes and is not
recommended for production.