postgresql 数据库表隐藏列 oid、tableoid、ctid、xmin、xmax、cmin、cmax
生活随笔
收集整理的這篇文章主要介紹了
postgresql 数据库表隐藏列 oid、tableoid、ctid、xmin、xmax、cmin、cmax
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
os: centos 7.4
db: postgresql 10.11
oid、tableoid、ctid、xmin、xmax、cmin、cmax 這些都是 postgresql 數據庫表的隱藏列.
起著不同的作用.
版本
# cat /etc/centos-release CentOS Linux release 7.4.1708 (Core) # # # yum list installed |grep -i postgresql postgresql10.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-contrib.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-debuginfo.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-devel.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-docs.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-libs.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-odbc.x86_64 12.00.0000-1PGDG.rhel7 @pgdg10 postgresql10-plperl.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-plpython.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-pltcl.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-server.x86_64 10.11-2PGDG.rhel7 @pgdg10 postgresql10-tcl.x86_64 2.4.0-1.rhel7 @pgdg10 postgresql10-tcl-debuginfo.x86_64 2.3.1-1.rhel7 @pgdg10 postgresql10-test.x86_64 10.11-2PGDG.rhel7 @pgdg10 # su - postgres Last login: Wed Jan 15 18:34:12 CST 2020 on pts/0 $ $ $ psql -c "select version();"version ----------------------------------------------------------------------------------------------------------PostgreSQL 10.11 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit (1 row)oid、tableoid、ctid
oid 為內部行對象標識符,強烈不建議(或者禁止)用戶表使用oid。用戶默認建表時 with oids=false
tableoid 行歸屬的表的oid,通常在有繼承關系的父表時,可以快速判斷行數據所屬的子表。
ctid 標記行的物理位置,格式為 (m,n) 其中 m表示塊號,n表示在塊內的行號
xmin、xmax、cmin、cmax
多版本實現中控制數據行的可見性
插入行時:xmin 置為當前事務ID,xmax 置為 0
刪除行時:xmax 置為當前事務ID
更新行時:舊行上的 xmin 保持不變,舊行上的 xmax 置為當前事務ID
新行上的 xmin 置為當前事務ID,新行上的 xmax 置為 0
cmin、cmax 主要是用于判斷同一個事務內的不同命令導致的行版本變化是否可見。這個理解透徹后再補充下
create table 試一下
# su - postgres $ psqlpostgres=# create table tmp_t0 (id bigint primary key,name varchar(100) );postgres=# select oid,relname from pg_class where relname='tmp_t0';oid | relname --------+---------379767 | tmp_t0 (1 row)postgres=# select attrelid,attname,attnum from pg_attribute where attrelid=379767;attrelid | attname | attnum ----------+----------+--------379767 | tableoid | -7379767 | cmax | -6379767 | xmax | -5379767 | cmin | -4379767 | xmin | -3379767 | ctid | -1379767 | id | 1379767 | name | 2 (8 rows) postgres=# insert into tmp_t0 select 1,'a' ;postgres=# select * from tmp_t0;id | name ----+------1 | a (1 row)postgres=# select tableoid,cmax,xmax,cmin,xmin,ctid,id,name from tmp_t0;tableoid | cmax | xmax | cmin | xmin | ctid | id | name ----------+------+------+------+--------+-------+----+------379767 | 0 | 0 | 0 | 699844 | (0,1) | 1 | a (1 row)執行 update
postgres=# begin;postgres=# update tmp_t0 set name='b' where id=1;postgres=# select tableoid,cmax,xmax,cmin,xmin,ctid,id,name from tmp_t0;tableoid | cmax | xmax | cmin | xmin | ctid | id | name ----------+------+------+------+--------+-------+----+------379767 | 0 | 0 | 0 | 699845 | (0,2) | 1 | b (1 row)可以看到新數據的 xmin=699845 ctid=(0,2)
另外一個會話查詢數據
postgres=# select tableoid,cmax,xmax,cmin,xmin,ctid,id,name from tmp_t0;tableoid | cmax | xmax | cmin | xmin | ctid | id | name ----------+------+--------+------+--------+-------+----+------379767 | 0 | 699845 | 0 | 699844 | (0,1) | 1 | a (1 row)符合預期
參考:
總結
以上是生活随笔為你收集整理的postgresql 数据库表隐藏列 oid、tableoid、ctid、xmin、xmax、cmin、cmax的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue 3 组件开发:搭建基于Sprea
- 下一篇: Android开发如何进阶,薪资如何跟上