PostgreSQL学徒

PostgreSQL 术语简明指南

Word count: 1kReading time: 4 min
2023/12/26
loading

前言

今天晚上在整理素材的时候,Devrim Gündüz 的 Know the less known about PostgreSQL 议题使我来了兴趣,毕竟了解术语也是掌握PostgreSQL的方式之一。内核中有很多晦涩的术语,什么”ring buffer”、”buffer ring”、”tuple”、”row”等等,不说刚入门的小白,老油条看到这些术语也会云里雾里。本文简单聊聊 PostgreSQL 中常见的术语。

You are new-ish, or not used to some of the terms

Glossary

前面作者介绍了什么是 MVCC,相信各位都已十分了解了,在此表过不提。

xact

xact:”Transaction”,表示事务,在PostgreSQL中,事务状态信息存储在pg_xact中,在10以前的版本,其命名为pg_clog,由于其以”log”命名,误使很多新手认为它是”日志”,一旦删除,只能自求多福了。

txid

txid:”Transaction ID”,表示事务ID,其上限是32位无符号整型。对于写入操作,PostgreSQL会分配永久性的事务ID,而对于只读事务,只会分配虚拟事务ID,用于节省事务ID的资源

这里需要提一点的事,对于查询操作,并不会分配真正的事务ID,因此我们在做实验的时候,正确的姿势是使用txid_current_if_assigned,因为txid_current会真正消耗一个永久事务ID。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
postgres=# create table test(id int);
CREATE TABLE
postgres=# begin;
BEGIN
postgres=*# select txid_current_if_assigned();
txid_current_if_assigned
--------------------------

(1 row)

postgres=*# insert into test values(1);
INSERT 0 1
postgres=*# select txid_current_if_assigned();
txid_current_if_assigned
--------------------------
758
(1 row)

ctid

The physical location of the row version within its table,表示数据位于哪个位置,block number + offset

作者也提到,不要依赖这个值,在update、vacuum full的时候都会发生改变

xmin xmax

每一行上都有许多隐藏的系统列,用于判断可见性、获取行的状态信息等。

xmin 和 xmax 分别代表插入某一行的事务 ID、删除或更新某一行的事务 ID

不过要注意的是,并不是xmax有值,就表示一定被删除或更新了,比如删除的事务回滚了,其次行级锁也是通过xmax+infomask来实现的

cmin cmax

与xmin和xmax相对,不过是用于控制同一个事务内的可见性判断,典型场景就是游标。

datfrozenxid

All transaction IDs before this one have been replaced with a permanent transaction ID in this database,表示该数据库中最老的已冻结的事务 ID,是 pg_class.relfrozenxid 中的最小值。

multixact

Used to support row locking by multiple transactions,之前我也写文章详细讲过其原理 深度剖析 MultiXactID,将一组事务 ID 记录到一个 MultiXactID 中,进行映射。

MultiXactID 也是 32 位,所以也有类似的参数用于控制冻结

relfrozenxid

和 datfronzenxid 类似,All transaction IDs before this one have been replaced with a permanent (“frozen”) transaction ID in this table,所有小于该事务 ID 的元组都被冻结

WAL

Write Ahead Log,各位应该十分熟悉了,预写日志,用在 PITR/复制/备份恢复等等,对于 unlogged table 和 temp table,不会记录 REDO

WAL 细分为很多个资源管理器

LSN

Log Sequence Number,逻辑序列号,全局唯一标识

LSN 检查仅存在于共享缓冲区管理器中,不存在于临时表使用的本地缓冲区管理器中,因此对临时表的操作不能被 WAL 记录。

注意倒数第二句,比较页面的 LSN 和 WAL 的 LSN,以此达到”幂等”的伪效果。

隐藏参数

作者还提及了许多隐藏参数,默认情况下,postgresql.conf 文件中不包含,主要给开发者使用。

  • allow_system_table_mods (boolean)
  • ignore_checksum_failure (boolean)
  • zero_damaged_pages (boolean)
  • ignore_invalid_pages (boolean)
  • ignore_system_indexes (boolean)
  • post_auth_delay (integer)
  • pre_auth_delay (integer)
  • wal_consistency_checking (string)
  • wal_debug (boolean)
  • backtrace_functions (string)
  • debug_deadlocks (boolean)
  • log_btree_build_stats (boolean)
  • trace_notify (boolean)
  • trace_recovery_messages (enum)
  • trace_sort (boolean)
  • trace_locks (boolean)
  • trace_lwlocks (boolean)
  • trace_userlocks (boolean)
  • trace_lock_oidmin (integer)
  • trace_lock_table (integer)
  • jit_debugging_support (boolean)
  • jit_dump_bitcode (boolean)
  • jit_expressions (boolean)
  • jit_profiling_support (boolean)
  • jit_tuple_deforming (boolean)

更多信息可以参考 https://www.postgresql.org/docs/current/runtime-config-developer.html

只读参数

  • data_checksums (boolean):Initdb , off by default
  • block_size (integer):
  • debug_assertions (boolean):off
  • max_function_args (integer):最大函数参数,100
  • max_identifier_length (integer):最大标识符长度,63 字节
  • max_index_keys (integer):最大索引键,32
  • segment_size (integer)
  • server_encoding (string):initdb, UTF-8
  • server_version (string)
  • server_version_num (integer):150002,160000,比如 120007 代表 12.7 的版本。

参考

Know the less known: A PostgreSQL Glossary

https://www.postgresql.org/docs/current/runtime-config-developer.html

CATALOG
  1. 1. 前言
  2. 2. Glossary
    1. 2.1. xact
    2. 2.2. txid
    3. 2.3. ctid
    4. 2.4. xmin xmax
    5. 2.5. cmin cmax
    6. 2.6. datfrozenxid
    7. 2.7. multixact
    8. 2.8. relfrozenxid
    9. 2.9. WAL
    10. 2.10. LSN
    11. 2.11. 隐藏参数
    12. 2.12. 只读参数
    13. 2.13. 参考