Postgresql kernel source code analysis - unlogged table

 74ac905cfa3740079f2f66a445a3d7c2.gif#pic_center

 

 

  • Column content: postgresql kernel source code analysis
  • Personal homepage: My homepage
  • Motto: Tian Xingjian, the gentleman strives for self-improvement;

Table of contents

foreword

concept introduction

Principle introduction

code analysis

demo

end


foreword

This article is based on the analysis and interpretation of the postgresql 15 code, and the demonstration is carried out on the centos8 system.


 

  • concept introduction

Specify as an unlogged table, which will not record wal logs, that is, the data will be at risk of loss and will not be restored.

 

When will it be used? The reason why the unlogged table does not record wal is that insert/update operations are much faster than ordinary tables, so try it out during performance testing.


  • Principle introduction

So how is the unlogged table implemented?

 

  1. The process of creating a table is the same as that of an ordinary table, except that when creating a table file, an additional fork file will be created, that is, INIT_FORK, and the file is named relfilenode_init;
  2. Normal insert/update is consistent with ordinary table files, except that wal is not recorded, and the data is also written into the relfilenode table file corresponding to MAIN_FORK.
  3. When the database stops abnormally, when the recovery is started again, if there is an _init file, the MAIN_FORK file is directly reset to the content of the _init file; that is to say, the data will not exist, and the state of creating the table will be restored;
  4. The index created on the unlogged table is also in the unlogged mode. Similar to the table file, there will be an _init file, which will be reset during recovery.

  • code analysis

  1. When creating a table, call heapam_relation_set_new_filelocator in heap_create to create main_fork and init_fork table files for unlogged tables;
  2. Calling ResetUnloggedRelations(int op) in StartXlog during recovery will reset the table file or index file with the _init file.
  3. When truncate unlogged table, calling RelationSetNewRelfilenumber will also create _init file when creating a new table file;

  • demo

Create an unlogged table

postgres=# create unlogged table tbl_unloggedtest1(name int);

CREATE TABLE

postgres=# insert into tbl_unloggedtest1 select id from generate_series(1,1000) as id;

INSERT 0 1000

 

Find the corresponding relfilenode

postgres=# select * from pg_class where relname = 'tbl_unloggedtest1';

  oid  |      relname      | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | relallvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind | relnatts |

 relchecks | relhasrules | relhastriggers | relhassubclass | relrowsecurity | relforcerowsecurity | relispopulated | relreplident | relispartition | relrewrite | relfrozenxid | relminmxid | relacl | reloptions | relpartbound

-------+-------------------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---------------+---------------+-------------+-------------+----------------+---------+----------+

-----------+-------------+----------------+----------------+----------------+---------------------+----------------+--------------+----------------+------------+--------------+------------+--------+------------+--------------

 24656 | tbl_unloggedtest1 |         2200 |   24658 |         0 |       10 |     2 |       24656 |             0 |        0 |        -1 |             0 |             0 | f           | f           | u              | r       |        1 |

         0 | f           | f              | f              | f              | f                   | t              | d            | f              |          0 |         1709 |          1 |        |            |

(1 row)

Looking at the table file, you can see that there is no data in the _init file, and it is still in the initial state. Of course, all data will be lost after recovery.

[postgres @localhost 5]$ ls -l 24656*

-rw-------. 1 postgres postgres 40960 Feb 23 08:49 24656

-rw-------. 1 postgres postgres 24576 Feb 23 08:47 24656_fsm

-rw-------. 1 postgres postgres 0 Feb 22 09:25 24656_init

 


end

Author email: [email protected]
If there are any mistakes or omissions, please point them out and learn from each other.

Note: Do not reprint without consent!

 

Guess you like

Origin blog.csdn.net/senllang/article/details/129193286