Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analyze the code related to OOM in the PostgreSQL SetupLockInTable method

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "analysis of PostgreSQL SetupLockInTable method and OOM related code", in daily operation, I believe many people in the analysis of PostgreSQL SetupLockInTable method and OOM related code problems have doubts, Xiaobian consulted all kinds of information, sorted out simple and easy to use operation methods, hope to answer "analysis of PostgreSQL SetupLockInTable method and OOM related code" doubts helpful! Next, please follow the small series to learn together!

Sometimes we may find the following information in PG logs:

2020-01-09 16:29:19.062 CST,"pg12","testdb",6193,"[local]",5e16dccd.1831,1,"CREATE TABLE",2020-01-09 15:57:01 CST,2/34,1512004206,ERROR,53200,"out of shared memory",,"You might need to increase max_locks_per_transaction. ",,,,"CREATE TABLE a13030 (id int);",,,"psql"2020-01-09 16:29:19.379 CST,"pg12","testdb",6193,"[local]",5e16dccd.1831,2,"CREATE TABLE",2020-01-09 15:57:01 CST,2/0,1512004206,ERROR,25P02,"current transaction is aborted, commands ignored until end of transaction block",,,,,,"CREATE TABLE a13031 (id int);",,,"psql"

Intuitively, OOM seems to have nothing to do with max_locks_per_transaction. Why does PG prompt to increase the value of max_locks_per_transaction?

I. Interpretation of source code

test script

\pset footer off\pset tuples_only\o /tmp/drop.sqlSELECT 'drop table if exists tbl' || id || ' ;' as "--" FROM generate_series(1, 20000) AS id;\i /tmp/drop.sql\pset footer off\pset tuples_only\o /tmp/create.sqlSELECT 'CREATE TABLE tbl' || id || ' (id int);' as "--" FROM generate_series(1, 20000) AS id;\o /tmp/ret.txtbegin;\i /tmp/create.sql

data structure

HTAB

/* * Top control structure for a hashtable --- in a shared table, each backend * has its own copy (OK since no fields change at runtime) * Top control structure for a hashtable. * In this shared hash table, each daemon has its own copy * (there is no problem because no field changes at runtime after the fork comes out) */struct HTAB{ //points to shared control information HASHHDR *hctl; /* => shared control information */ //Paragraph Start Directory HASHSEGMENT *dir; /* directory of segment starts */ //hash function HashValueFunc hash; /* hash function */ //hash key comparison function HashCompareFunc match; /* key comparison function */ //hash key copy function HashCopyFunc keycopy; /* key copying function */ //Memory allocator HashAllocFunc alloc; /* memory allocator */ //Memory context MemoryContext hcxt; /* memory context if default allocator used */ //table name (for error messages) char *tabname; /* table name (for error messages) */ //if in shared memory, T bool isshared; /* true if table is in shared memory */ //if T, fixed size cannot be extended bool isfixed; /* if true, don't enlarge */ /* freezing a shared table isn't allowed, so we can keep state here */ //Freezing shared tables is not allowed, so state is saved here bool frozen; /* true = no more inserts allowed */ /* We keep local copies of these fixed values to reduce contention */ //Save local copies of these fixed values to reduce conflicts //hash key length in bytes Size keysize; /* hash key length in bytes */ //Segment size, must be a power of 2 long ssize; /* segment size --- must be power of 2 */ //Segment offset, logarithm of ssize int sshift; /* segment shift = log2(ssize) */};* * In a shared-memory hash table, the HASHDR is in shared memory, while * each backend has a local HTAB structure. For a non-shared table, there isn't * any functional difference between HASHDR and HTAB, but we separate them * anyway to share code between shared and non-shared tables. * In shared memory hash tables, HASHDR resides in shared memory, and each background process has a native HTAB structure. * For non-shared hash tables, HASHDR and HTAB do not differ in any functional way, * but we distinguish between shared and non-shared tables anyway. */struct HASHHDR{ /* * The freelist can become a point of contention in high-concurrency hash * tables, so we use an array of freelists, each with its own mutex and * nentries count, instead of just a single one. Although the freelists * normally operate independently, we will scavenge entries from freelists * other than a hashcode's default freelist when necessary. * In a hash table with high concurrency, free lists become hot spots for contention, so we use an array of free lists. * Each element in an array has its own mutex and entry statistics, rather than using one. * * If the hash table is not partitioned, only freeList[0] is used and its * spinlock is not used at all; callers' locking is assumed sufficient. * If the hash table has no partitions, then only the freelist[0] element is useful, and spin locks are useless; * Caller locking is considered OK. */ /* Number of freelists to be used for a partitioned hash table. */ //#define NUM_FREELISTS 32 FreeListData freeList[NUM_FREELISTS]; /* These fields can change, but not in a partitioned table */ //These fields can be changed, but not for partition tables /* Also, dsize can't change in a shared table, even if unpartitioned */ //At the same time, even if it is a non-partitioned table, the dsize of the shared table cannot be changed //directory size long dsize; /* directory size */ //allocated segment size (

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report