
编译参数
在对PostgreSQL 进行编译的情况下,要加上这两个参数,否则命令无法获得相关的内存的信息。
./configure --enable-debug --enable-cassert
在编译后,直接初始化数据库
同时需要编译 valgrind 软件,在数据库所在的系统上
通过下面的命令来启动,数据库产品,启动后,后续数据库的一些的操作所涉及的内存方面的分配和工作都会在日志里面进行展示。
valgrind --leak-check=full --track-origins=yes --log-file=/home/postgres/valgrind.log --trace-children=yes usr/local/postgres/bin/pg_ctl -D pgdata/data start
比如下面这段就是
==37794== 584 bytes in 1 blocks are possibly lost in loss record 16 of 82
==37794== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37794== by 0x56027C: RegisterBackgroundWorker (bgworker.c:929)
==37794== by 0x580E8B: ApplyLauncherRegister (launcher.c:923)
==37794== by 0x56811F: PostmasterMain (postmaster.c:1003)
==37794== by 0x26F2D0: main (main.c:198)
==37794==
==37794== 2,666 (232 direct, 2,434 indirect) bytes in 1 blocks are definitely lost in loss record 35 of 82
==37794== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37794== by 0x761C5A: save_ps_display_args (ps_status.c:165)
==37794== by 0x26F101: main (main.c:91)
==37794==
==37794== LEAK SUMMARY:
==37794== definitely lost: 245 bytes in 2 blocks
==37794== indirectly lost: 2,434 bytes in 28 blocks
==37794== possibly lost: 624 bytes in 2 blocks
==37794== still reachable: 1,229,816 bytes in 204 blocks
==37794== suppressed: 0 bytes in 0 blocks
==37794== Reachable blocks (those to which a pointer was found) are not shown.
==37794== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37794==
==37794== For lists of detected and suppressed errors, rerun with: -s
==37794== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
==37795==
==37795== HEAP SUMMARY:
==37795== in use at exit: 1,294,559 bytes in 240 blocks
==37795== total heap usage: 7,193 allocs, 6,953 frees, 3,359,897 bytes allocated
==37795==
==37795== 13 bytes in 1 blocks are definitely lost in loss record 6 of 84
==37795== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37795== by 0x570258E: strdup (strdup.c:42)
==37795== by 0x567D71: PostmasterMain (postmaster.c:737)
==37795== by 0x26F2D0: main (main.c:198)
==37795==
==37795== 40 bytes in 1 blocks are possibly lost in loss record 10 of 84
==37795== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37795== by 0x565463: assign_backendlist_entry (postmaster.c:5864)
==37795== by 0x565463: do_start_bgworker (postmaster.c:5715)
==37795== by 0x565463: maybe_start_bgworkers (postmaster.c:5989)
==37795== by 0x56626C: process_pm_child_exit (postmaster.c:3106)
==37795== by 0x56626C: ServerLoop (postmaster.c:1771)
==37795== by 0x5687D5: PostmasterMain (postmaster.c:1466)
==37795== by 0x26F2D0: main (main.c:198)
==37795==
==37795== 584 bytes in 1 blocks are possibly lost in loss record 16 of 84
==37795== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37795== by 0x56027C: RegisterBackgroundWorker (bgworker.c:929)
==37795== by 0x580E8B: ApplyLauncherRegister (launcher.c:923)
==37795== by 0x56811F: PostmasterMain (postmaster.c:1003)
==37795== by 0x26F2D0: main (main.c:198)
==37795==
==37795== 2,666 (232 direct, 2,434 indirect) bytes in 1 blocks are definitely lost in loss record 34 of 84
==37795== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37795== by 0x761C5A: save_ps_display_args (ps_status.c:165)
==37795== by 0x26F101: main (main.c:91)
==37795==
==37795== LEAK SUMMARY:
==37795== definitely lost: 245 bytes in 2 blocks
==37795== indirectly lost: 2,434 bytes in 28 blocks
==37795== possibly lost: 624 bytes in 2 blocks
==37795== still reachable: 1,291,256 bytes in 208 blocks
==37795== suppressed: 0 bytes in 0 blocks
==37795== Reachable blocks (those to which a pointer was found) are not shown.
==37795== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37795==
==37795== For lists of detected and suppressed errors, rerun with: -s
==37795== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
tail: valgrind.log: file truncated
==37796== Memcheck, a memory error detector
==37796== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==37796== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==37796== Command: /bin/sh -c test\ !\ -f\ /pgdata/archive/000000010000000000000001\ &&\ cp\ pg_wal/000000010000000000000001\ /pgdata/archive/000000010000000000000001
==37796== Parent PID: 37524
==37796==
==37796==
==37796== HEAP SUMMARY:
==37796== in use at exit: 1,114 bytes in 36 blocks
==37796== total heap usage: 40 allocs, 4 frees, 5,874 bytes allocated
==37796==
==37796== LEAK SUMMARY:
==37796== definitely lost: 0 bytes in 0 blocks
==37796== indirectly lost: 0 bytes in 0 blocks
==37796== possibly lost: 0 bytes in 0 blocks
==37796== still reachable: 1,114 bytes in 36 blocks
==37796== suppressed: 0 bytes in 0 blocks
==37796== Reachable blocks (those to which a pointer was found) are not shown.
==37796== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37796==
==37796== For lists of detected and suppressed errors, rerun with: -s
==37796== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
tail: valgrind.log: file truncated
==37797== Memcheck, a memory error detector
==37797== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==37797== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==37797== Command: /bin/sh -c test\ !\ -f\ /pgdata/archive/000000010000000000000001\ &&\ cp\ pg_wal/000000010000000000000001\ /pgdata/archive/000000010000000000000001
==37797== Parent PID: 37524
==37797==
==37797==
==37797== HEAP SUMMARY:
==37797== in use at exit: 1,114 bytes in 36 blocks
==37797== total heap usage: 40 allocs, 4 frees, 5,874 bytes allocated
==37797==
==37797== LEAK SUMMARY:
==37797== definitely lost: 0 bytes in 0 blocks
==37797== indirectly lost: 0 bytes in 0 blocks
==37797== possibly lost: 0 bytes in 0 blocks
==37797== still reachable: 1,114 bytes in 36 blocks
==37797== suppressed: 0 bytes in 0 blocks
==37797== Reachable blocks (those to which a pointer was found) are not shown.
==37797== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37797==
==37797== For lists of detected and suppressed errors, rerun with: -s
==37797== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
tail: valgrind.log: file truncated
==37798== Memcheck, a memory error detector
==37798== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==37798== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==37798== Command: /bin/sh -c test\ !\ -f\ /pgdata/archive/000000010000000000000001\ &&\ cp\ pg_wal/000000010000000000000001\ /pgdata/archive/000000010000000000000001
==37798== Parent PID: 37524
==37798==
==37798==
==37798== HEAP SUMMARY:
==37798== in use at exit: 1,114 bytes in 36 blocks
==37798== total heap usage: 40 allocs, 4 frees, 5,874 bytes allocated
==37798==
==37798== LEAK SUMMARY:
==37798== definitely lost: 0 bytes in 0 blocks
==37798== indirectly lost: 0 bytes in 0 blocks
==37798== possibly lost: 0 bytes in 0 blocks
==37798== still reachable: 1,114 bytes in 36 blocks
==37798== suppressed: 0 bytes in 0 blocks
==37798== Reachable blocks (those to which a pointer was found) are not shown.
==37798== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37798==
==37798== For lists of detected and suppressed errors, rerun with: -s
==37798== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==37799==
==37799== HEAP SUMMARY:
==37799== in use at exit: 1,233,119 bytes in 236 blocks
==37799== total heap usage: 7,191 allocs, 6,955 frees, 3,325,897 bytes allocated
==37799==
==37799== 13 bytes in 1 blocks are definitely lost in loss record 6 of 82
==37799== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37799== by 0x570258E: strdup (strdup.c:42)
==37799== by 0x567D71: PostmasterMain (postmaster.c:737)
==37799== by 0x26F2D0: main (main.c:198)
==37799==
==37799== 40 bytes in 1 blocks are possibly lost in loss record 10 of 82
==37799== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37799== by 0x565463: assign_backendlist_entry (postmaster.c:5864)
==37799== by 0x565463: do_start_bgworker (postmaster.c:5715)
==37799== by 0x565463: maybe_start_bgworkers (postmaster.c:5989)
==37799== by 0x56626C: process_pm_child_exit (postmaster.c:3106)
==37799== by 0x56626C: ServerLoop (postmaster.c:1771)
==37799== by 0x5687D5: PostmasterMain (postmaster.c:1466)
==37799== by 0x26F2D0: main (main.c:198)
==37799==
==37799== 584 bytes in 1 blocks are possibly lost in loss record 16 of 82
==37799== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37799== by 0x56027C: RegisterBackgroundWorker (bgworker.c:929)
==37799== by 0x580E8B: ApplyLauncherRegister (launcher.c:923)
==37799== by 0x56811F: PostmasterMain (postmaster.c:1003)
==37799== by 0x26F2D0: main (main.c:198)
==37799==
==37799== 2,666 (232 direct, 2,434 indirect) bytes in 1 blocks are definitely lost in loss record 35 of 82
==37799== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37799== by 0x761C5A: save_ps_display_args (ps_status.c:165)
==37799== by 0x26F101: main (main.c:91)
==37799==
==37799== LEAK SUMMARY:
==37799== definitely lost: 245 bytes in 2 blocks
==37799== indirectly lost: 2,434 bytes in 28 blocks
==37799== possibly lost: 624 bytes in 2 blocks
==37799== still reachable: 1,229,816 bytes in 204 blocks
==37799== suppressed: 0 bytes in 0 blocks
==37799== Reachable blocks (those to which a pointer was found) are not shown.
==37799== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37799==
==37945== HEAP SUMMARY:
==37945== in use at exit: 1,114 bytes in 36 blocks
==37945== total heap usage: 40 allocs, 4 frees, 5,874 bytes allocated
==37945==
==37945== LEAK SUMMARY:
==37945== definitely lost: 0 bytes in 0 blocks
==37945== indirectly lost: 0 bytes in 0 blocks
==37945== possibly lost: 0 bytes in 0 blocks
==37945== still reachable: 1,114 bytes in 36 blocks
==37945== suppressed: 0 bytes in 0 blocks
==37945== Reachable blocks (those to which a pointer was found) are not shown.
==37945== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37945==
==37945== For lists of detected and suppressed errors, rerun with: -s
==37945== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
tail: valgrind.log: file truncated
==37946== Memcheck, a memory error detector
==37946== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==37946== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==37946== Command: /bin/sh -c test\ !\ -f\ /pgdata/archive/000000010000000000000001\ &&\ cp\ pg_wal/000000010000000000000001\ /pgdata/archive/000000010000000000000001
==37946== Parent PID: 37844
==37946==
==37946==
==37946== HEAP SUMMARY:
==37946== in use at exit: 1,114 bytes in 36 blocks
==37946== total heap usage: 40 allocs, 4 frees, 5,874 bytes allocated
==37946==
==37946== LEAK SUMMARY:
==37946== definitely lost: 0 bytes in 0 blocks
==37946== indirectly lost: 0 bytes in 0 blocks
==37946== possibly lost: 0 bytes in 0 blocks
==37946== still reachable: 1,114 bytes in 36 blocks
==37946== suppressed: 0 bytes in 0 blocks
==37946== Reachable blocks (those to which a pointer was found) are not shown.
==37946== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37946==
==37946== For lists of detected and suppressed errors, rerun with: -s
==37946== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==37944== 13 bytes in 1 blocks are definitely lost in loss record 6 of 82
==37944== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37944== by 0x570258E: strdup (strdup.c:42)
==37944== by 0x567D71: PostmasterMain (postmaster.c:737)
==37944== by 0x26F2D0: main (main.c:198)
==37944==
==37944== 40 bytes in 1 blocks are possibly lost in loss record 10 of 82
==37944== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37944== by 0x565463: assign_backendlist_entry (postmaster.c:5864)
==37944== by 0x565463: do_start_bgworker (postmaster.c:5715)
==37944== by 0x565463: maybe_start_bgworkers (postmaster.c:5989)
==37944== by 0x56626C: process_pm_child_exit (postmaster.c:3106)
==37944== by 0x56626C: ServerLoop (postmaster.c:1771)
==37944== by 0x5687D5: PostmasterMain (postmaster.c:1466)
==37944== by 0x26F2D0: main (main.c:198)
==37944==
==37944== 584 bytes in 1 blocks are possibly lost in loss record 16 of 82
==37944== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37944== by 0x56027C: RegisterBackgroundWorker (bgworker.c:929)
==37944== by 0x580E8B: ApplyLauncherRegister (launcher.c:923)
==37944== by 0x56811F: PostmasterMain (postmaster.c:1003)
==37944== by 0x26F2D0: main (main.c:198)
==37944==
==37944== 2,666 (232 direct, 2,434 indirect) bytes in 1 blocks are definitely lost in loss record 35 of 82
==37944== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37944== by 0x761C5A: save_ps_display_args (ps_status.c:165)
==37944== by 0x26F101: main (main.c:91)
==37944==
==37944== LEAK SUMMARY:
==37944== definitely lost: 245 bytes in 2 blocks
==37944== indirectly lost: 2,434 bytes in 28 blocks
==37944== possibly lost: 624 bytes in 2 blocks
==37944== still reachable: 1,229,816 bytes in 204 blocks
==37944== suppressed: 0 bytes in 0 blocks
==37944== Reachable blocks (those to which a pointer was found) are not shown.
==37944== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37944==
==37944== For lists of detected and suppressed errors, rerun with: -s
==37944== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
==37947==
==37947== HEAP SUMMARY:
==37947== in use at exit: 1,294,559 bytes in 240 blocks
==37947== total heap usage: 7,137 allocs, 6,897 frees, 3,358,113 bytes allocated
==37947==
==37947== 13 bytes in 1 blocks are definitely lost in loss record 6 of 84
==37947== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37947== by 0x570258E: strdup (strdup.c:42)
==37947== by 0x567D71: PostmasterMain (postmaster.c:737)
==37947== by 0x26F2D0: main (main.c:198)
==37947==
==37947== 40 bytes in 1 blocks are possibly lost in loss record 10 of 84
==37947== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37947== by 0x565463: assign_backendlist_entry (postmaster.c:5864)
==37947== by 0x565463: do_start_bgworker (postmaster.c:5715)
==37947== by 0x565463: maybe_start_bgworkers (postmaster.c:5989)
==37947== by 0x56626C: process_pm_child_exit (postmaster.c:3106)
==37947== by 0x56626C: ServerLoop (postmaster.c:1771)
==37947== by 0x5687D5: PostmasterMain (postmaster.c:1466)
==37947== by 0x26F2D0: main (main.c:198)
==37947==
==37947== 584 bytes in 1 blocks are possibly lost in loss record 16 of 84
==37947== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37947== by 0x56027C: RegisterBackgroundWorker (bgworker.c:929)
==37947== by 0x580E8B: ApplyLauncherRegister (launcher.c:923)
==37947== by 0x56811F: PostmasterMain (postmaster.c:1003)
==37947== by 0x26F2D0: main (main.c:198)
==37947==
==37947== 2,666 (232 direct, 2,434 indirect) bytes in 1 blocks are definitely lost in loss record 34 of 84
==37947== at 0x484880F: malloc (vg_replace_malloc.c:446)
==37947== by 0x761C5A: save_ps_display_args (ps_status.c:165)
==37947== by 0x26F101: main (main.c:91)
==37947==
==37947== LEAK SUMMARY:
==37947== definitely lost: 245 bytes in 2 blocks
==37947== indirectly lost: 2,434 bytes in 28 blocks
==37947== possibly lost: 624 bytes in 2 blocks
==37947== still reachable: 1,291,256 bytes in 208 blocks
==37947== suppressed: 0 bytes in 0 blocks
==37947== Reachable blocks (those to which a pointer was found) are not shown.
==37947== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==37947==
==37947== For lists of detected and suppressed errors, rerun with: -s
==37947== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)
postgres=# exit
postgres@pg16:~$ cd /home/postgres/
postgres@pg16:~$ ls
data.backup valgrind_client.log valgrind.log
postgres@pg16:~$ cat valgrind_client.log
==38001== Memcheck, a memory error detector
==38001== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==38001== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==38001== Command: /usr/local/postgres/bin/psql
==38001== Parent PID: 37352
==38001==
postgres@pg16:~$ tail -f valgrind_client.log
==38001== Memcheck, a memory error detector
==38001== Copyright (C) 2002-2024, and GNU GPL'd, by Julian Seward et al.
==38001== Using Valgrind-3.23.0 and LibVEX; rerun with -h for copyright info
==38001== Command: /usr/local/postgres/bin/psql
==38001== Parent PID: 37352
==38001==

==38568== 2,666 (232 direct, 2,434 indirect) bytes in 1 blocks are definitely lost in loss record 37 of 78
==38568== at 0x484880F: malloc (vg_replace_malloc.c:446)
==38568== by 0x761C5A: save_ps_display_args (ps_status.c:165)
==38568== by 0x26F101: main (main.c:91)
==38568==
==38568== LEAK SUMMARY:
==38568== definitely lost: 245 bytes in 2 blocks
==38568== indirectly lost: 2,434 bytes in 28 blocks
==38568== possibly lost: 704 bytes in 4 blocks
==38568== still reachable: 1,165,693 bytes in 197 blocks
==38568== suppressed: 0 bytes in 0 blocks
==38568== Reachable blocks (those to which a pointer was found) are not shown.
==38568== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==38568==
==38568== For lists of detected and suppressed errors, rerun with: -s
==38568== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 0 from 0)
==38571==
==38571== HEAP SUMMARY:
==38571== in use at exit: 1,294,559 bytes in 240 blocks
==38571== total heap usage: 7,202 allocs, 6,962 frees, 3,362,817 bytes allocated
==38571==
==38568== 2,666 (232 direct, 2,434 indirect) bytes in 1 blocks are definitely lost in loss record 37 of 78
比如如definiely lost 这个部分的意思是内存存在泄露,也就是没有任何的指针指向这些内存块,他们是无法被访问的。
往期热门文章:
PostgreSQL 同一种SQL为什么这样写会提升45%性能 --程序员和DBA思维方式不同决定
感谢 老虎刘 刘老师 对 5月20日 SQL 问题纠正贴 ---PostgreSQL 同一种SQL为什么这样写会提升45%性能
MySQL 的SQL引擎很差吗?由一个同学提出问题引出的实验
PostgreSQL 熊灿灿一句话够学半个月 之 KILL -9
PolarDB Serverless POC测试中有没有坑与发现的疑问 (大妖复仇记前传)
临时工访谈:庙小妖风大-PolarDB 组团镇妖 之 他们是第一 (阿里云组团PK笔者实录)
临时工访谈:金牌 “女” 销售从ORACLE 转到另类国产数据库 到底 为什么?
临时工访谈:无名氏意外到访-- 也祝你好运(管理者PUA DBA现场直播)
截止今天已经发布






