Thursday, February 8, 2024

new extension pgmeminfo

I wrote very simple extension pgmeminfo for PostgreSQL12+, that should to help with investigation of memory usage by Postgres. Function pgmeminfo returns glibc information about memory usage provided by function mallinfo(). It is much more precious than usage top or htop.

Second function pgmeminfo_contexts returns data about memory contexts - this is PostgreSQL internal memory management. Same data can be showed by view pg_get_backend_memory_contexts. The function pgmeminfo_contexts can simply accumulate data, and can simply limit deep of iteration over memory contexts. It is little bit faster than recursive query over view (but it is not too important).


(2024-02-08 19:16:12) postgres=# SELECT * FROM pgmeminfo();
┌─────────┬─────────┬────────┬───────┬────────┬─────────┬─────────┬──────────┬──────────┬──────────┐
│  arena  │ ordblks │ smblks │ hblks │ hblkhd │ usmblks │ fsmblks │ uordblks │ fordblks │ keepcost │
╞═════════╪═════════╪════════╪═══════╪════════╪═════════╪═════════╪══════════╪══════════╪══════════╡
│ 1118208 │       7 │      0 │     2 │ 401408 │       0 │       0 │  1056160 │    62048 │    56368 │
└─────────┴─────────┴────────┴───────┴────────┴─────────┴─────────┴──────────┴──────────┴──────────┘
(1 row)
(2024-02-08 19:18:44) postgres=# select * from pgmeminfo_contexts(deep => 1, accum_mode=>'off');
┌──────────────────────────────┬──────────────────────────┬──────────────────┬───────┬─────────────┬───────────────┬────────────┬────────────┐
│ name │ ident │ parent │ level │ total_bytes │ total_nblocks │ free_bytes │ used_bytes │
╞══════════════════════════════╪══════════════════════════╪══════════════════╪═══════╪═════════════╪═══════════════╪════════════╪════════════╡
│ TopMemoryContext │ │ │ 0 │ 97696 │ 5 │ 9840 │ 87856 │
│ TopTransactionContext │ │ TopMemoryContext │ 1 │ 8192 │ 1 │ 7760 │ 432 │
│ dynahash │ CFuncHash │ TopMemoryContext │ 1 │ 8192 │ 1 │ 576 │ 7616 │
│ dynahash │ Record information cache │ TopMemoryContext │ 1 │ 8192 │ 1 │ 1600 │ 6592 │
│ dynahash │ TableSpace cache │ TopMemoryContext │ 1 │ 8192 │ 1 │ 2112 │ 6080 │
│ RegexpCacheMemoryContext │ │ TopMemoryContext │ 1 │ 1024 │ 1 │ 784 │ 240 │
│ dynahash │ Type information cache │ TopMemoryContext │ 1 │ 24384 │ 2 │ 2640 │ 21744 │
│ dynahash │ Operator lookup cache │ TopMemoryContext │ 1 │ 24576 │ 2 │ 10776 │ 13800 │
│ RowDescriptionContext │ │ TopMemoryContext │ 1 │ 8192 │ 1 │ 6912 │ 1280 │
│ MessageContext │ │ TopMemoryContext │ 1 │ 32768 │ 3 │ 10392 │ 22376 │
│ search_path processing cache │ │ TopMemoryContext │ 1 │ 8192 │ 1 │ 5616 │ 2576 │
│ dynahash │ Operator class cache │ TopMemoryContext │ 1 │ 8192 │ 1 │ 576 │ 7616 │
│ dynahash │ smgr relation table │ TopMemoryContext │ 1 │ 32768 │ 3 │ 16848 │ 15920 │
│ PgStat Shared Ref Hash │ │ TopMemoryContext │ 1 │ 7232 │ 2 │ 704 │ 6528 │
│ PgStat Shared Ref │ │ TopMemoryContext │ 1 │ 8192 │ 4 │ 2952 │ 5240 │
│ PgStat Pending │ │ TopMemoryContext │ 1 │ 16384 │ 5 │ 15984 │ 400 │
│ TransactionAbortContext │ │ TopMemoryContext │ 1 │ 32768 │ 1 │ 32528 │ 240 │
│ dynahash │ Portal hash │ TopMemoryContext │ 1 │ 8192 │ 1 │ 576 │ 7616 │
│ TopPortalContext │ │ TopMemoryContext │ 1 │ 8192 │ 1 │ 7680 │ 512 │
│ dynahash │ Relcache by OID │ TopMemoryContext │ 1 │ 16384 │ 2 │ 3528 │ 12856 │
│ CacheMemoryContext │ │ TopMemoryContext │ 1 │ 1048576 │ 8 │ 399064 │ 649512 │
│ WAL record construction │ │ TopMemoryContext │ 1 │ 49976 │ 2 │ 6384 │ 43592 │
│ dynahash │ PrivateRefCount │ TopMemoryContext │ 1 │ 8192 │ 1 │ 2640 │ 5552 │
│ MdSmgr │ │ TopMemoryContext │ 1 │ 8192 │ 1 │ 7016 │ 1176 │
│ dynahash │ LOCALLOCK hash │ TopMemoryContext │ 1 │ 16384 │ 2 │ 4616 │ 11768 │
│ GUCMemoryContext │ │ TopMemoryContext │ 1 │ 24576 │ 2 │ 12000 │ 12576 │
│ dynahash │ Timezones │ TopMemoryContext │ 1 │ 104128 │ 2 │ 2640 │ 101488 │
│ ErrorContext │ │ TopMemoryContext │ 1 │ 8192 │ 1 │ 7952 │ 240 │
└──────────────────────────────┴──────────────────────────┴──────────────────┴───────┴─────────────┴───────────────┴────────────┴────────────┘
(28 rows)

(2024-02-08 19:18:46) postgres=# select * from pgmeminfo_contexts();
┌──────────────────┬───────┬────────┬───────┬─────────────┬───────────────┬────────────┬────────────┐
│ name │ ident │ parent │ level │ total_bytes │ total_nblocks │ free_bytes │ used_bytes │
╞══════════════════╪═══════╪════════╪═══════╪═════════════╪═══════════════╪════════════╪════════════╡
│ TopMemoryContext │ │ │ 0 │ 1945816 │ 248 │ 690856 │ 1254960 │
└──────────────────┴───────┴────────┴───────┴─────────────┴───────────────┴────────────┴────────────┘
(1 row)