影响或控制PostgreSQL垃圾回收的参数或因素

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
云原生数据库 PolarDB 分布式版,标准版 2核8GB
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介:

标签

PostgreSQL , 垃圾回收 , 参数 , freeze


背景

哪些参数或因素将影响或控制PostgreSQL的垃圾回收呢?

参数

全局参数

1、控制VACUUM命令的睡眠时间,当vacuum的阶段性COST大于vacuum_cost_limit时,睡眠一段时间后继续。

vacuum_cost_delay = 0                   # 0-100 milliseconds  

2、VACUUM时,不同数据块(在SHARED BUFFER中命中的块、未命中的块、脏块)的成本

#vacuum_cost_page_hit = 1               # 0-10000 credits  
#vacuum_cost_page_miss = 10             # 0-10000 credits  
#vacuum_cost_page_dirty = 20            # 0-10000 credits  

3、VACUUM阶段性COST阈值

#vacuum_cost_limit = 200                # 1-10000 credits  

4、对只读事务设置的超时时间,防止LONG SQL带来的膨胀

#old_snapshot_threshold = -1            # 1min-60d; -1 disables; 0 is immediate  
                                        # (change requires restart)  

5、在CKPT后第一次产生或变更的脏块,是否记录整个数据块的内容到WAL中。

full_page_writes = off                  # recover from partial page writes  

6、是否开启WAL压缩

#wal_compression = off                  # enable compression of full-page writes  

7、自动垃圾回收相关进程

#------------------------------------------------------------------------------  
# AUTOVACUUM PARAMETERS  
#------------------------------------------------------------------------------  
  
是否开启自动垃圾回收  
  
#autovacuum = on                        # Enable autovacuum subprocess?  'on'  
                                        # requires track_counts to also be on.  
  
运行时长超过阈值的AUTO VACUUM将被记录下来  
  
#log_autovacuum_min_duration = -1       # -1 disables, 0 logs all actions and  
                                        # their durations, > 0 logs only  
                                        # actions running at least this number  
                                        # of milliseconds.  
  
最多允许多少个垃圾回收WORKER进程同时工作  
autovacuum_max_workers = 9              # max number of autovacuum subprocesses  
                                        # (change requires restart)  
  
轮询查询完所有数据库是否有需要进行垃圾回收的对象的周期。  
autovacuum_naptime = 1min         # time between autovacuum runs  
  
判断是否需要进行垃圾回收、收集统计信息的阈值1:最小被影响记录数  
#autovacuum_vacuum_threshold = 50       # min number of row updates before  
                                        # vacuum  
#autovacuum_analyze_threshold = 50      # min number of row updates before  
                                        # analyze  
  
  
判断是否需要进行垃圾回收、收集统计信息的阈值2:被影响记录数占比  
autovacuum_vacuum_scale_factor = 0.00002        # fraction of table size before vacuum  
autovacuum_analyze_scale_factor = 0.00001       # fraction of table size before analyze  
  
不管有没有开启autovacuum,当年龄达到这个阈值,都会强制触发freeze  
#autovacuum_freeze_max_age = 200000000  # maximum XID age before forced vacuum  
                                        # (change requires restart)  
#autovacuum_multixact_freeze_max_age = 400000000        # maximum multixact age  
                                        # before forced vacuum  
                                        # (change requires restart)  
  
自动垃圾回收的睡眠时间  
autovacuum_vacuum_cost_delay = 0        # default vacuum cost delay for  
                                        # autovacuum, in milliseconds;  
                                        # -1 means use vacuum_cost_delay  
  
自动垃圾回收睡眠前的COST阈值  
autovacuum_vacuum_cost_limit = 0        # default vacuum cost limit for  
                                        # autovacuum, -1 means use  
                                        # vacuum_cost_limit  

主库参数

是否延迟回收垃圾,可能导致膨胀,但是可以降低只读备库的查询冲突的可能性。但是可能导致主库频繁进行垃圾回收,并回收不掉。  
  
#vacuum_defer_cleanup_age = 0   # number of xacts by which cleanup is delayed  

备库参数

备库是否把事务快照发回给主库,主库不能回收备库运行中事务所需的脏数据,可能导致主库膨胀。甚至导致主库频繁进行垃圾回收,并回收不掉。  
  
#hot_standby_feedback = off             # send info from standby to prevent  
                                        # query conflicts  

客户端参数

执行vacuum或触发autovacuum时,年龄小于vacuum_freeze_min_age的记录,不会被FREEZE。  
  
当表的年龄大于vacuum_freeze_table_age时,VACUUM会扫描全表,并执行FREEZE。  
  
#vacuum_freeze_min_age = 50000000  
#vacuum_freeze_table_age = 150000000  
  
以下则针对multixact事务号的年龄  
  
#vacuum_multixact_freeze_min_age = 5000000  
#vacuum_multixact_freeze_table_age = 150000000  

表级参数

表的PAGE中,分配多少空间给INSERT、COPY时请求的空间,剩余的留给UPDATE,尽量实现HOT。  
       fillfactor (integer)  
           The fillfactor for a table is a percentage between 10 and 100.   
	   100 (complete packing) is the default.   
	   When a smaller fillfactor is specified,   
	   INSERT operations pack table pages only to the indicated percentage;   
	   the  
           remaining space on each page is reserved for updating rows on that page.   
	   This gives UPDATE a chance to place the updated copy of a row on the same page as the original,   
	   which is more efficient than placing it on a  
           different page. For a table whose entries are never updated,   
	   complete packing is the best choice,   
	   but in heavily updated tables smaller fillfactors are appropriate.   
	   This parameter cannot be set for TOAST tables.  
  
是否开启表的自动垃圾回收  
       autovacuum_enabled, toast.autovacuum_enabled (boolean)  
           Enables or disables the autovacuum daemon for a particular table.   
	   If true, the autovacuum daemon will perform automatic VACUUM and/or   
	   ANALYZE operations on this table following the rules discussed in   
	   Section 24.1.6. If  
           false, this table will not be autovacuumed, except to prevent   
	   transaction ID wraparound. See Section 24.1.5 for more about   
	   wraparound prevention. Note that the autovacuum daemon does not   
	   run at all (except to prevent  
           transaction ID wraparound) if the autovacuum parameter is false;   
	   setting individual tables' storage parameters does not override that.  
	   Therefore there is seldom much point in explicitly setting this storage parameter to  
           true, only to false.  
  
与全局参数类似功能  
       autovacuum_vacuum_threshold, toast.autovacuum_vacuum_threshold (integer)  
           Per-table value for autovacuum_vacuum_threshold parameter.  
  
       autovacuum_vacuum_scale_factor, toast.autovacuum_vacuum_scale_factor (float4)  
           Per-table value for autovacuum_vacuum_scale_factor parameter.  
  
       autovacuum_analyze_threshold (integer)  
           Per-table value for autovacuum_analyze_threshold parameter.  
  
       autovacuum_analyze_scale_factor (float4)  
           Per-table value for autovacuum_analyze_scale_factor parameter.  
  
       autovacuum_vacuum_cost_delay, toast.autovacuum_vacuum_cost_delay (integer)  
           Per-table value for autovacuum_vacuum_cost_delay parameter.  
  
       autovacuum_vacuum_cost_limit, toast.autovacuum_vacuum_cost_limit (integer)  
           Per-table value for autovacuum_vacuum_cost_limit parameter.  
  
       autovacuum_freeze_min_age, toast.autovacuum_freeze_min_age (integer)  
           Per-table value for vacuum_freeze_min_age parameter.   
	   Note that autovacuum will ignore per-table autovacuum_freeze_min_age   
	   parameters that are larger than half the system-wide autovacuum_freeze_max_age setting.  
  
       autovacuum_freeze_max_age, toast.autovacuum_freeze_max_age (integer)  
           Per-table value for autovacuum_freeze_max_age parameter.   
	   Note that autovacuum will ignore per-table autovacuum_freeze_max_age   
	   parameters that are larger than the system-wide setting (it can only be set smaller).  
  
       autovacuum_freeze_table_age, toast.autovacuum_freeze_table_age (integer)  
           Per-table value for vacuum_freeze_table_age parameter.  
  
       autovacuum_multixact_freeze_min_age, toast.autovacuum_multixact_freeze_min_age (integer)  
           Per-table value for vacuum_multixact_freeze_min_age parameter.   
	   Note that autovacuum will ignore per-table autovacuum_multixact_freeze_min_age   
	   parameters that are larger than half the system-wide  
           autovacuum_multixact_freeze_max_age setting.  
  
       autovacuum_multixact_freeze_max_age, toast.autovacuum_multixact_freeze_max_age (integer)  
           Per-table value for autovacuum_multixact_freeze_max_age parameter.   
	   Note that autovacuum will ignore per-table autovacuum_multixact_freeze_max_age   
	   parameters that are larger than the system-wide setting (it can only be  
           set smaller).  
  
       autovacuum_multixact_freeze_table_age, toast.autovacuum_multixact_freeze_table_age (integer)  
           Per-table value for vacuum_multixact_freeze_table_age parameter.  
  
       log_autovacuum_min_duration, toast.log_autovacuum_min_duration (integer)  
           Per-table value for log_autovacuum_min_duration parameter.  

因素

1、数据库不能回收LONG SQL事务快照后产生的垃圾,也即是说,数据库中最老的事务,决定了数据库可以回收的垃圾上限。在此事务快照后产生的垃圾都无法被回收。

2、snapshot too old,开启这个参数后,只读事务的允许时间超过这个时间后,将自动回滚。防止LONG SQL引起的膨胀。

3、备库开启了hot_standby_feedback = on时,备库如果运行LONG SQL,也会导致主库垃圾回收受限。导致膨胀。同时如果naptime很小,则可能导致频繁的无效VACUUM发生,导致数据库的IO,CPU飙高。

4、freeze是扫描全表的动作,如果大表发生freeze,可能导致大量的数据文件读写IO,以及WAL的写IO。通过wal日志分析,可以找到FREEZE的蛛丝马迹。

5、如果主库设置了vacuum_defer_cleanup_age 大于 0 ,可能导致膨胀,同时如果naptime很小,则可能导致频繁的无效VACUUM发生,导致数据库的IO,CPU飙高。

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
1月前
|
DataWorks Java 关系型数据库
DataWorks常见问题之将预警信息发送至邮箱
DataWorks是阿里云提供的一站式大数据开发与管理平台,支持数据集成、数据开发、数据治理等功能;在本汇总中,我们梳理了DataWorks产品在使用过程中经常遇到的问题及解答,以助用户在数据处理和分析工作中提高效率,降低难度。
35 1
|
5月前
|
缓存 关系型数据库 数据库
PostgreSQL技术大讲堂 - 第32讲:数据库参数调整
从零开始学PostgreSQL技术大讲堂 - 第32讲:数据库参数调整
446 2
|
4月前
|
SQL 关系型数据库 数据库
postgresql数据库修改参数的方式
在PostgreSQL数据库中,你可以通过多种方式修改数据库参数,以更改其行为。以下是一些常见的修改数据库参数的方式: 1. **通过配置文件修改(postgresql.conf):** PostgreSQL的配置文件是 `postgresql.conf`。你可以直接编辑该文件,找到要修改的参数,修改其值,然后重新启动PostgreSQL服务以使更改生效。 通常,`postgresql.conf` 文件位于 PostgreSQL 数据目录下。修改完毕后,确保重新启动 PostgreSQL 服务。 2. **使用 ALTER SYSTEM 命令:** PostgreSQL
102 1
|
2月前
|
关系型数据库 Java 分布式数据库
PolarDB for PostgreSQL参数问题之参数删除失败如何解决
PolarDB for PostgreSQL是基于PostgreSQL开发的一款云原生关系型数据库服务,它提供了高性能、高可用性和弹性扩展的特性;本合集将围绕PolarDB(pg)的部署、管理和优化提供指导,以及常见问题的排查和解决办法。
|
3月前
|
关系型数据库 PostgreSQL
PostgreSQL 的哪些参数不能通过ALTER SYSTEM SET 修改
在 PostgreSQL 中,有一些参数是不能通过 `ALTER SYSTEM SET` 语句进行动态修改的,这些参数通常需要在 PostgreSQL 的配置文件中进行手动修改。以下是一些不能通过 `ALTER SYSTEM SET` 修改的常见参数: 1. **track_activities** 2. **track_counts** 3. **track_io_timing** 4. **track_functions** 5. **track_activity_query_size** 6. **track_commit_timestamp** 7. **shared_preload
|
3月前
|
Java
jvm性能调优 - 17案例实战_每日上亿请求量的电商系统 老轻代垃圾回收参数如何优化
jvm性能调优 - 17案例实战_每日上亿请求量的电商系统 老轻代垃圾回收参数如何优化
72 0
|
3月前
|
缓存 Java 双11
jvm性能调优 - 16案例实战_每日上亿请求量的电商系统 年轻代垃圾回收参数如何优化
jvm性能调优 - 16案例实战_每日上亿请求量的电商系统 年轻代垃圾回收参数如何优化
45 0
|
8月前
|
缓存 算法 Java
透彻理解JVM中垃圾回收GC生产参数,停顿时间+执行效率相关参数
停顿时间相关参数 部分垃圾回收器实现了GC执行时应用最大停顿时间的功能,所以提供参数用于应用控制停顿时间。另外,GC为了满足停顿时间,会设计和实现一些动态算法来调整堆空间,从而满足停顿时间这个目标。本节介绍相关参数。 该参数表示GC的最大的停顿时间。不同GC对于该参数的行为不一致,具体来说: 1)若Parallel GC中GC执行的时间超过该值,将导致调整新生代和老生代的大小(参数UseAdaptiveSizePolicy设置为true)。参数的默认值为4294 967 295,大约为50天(所以通常不会触发这个调整策略)。 2)若G1中GC执行的时间超过该值,将导致调整新生代的大小和
|
8月前
|
缓存 安全 Java
JVM中垃圾回收相关参数介绍:大页和NUMA参数+GC日志相关参数
大页和NUMA参数 本节介绍JVM为使用OS而提供的大页和NUMA特性相关的参数。 该参数控制JVM向OS请求内存时使用大页的粒度。使用该参数时需要对OS进行配置,只有OS允许时才能真正启动。参数的默认值与平台相关,一般为false。 在允许使用大页方式向OS请求内存时,如果堆空间小于该阈值,则强制禁止大页使用。该参数的默认值为128MB。 在允许使用大页方式向OS请求内存时,优先在本地节点进行分配。该参数仅适用于Windows系统。 在允许使用大页方式向OS请求内存时,如果OS提供了多种大页的设置,可通过该参数选择其中的大页设置。参数的默认值为0,表示使用OS默认的大页设置。
104 0
|
9月前
|
关系型数据库 Java 数据库连接
PostgreSQL 14中连接参数target_session_attrs增强
PostgreSQL 14中连接参数target_session_attrs增强
98 0

相关产品

  • 云原生数据库 PolarDB