Measuring PostgreSQL Checkpoint Statistics
Checkpoints can be a major drag on write-heavy PostgreSQL installations. The first step toward identifying issues in this area is to monitor how often they happen, which just got an easier to use interface added to the database recently.
Checkpoints are periodic maintenance operations the database performs to make sure that everything it’s been caching in memory has been synchronized with the disk. The idea is that once you’ve finished one, you can eliminate needing to worry about older entries placed into the write-ahead log of the database. That means less time to recover after a crash.
The problem with checkpoints is that they can be very intensive, because to complete one requires writing every single bit of changed data in the database’s buffer cache out to disk. There were a number of features added to PostgreSQL 8.3 that allow you to better monitor the checkpoint overhead, and to lower it by spreading the activity over a longer period of time. I wrote a long article about those changes called Checkpoints and the Background Writer that goes over what changed, but it’s pretty dry reading.
What you probably want to know is how to monitor checkpoints on your production system, and how to tell if they’re happening too often. Even though things have improved, “checkpoint spikes” where disk I/O becomes really heavy are still possible even in current PostgreSQL versions. And it doesn’t help that the default configuration is tuned for very low disk space and fast crash recovery rather than performance. The checkpoint_segments parameter that’s one input on how often a checkpoint happens defaults to 3, which forces a checkpoint after only 48MB of writes.
You can find out checkpoint frequency two ways. You can turn on log_checkpoints and watch what happens in the logs. You can also use the pg_stat_bgwriter view, which gives a count of each of the two sources for checkpoints (time passing and writes occurring) as well as statistics about how much work they did.
The main problem with making that easier to do is that until recently, it’s been impossible to reset the counters inside of pg_stat_bgwriter. That means you have to take a snapshot with a timestamp on it, wait a while, take another snapshot, then subtract all the values to derive any useful statistics from the data. That’s a pain.
Enough of a pain that I wrote a patch to make it easier. With the current development version of the database, you can now call pg_stat_reset_shared(‘bgwriter’) and pop all these values back to 0 again. This allows following a practice that used to be common on PostgreSQL. Before 8.3, there was a parameter named stats_reset_on_server_start you could turn on. That reset all of the server’s internal statistics each time you started it. That meant that you could call the handy pg_postmaster_start_time() function, compare with the current time, and always have an accurate count in terms of operations/second of any statistic available on the system.
It’s still not automatic, but now that resetting these shared pieces is possible you can do it yourself. The first key is to integrate statistics clearing into your server startup sequence. A script like this will work:
- pg_ctl start -l $PGLOG -w
- psql -c "select pg_stat_reset();"
- psql -c "select pg_stat_reset_shared('bgwriter');"
Note the “-w” on the start command there–that will make pg_ctl wait until the server is finished starting before it returns, which is vital if you want to immediately execute a statement against it.
If you’ve done that, and your server start time is essentially the same as when the background writer stats started collection, you can now use this fun query:
- SELECT
- total_checkpoints,
- seconds_since_start / total_checkpoints / 60 AS minutes_between_checkpoints
- FROM
- (SELECT
- EXTRACT(EPOCH FROM (now() - pg_postmaster_start_time())) AS seconds_since_start,
- (checkpoints_timed+checkpoints_req) AS total_checkpoints
- FROM pg_stat_bgwriter
- ) AS sub;
And get a simple report of exactly how often checkpoints are happening on your system. The output looks like this:
- total_checkpoints | 9
- minutes_between_checkpoints | 3.82999310740741
What you do with this information is stare at the average time interval and see if it seems too fast. Normally, you’d want a checkpoint to happen no more than every five minutes, and on a busy system you might need to push it to ten minutes or more to have a hope of keeping up. With this example, every 3.8 minutes is probably too fast–this is a system that needs checkpoint_segments to be higher.
Using this technique to measure the checkpoint interval lets you know if you need to increase the checkpoint_segments and checkpoint_timeout parameters in order to achieve that goal. You can compute the numbers manually right now, and once 9.0 ships it’s something you can consider making completely automatic–so long as you don’t mind your stats going away each time the server restarts.
There are some other interesting ways to analyze the data the background writer provides for you in pg_stat_bgwriter, but I’m not going to give away all of my tricks today.
注:
1、上面给了一个查询checkpoint执行时间长度的sql,当然在计算之前要清掉历史记录。直接运行select pg_stat_reset()是清不掉的,需要执行select pg_stat_reset_shared('bgwriter'),这可以将视图pg_stat_bgwriter中的各值除掉stats_reset均置为0;
2、视图pg_stat_bgwriter 字段:
- swrd=# \d pg_stat_bgwriter
- View "pg_catalog.pg_stat_bgwriter"
- Column | Type | Modifiers
- -----------------------+--------------------------+-----------
- checkpoints_timed | bigint |
- checkpoints_req | bigint |
- checkpoint_write_time | double precision |
- checkpoint_sync_time | double precision |
- buffers_checkpoint | bigint |
- buffers_clean | bigint |
- maxwritten_clean | bigint |
- buffers_backend | bigint |
- buffers_backend_fsync | bigint |
- buffers_alloc | bigint |
- stats_reset | timestamp with time zone |
其中checkpoints_timed表示由于checkpoint_timeout 引起的checkpoint的次数,checkpoints_req表示由于checkpoint_segments引起的checkpoint的次数。手动执行checkpoint命令,会将次数计算到checkpoints_req字段中,根据这两个的大小情况,可以来决定修改checkpoint_timeout 和checkpoint_segments值的大小。
两字段数值相加就是总的checkpoint数,可以结合buffers_checkpoint值计算出平均每次checkpoint的buffer大小。
3、计算checkpoint时间的sql:
- SELECT
- total_checkpoints,
- seconds_since_start / total_checkpoints / 60 AS minutes_between_checkpoints
- FROM
- (SELECT
- EXTRACT(EPOCH FROM (now() - pg_postmaster_start_time())) AS seconds_since_start,
- (checkpoints_timed+checkpoints_req) AS total_checkpoints
- FROM pg_stat_bgwriter
- ) AS sub;
EPOCH:
The Unix epoch (or Unix time or POSIX time or Unix timestamp ) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1-1-1970), but 'epoch' is often used as a synonym for 'Unix time'. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).
EXTRACT:
EXTRACT(field FROM source)
The extract
function retrieves subfields such as year or hour from date/time values. source must be a value expression of type timestamp, time, or interval. (Expressions of type date are cast to timestamp and can therefore be used as well.) field is an identifier or string that selects what field to extract from the source value. The extract
function returns values of type double precision.
参考:
http://blog.2ndquadrant.com/measuring_postgresql_checkpoin/
http://www.westnet.com/~gsmith/content/postgresql/chkp-bgw-83.htm
http://yao.iteye.com/blog/628941
http://www.postgresql.org/docs/9.4/static/functions-datetime.html
Measuring PostgreSQL Checkpoint Statistics的更多相关文章
- PostgreSQL CheckPoint设置(转)
今天在研究checkpoint process的问题时,顺便复习了一下checkpoint设置问题,又有新的疑惑了. checkpoint又名检查点,在oracle中checkpoint的发生意味着之 ...
- Flink - Checkpoint
Flink在流上最大的特点,就是引入全局snapshot, CheckpointCoordinator 做snapshot的核心组件为, CheckpointCoordinator /** * T ...
- PostgreSQL 9.3 Streaming Replication 状态监控
postgresql是使用Streaming Replication来实现热备份的,热备份的作用如下: 灾难恢复 高可用性 负载均衡,当你使用Streaming Replication来实现热备份(h ...
- Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关
在 Apache ShardingSphere 5.1.1 发布后,ShardingSphere 合并了来自全球的团队或个人的累计 1028 个 PR,为大家带来 5.1.2 新版本.该版本在功能.性 ...
- PatentTips - Systems, methods, and devices for dynamic resource monitoring and allocation in a cluster system
BACKGROUND 1. Field The embodiments of the disclosure generally relate to computer clusters, and m ...
- oracle_fdw安装及使用(无法访问oracle存储过程等对象)
通过oracle_fdw可以访问oracle中的一些表和视图,也可以进行修改,尤其是给比较复杂的系统使用非常方便. (但不能使用oracle_fdw来访问oracle的存储过程.包.函数.序列等对象) ...
- PostgreSQL的Checkpoint 发生的时机
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面:PostgreSQL基础知识与基本操作索引页 回到顶级页面:PostgreSQL索引页 官方说明来自: http://www.postg ...
- PostgreSQL的checkpoint能否并行
对于此问题,在社区进行了提问,并得到了一些大牛的解答: http://postgresql.1045698.n5.nabble.com/Can-checkpoint-creation-be-paral ...
- 在PostgreSQL中CREATE STATISTICS
如果你用Postgres做了一些性能调优,你可能用过EXPLAIN.EXPLAIN向你展示了PostgreSQL计划器为所提供的语句生成的执行计划,它显示了语句所引用的表如何被扫描(使用顺序扫描.索引 ...
随机推荐
- php中ckeditor(Fckeditor)的配置方法
ckeditor 编辑器php正确配置方法 1. 下载安装 CKEditor: http://ckeditor.com/ 解压下载到的CKEditor放到网站的路径中即可 2. 下载安装 CKFind ...
- hdu 2061
PS: 以为找个简单来恢复信心..结果碰到那么傻逼的题目... 题意:给出学分和成绩,算GPA...关键是注意换行....它要求的换行我觉得超级奇怪...除了第一个正常,其他的输入完之后先一个换行. ...
- 搭建EJB3开发环境
开发工具:myeclipse8.5.antjdk:1.5容器:jboss4.2.31.安装jboss:解压.配置JBOSS_HOME环境变量2.测试安装是否成功:启动%JBOSS_HOME%\bin\ ...
- Cookies和Session的区别
原文:http://www.cnblogs.com/lijihong/p/4743818.html 今天主要学习了Cookies和Session,网络上关于这方面的知识可谓很多,让人眼花缭乱,在此作一 ...
- iOS开发之修改动画对象的元素属性
步骤:1.使用single view application创建新的项目 2.在.h文件中使用UIimageview创建两个图片实例对象,使用UIDynamicAnimator创建动画对象 3.在.m ...
- KochSnow曲线
在这里实现了Koch曲线,而且提到我们只需要对一个等边三角形的各条边按照Koch曲线的算法进行绘图就能得到KochSnow曲线,将其实现到之前提到的绘图框架中,考虑到KochSnow的实现主要依赖Ko ...
- Java容器类接口的选择
我们知道Java容器类实际提供了四类接口:Map,List,Set和Queue,如下图所示,每种接口都有不止一个版本的实现,如果在实际编写程序时需要使用某种接口时该如何选择. 从Oracle的Java ...
- Java容器类概述
1.简介 容器是一种在一个单元里处理一组复杂元素的对象.使用集合框架理论上能够减少编程工作量,提高程序的速度和质量,毕竟类库帮我们实现的集合在一定程度上时最优的.在Java中通过java.util为用 ...
- 51 nod 机器人走方格
从一个长方形的方格的右上角 走到 左下角 , 问一共有多少种不同的路线可以达到 . #include<stdio.h> #include<string.h> #include& ...
- OD调试4--绕过nag窗口
先看一下程序的运行情况 先跳出了一个nag窗口 点确定 又跳出了一个NAG窗口,这是一些程序编写的时候常用的方法,设法让你购买正版软件, 于是今天呢,学会了四种绕过NAG的方法 我们先用OD加载进入这 ...