All Products
Search
Document Center

ApsaraDB RDS:Use the cache eviction feature

Last Updated:Dec 27, 2023

The process-level cache eviction feature is supported to reduce the memory usage of persistent connections and ensure that your database does not run out of memory due to excessive persistent connections. The feature uses the Least Recently Used (LRU) policy to release caches that are infrequently used. This reduces the memory usage of connections and improves the stability of ApsaraDB RDS for PostgreSQL instances.

Prerequisites

The RDS instance runs PostgreSQL 14 or later. If the feature is still not supported, you must update the minor engine version of the RDS instance. For more information, see Update the minor engine version.

Background information

Each time a backend process in PostgreSQL accesses a table, the metadata of the table is cached to your on-premises device, and the cache is not proactively released. This reduces subsequent disk access. As a result, persistent connections may generate a large number of caches and cause out of memory (OOM) errors. To resolve this issue, the cache eviction feature is introduced to release the caches that are infrequently used at the earliest opportunity.

Scenarios

Software as a service (SaaS) scenarios in which a large number of persistent connections exist

Impacts

The modification of the parameters that are involved in this topic does not trigger instance restart.

Usage notes

To ensure that your RDS instance runs as expected, some caches cannot be evicted. In extreme cases, the specified upper limit on caches cannot be fully met.

Procedure

The connection caches of ApsaraDB RDS for PostgreSQL consist of the following caches: relcache, syscache, and plancache. relcache is used to cache the metadata of user tables. syscache is used to cache the tuples of system tables. plancache is used to cache user-defined execution plans. ApsaraDB RDS for PostgreSQL allows you to adjust the numbers of objects that can be cached in relcache and syscache. The number of objects that can be cached in plancache cannot be limited.

Note

If you want to monitor the cache usage of each process, you must execute the CREATE EXTENSION rdsutils statement to install a dependent extension and set the value of the rds_enable_cache_monitor parameter to on.

  • If you set the rds_enable_cache_monitor parameter to on, the database performance deteriorates, and the execution duration of each SQL statement is increased by approximately 2 milliseconds.

  • You can modify the rds_enable_cache_monitor parameter only in the ApsaraDB RDS console. For more information, see Modify the parameters of an ApsaraDB RDS for PostgreSQL instance.

Modify the settings of the cache eviction feature for the current connection

-- Enable the eviction feature for relcache of the current connection to cache a maximum of 1,000 user tables.
SET rds_relcache_max_cached_relations = 1000;
-- Disable the eviction feature for relcache of the current connection.
SET rds_relcache_max_cached_relations = -1;


-- Enable the eviction feature for syscache of the current connection to cache the tuples of a maximum of 10,000 system tables.
set rds_syscache_max_cached_tuples = 10000;
-- Disable the eviction feature for syscache of the current connection.
set rds_syscache_max_cached_tuples = -1;

Configure the cache eviction feature for all connections

Log on to the ApsaraDB RDS console and configure the rds_relcache_max_cached_relations and rds_syscache_max_cached_tuples parameters. For more information, see Modify the parameters of an ApsaraDB RDS for PostgreSQL instance.

Parameters

Parameter

Description

Setting

rds_enable_cache_monitor

Specifies whether to monitor the cache usage of each process.

Valid values:

  • on: monitors the cache usage of each process.

  • off: does not monitor the cache usage of each process. This is the default value.

You need to configure this parameter based on your business requirements.

rds_relcache_max_cached_relations

The maximum number of user tables that can be cached for a single connection.

Valid values: [-1, INT_MAX].

Default value: -1, which indicates that the number is not limited.

  • You need to configure this parameter based on your business requirements. If a large number of tables are used for your workloads, you need to set this parameter to a large value.

  • If partitioned tables are used or the timescaledb extension is used, we recommend that you set this parameter to -1 or a large value.

  • If you set this parameter to a small value, the database performance may deteriorate. We recommend that you set this parameter to a value that is greater than or equal to 1000.

rds_syscache_max_cached_tuples

The maximum number of tuples of system tables that can be cached for a single connection.

Valid values: [-1, INT_MAX].

Default value: -1, which indicates that the number is not limited.

  • You need to configure this parameter based on your business requirements. If a large number of tables and fields are used for your workloads, you need to set this parameter to a large value.

  • If partitioned tables are used or the timescaledb extension is used, we recommend that you set this parameter to -1 or a large value.

  • If you set this parameter to a small value, the database performance may deteriorate. We recommend that you set this parameter to a value that is greater than or equal to 5000.