• PostgreSQL PG15 新功能 PG_WALINSPECT


    fbb4f2c43b72e42dcdeed89ec4672b02.png

    开头还是介绍一下群,如果感兴趣PolarDB ,MongoDB ,MySQL ,PostgreSQL ,Redis ,Oracle ,Oceanbase 等有问题,有需求都可以加群群内有各大数据库行业大咖,CTO,可以解决你的问题。加群请加微信号 liuaustin3 (共1250人左右 1 + 2 + 3 +4)新人会进入3群(即将关闭自由申请)  默认会进入4群 

    每天感悟

    公平很多人一辈子追求,期望,奢望,可惜了,公平只存在于公众可以看到的地方,然而实际上你生活的世界和非洲大草原上的生存的原理本质是相同的。

    PostgreSQL 在PG15 版本之前如果想了解wal 日志中的信息,只能使用上期提到的工具去查看,但从PG15这个版本,查看 wal 日志的内容的方式变化了可以在数据库内部进行查看。作者名为 Bharath Rupireddy

    pg_walinspect 这个模块提供了SQL 方面的函数允许你可以探究 write-ahead log 里面的内容,在一个正在运行的PostgreSQL数据库中,实际上功能和我们熟知的pg_waldump功能是类似的,但是在数据内部运行比一个外部的命令给DB人员的在一些情况下,可操作性性要更高。

    CREATE EXTENSION pg_walinspect;

    这里pg_walinspect函数在PG16 有增强,添加了如下的函数

    1. function pg_get_wal_block_info() added (commit c31cf1c0, initially as pg_get_wal_fpi_info() but renamed and expanded in commit 9ecb134a)
    2. functions pg_get_wal_records_info(), pg_get_wal_stats() and pg_get_wal_block_info() accept an LSN value higher than the current LSN (commit 5c1b6628)
    3. functions pg_get_wal_records_info_till_end_of_wal() and pg_get_wal_stats_till_end_of_wal() removed (commit 5c1b6628)

    我们通过下面的实验来快速了解pg_walinspect的工作,

    1. postgres=# select now(),pg_current_wal_lsn();
    2. elect now(),pg_current_wal_lsn();
    3.              now              | pg_current_wal_lsn 
    4. ------------------------------+--------------------
    5.  2023-08-11 08:08:16.79274-04 | 0/4552810
    6. (1 row)
    7. postgres=# 
    8. postgres=# 
    9. postgres=# create database test;
    10. CREATE DATABASE
    11. postgres=# create table  test (id int primary key, name varchar(200));
    12. CREATE TABLE
    13. postgres=# insert into test (id,name) values (1,'Austin');
    14. INSERT 0 1
    15. postgres=# insert into test (id,name) values (2,'Simon');
    16. INSERT 0 1
    17. postgres=# 
    18. postgres=# 
    19. postgres=# create index idx_test on test (name);
    20. CREATE INDEX
    21. postgres=# 
    22. postgres=# select now(),pg_current_wal_lsn();
    23.               now              | pg_current_wal_lsn 
    24. -------------------------------+--------------------
    25.  2023-08-11 08:08:16.896122-04 | 0/498AE38
    26. (1 row)

    这里我们在操作前获得事务的LSN号,同时在任务结束后,获得结束后的事务号,方便后面我们演示。

    首先我们先用第一个函数 pg_get_wal_records() 通过这个函数可以查看系统中的一段日志的内容

    c1617f80c7883ab97e08043169d8d721.png

    1. postgres=# select count(*) from pg_get_wal_records_info('0/4552810''0/498AE38');
    2.  count 
    3. -------
    4.   1318
    5. (1 row)

    这一段产生1318 个记录。

    9bff01b12d4f2e60a43ae7f257cdaa30.png

    1. postgres=# select count(*) 
    2. postgres-# from pg_get_wal_records_info('0/4552810''0/498AE38') where record_type <> 'FPI';
    3. -[ RECORD 1 ]
    4. count | 394

    而我们排除了FPI 的日志信息后,我们剩下的日志信息只有394 ,实际上其他的日志信息只占整体日志信息的29%,

    通过这样的方法可以PG_WAL中的日志大部分信息是不是 FULL PAGE 

    97307fd800e467a8dd75bbdbadec7273.png

    实际上在这段f8f8cec4d4044dd15768da467169bbcf.png

    实际上在这段里面日志里面我们根据resource_manager 来区分记录的类型,这里主要有 storage , database , btree ,heap , Transaction , heap2, Relmap, Standby , xlog 等,同时记录的类型,有以下集中

    1. postgres=# select distinct record_type from pg_get_wal_records_info('0/4552810''0/498AE38');
    2. -[ RECORD 1 ]---------------
    3. record_type | INSERT
    4. -[ RECORD 2 ]---------------
    5. record_type | NEWROOT
    6. -[ RECORD 3 ]---------------
    7. record_type | CREATE_WAL_LOG
    8. -[ RECORD 4 ]---------------
    9. record_type | MULTI_INSERT
    10. -[ RECORD 5 ]---------------
    11. record_type | INPLACE
    12. -[ RECORD 6 ]---------------
    13. record_type | UPDATE
    14. -[ RECORD 7 ]---------------
    15. record_type | FPI
    16. -[ RECORD 8 ]---------------
    17. record_type | LOCK
    18. -[ RECORD 9 ]---------------
    19. record_type | CREATE
    20. -[ RECORD 10 ]--------------
    21. record_type | RUNNING_XACTS
    22. -[ RECORD 11 ]--------------
    23. record_type | COMMIT
    24. -[ RECORD 12 ]--------------
    25. record_type | INSERT+INIT
    26. -[ RECORD 13 ]--------------
    27. record_type | INSERT_LEAF
    1. postgres=# SELECT * FROM pg_get_wal_stats('0/4552810''0/498AE38');
    2. -[ RECORD 1 ]----------------+----------------------
    3. resource_manager/record_type | XLOG
    4. count                        | 924
    5. count_percentage             | 70.10622154779969
    6. record_size                  | 45276
    7. record_size_percentage       | 67.49552772808586
    8. fpi_size                     | 4216068
    9. fpi_size_percentage          | 97.18706086725605
    10. combined_size                | 4261344
    11. combined_size_percentage     | 96.73493181657214
    12. -[ RECORD 2 ]----------------+----------------------
    13. resource_manager/record_type | Transaction
    14. count                        | 5
    15. count_percentage             | 0.37936267071320184
    16. record_size                  | 1085
    17. record_size_percentage       | 1.6174716756112104
    18. fpi_size                     | 0
    19. fpi_size_percentage          | 0
    20. combined_size                | 1085
    21. combined_size_percentage     | 0.02463011693516899
    22. -[ RECORD 3 ]----------------+----------------------
    23. resource_manager/record_type | Storage
    24. count                        | 299
    25. count_percentage             | 22.685887708649467
    26. record_size                  | 12558
    27. record_size_percentage       | 18.72093023255814
    28. fpi_size                     | 0
    29. fpi_size_percentage          | 0
    30. combined_size                | 12558
    31. combined_size_percentage     | 0.2850737405270527
    32. -[ RECORD 4 ]----------------+----------------------
    33. resource_manager/record_type | CLOG
    34. count                        | 0
    35. count_percentage             | 0
    36. record_size                  | 0
    37. record_size_percentage       | 0
    38. fpi_size                     | 0
    39. fpi_size_percentage          | 0
    40. combined_size                | 0
    41. combined_size_percentage     | 0
    42. -[ RECORD 5 ]----------------+----------------------
    43. resource_manager/record_type | Database
    44. count                        | 1
    45. count_percentage             | 0.07587253414264036
    46. record_size                  | 34
    47. record_size_percentage       | 0.05068574836016696
    48. fpi_size                     | 0
    49. fpi_size_percentage          | 0
    50. combined_size                | 34
    51. combined_size_percentage     | 0.0007718193325306412
    52. -[ RECORD 6 ]----------------+----------------------
    53. resource_manager/record_type | Tablespace
    54. count                        | 0
    55. count_percentage             | 0
    56. record_size                  | 0
    57. record_size_percentage       | 0
    58. fpi_size                     | 0
    59. fpi_size_percentage          | 0
    60. combined_size                | 0
    61. combined_size_percentage     | 0
    62. -[ RECORD 7 ]----------------+----------------------
    63. resource_manager/record_type | MultiXact
    64. count                        | 0
    65. count_percentage             | 0
    66. record_size                  | 0
    67. record_size_percentage       | 0
    68. fpi_size                     | 0
    69. fpi_size_percentage          | 0
    70. combined_size                | 0
    71. combined_size_percentage     | 0
    72. -[ RECORD 8 ]----------------+----------------------
    73. resource_manager/record_type | RelMap
    74. count                        | 1
    75. count_percentage             | 0.07587253414264036
    76. record_size                  | 553
    77. record_size_percentage       | 0.8243887895050686
    78. fpi_size                     | 0
    79. Cancel request sent

    通过这个功能的另一个函数 pg_get_wal_stats ,可以通过这个功能完全,了解这一段实际的日志中的日志的占比,我们可以看到FPI  , XLOG 里面FPI 占比70%。

    select * from pg_get_wal_stats('0/4552810''0/498AE38'true) order by count_percentage desc;

    8c5b29ca3f0ab9add2fe0692dd7afaa5.png

    最后为什么会产生那么多FPI, full page image, 主要有以下的原因, 数据库页面记录在wal日志中的原因,FPI 记录包含整个页面的内容,包括数据和元数据信息,每一个被修改的页面均会产生一个FPI记录,这些FPI记录会写到WAL 日志中,当进行事务性操作是,会对事务牵扯的需要操作的多个页面进行操作被修改的页面都需要有对应的FPI 记录,所以WAL日志中占据最大存储量的是FPI 。换言之,你的系统做的数据变动越多,牵扯的页面数量越多,则产生的FPI 会越多,最终就是你的WAL 日志会较大。

    当然如果你想获得更多关于数据库操作的一些内部构造知识,可以通过下面的方式来初步获取,比如日志中一段时间,频繁操作OID,你可以把OID 放到下面的SQL中,来查看到底在这段时间,系统操作了什么。

    1. SELECT
    2.  relname,
    3.  CASE 
    4.   when relkind = 'r' then 'tab'
    5.   when relkind = 'i' then 'idx'
    6.   when relkind = 'S' then 'seq'
    7.   when relkind = 't' then 'toast'
    8.   when relkind = 'v' then 'view'
    9.   when relkind = 'm' then 'matview'
    10.   when relkind = 'c' then 'composite'
    11.   when relkind = 'f' then 'F tab'
    12.   when relkind = 'p' then 'part tab'
    13.   when relkind = 'I' then 'part idx'
    14.  END as object_type
    15.    FROM
    16.   pg_class
    17.  WHERE
    18.   oid IN ('oid');
    1. select * from pg_get_wal_stats('0/4552810''0/498AE38'true) order by count_percentage desc;
    2. postgres-# 
    3. oid                  relfilenode          relhassubclass       relkind              reloftype            relpersistence       reltoastrelid
    4. relacl               relforcerowsecurity  relhastriggers       relminmxid           reloptions           relreplident         reltuples
    5. relallvisible        relfrozenxid         relispartition       relname              relowner             relrewrite           reltype
    6. relam                relhasindex          relispopulated       relnamespace         relpages             relrowsecurity       
    7. relchecks            relhasrules          relisshared          relnatts             relpartbound         reltablespace        
    8. postgres-# oid IN ('1663','16394','2619','1247');
    9. -[ RECORD 1 ]-------------
    10. relname     | pg_statistic
    11. object_type | tab
    12. -[ RECORD 2 ]-------------
    13. relname     | pg_type
    14. object_type | tab
    15. postgres-# 
    16. oid                  relfilenode          relhassubclass       relkind              reloftype            relpersistence       reltoastrelid
    17. relacl               relforcerowsecurity  relhastriggers       relminmxid           reloptions           relreplident         reltuples
    18. relallvisible        relfrozenxid         relispartition       relname              relowner             relrewrite           reltype
    19. relam                relhasindex          relispopulated       relnamespace         relpages             relrowsecurity       
    20. relchecks            relhasrules          relisshared          relnatts             relpartbound         reltablespace        
    21. postgres-# oid IN ('1663','16394','2619','1247');
    22. -[ RECORD 1 ]-------------
    23. relname     | pg_statistic
    24. object_type | tab
    25. -[ RECORD 2 ]-------------
    26. relname     | pg_type
    27. object_type | tab

    52d1c11f7f13f24c6b32fdc04951201f.png

  • 相关阅读:
    leetcode127单词接龙刷题打卡
    Android 富文本SpannableString
    BroadcastReciver 和LocalBroadcastManager区别
    ASON 技术简介
    UE4 粒子特效基础学习 (02-光晕材质制作)
    论文:DeepAR: Probabilistic Forecasting with Autoregressive Recurrent Networks
    机器人中的数值优化(十)——线性共轭梯度法
    flutter开发实战-TweenSequence实现动画序列
    快速了解服务器单CPU与双CPU
    【EMC专题】为什么说产品的EMC性能是设计赋予的?
  • 原文地址:https://blog.csdn.net/liuhuayang/article/details/132727244