Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRfGe-0005Xx-Um for pgsql-general@arkaria.postgresql.org; Wed, 31 Mar 2021 18:08:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lRfGd-0004TQ-B4 for pgsql-general@arkaria.postgresql.org; Wed, 31 Mar 2021 18:08:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRfGc-0004TJ-V7 for pgsql-general@lists.postgresql.org; Wed, 31 Mar 2021 18:08:35 +0000 Received: from [2a01:4f9:3a:121a::2] (helo=mail.stephans-server.de) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lRfGZ-0001FO-Mb for pgsql-general@lists.postgresql.org; Wed, 31 Mar 2021 18:08:33 +0000 Received: from [192.168.178.22] ([::ffff:87.123.124.210]) (AUTH: PLAIN knauss, ) by mail.stephans-server.de with ESMTPSA id 00000000001E0E71.000000006064BA99.002527C8; Wed, 31 Mar 2021 20:08:25 +0200 Subject: Re: Debugging leaking memory in Postgresql 13.2/Postgis 3.1 To: Tom Lane Cc: "pgsql-generallists.postgresql.org" References: <9f73e655-14b8-feaf-bd66-c0f506224b9e@stephans-server.de> <2919657.1617129981@sss.pgh.pa.us> From: Stephan Knauss Message-ID: <6ea52e56-b401-7716-f592-a8fdc98df667@stephans-server.de> Date: Wed, 31 Mar 2021 20:08:23 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.9.0 MIME-Version: 1.0 In-Reply-To: <2919657.1617129981@sss.pgh.pa.us> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable Content-Language: en-US X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a01:4f9:3a:121a::2 (failed) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 30.03.2021 20:46, Tom Lane wrote: > Stephan Knauss writes: >> The wiki suggested to dump MemoryContext states for more details, but >> something strange happens when attaching gdb. It seems that the proces= s >> is immediately killed and I can no longer dump such details. > (I think the -v option is the one that matters on Linux, not -d > as you might guess). The idea here is that the backends would > get an actual ENOMEM failure from malloc() before reaching the > point where the kernel's OOM-kill behavior takes over. Given > that, they'd dump memory maps to stderr of their own accord, > and you could maybe get some insight as to what's leaking. > This'd also reduce the severity of the problem when it does > happen. Hello Tom, the output below looks similar to the OOM output you=20 expected. Can you give a hint how to interpret the results? I had a backend which had a larger amount of memory allocated already.=20 So I gave "gcore -a" a try. In contrast to the advertised behavior, the process did not continue to=20 run but I got a core file at least. Probably related to gcore just=20 calling gdb attach which somehow triggers a SIGKILL of all backends. With 4.2GB in size it hopefully has most of the relevant memory=20 structures are there. Without a running process I still can not call=20 MemoryContextStats(), but I found a macro which claims to decode the=20 memory structure post mortem: https://www.cybertec-postgresql.com/en/checking-per-memory-context-memory= -consumption/ This gave me the following memory structure: How should it be interpreted? It looks like the size is bytes as it=20 calculates with pointers. But the numbers look a bit small, given that I = had a backend with roughly 6GB RSS memory. I thought it might print overall size and then indent and print the=20 memory of children, but the numbers do indicate this is not the case,=20 having a higher level smaller size than children: =C2=A0 CachedPlanSource: 67840 =C2=A0=C2=A0 unnamed prepared statement: 261920 So how to read it and any indication why I have a constantly increasing=20 memory footprint? Is there any indication where multiple gigabytes are=20 allocated? root@0ec98d20bda2:/# gdb /usr/lib/postgresql/13/bin/postgres core.154218 = This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Type "show copying" and "show warranty" for details. This GDB was configured as "x86_64-linux-gnu". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: =C2=A0=C2=A0=C2=A0 . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from /usr/lib/postgresql/13/bin/postgres...Reading=20 symbols from=20 /usr/lib/debug/.build-id/31/ae2853776500091d313e76cf679017e697884b.debug.= =2E.done. done. warning: core file may not match specified executable file. [New LWP 154218] [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1"= =2E Core was generated by `postgres: osm gis 172.20.0.3(51894) idle'. #0=C2=A0 0x00007fc01cfa07b7 in epoll_wait (epfd=3D4, events=3D0x55f403584= 080,=20 maxevents=3Dmaxevents@entry=3D1, timeout=3Dtimeout@entry=3D-1) at=20 =2E./sysdeps/unix/sysv/linux/epoll_wait.c:30 30=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 ../sysdeps/unix/sysv/linux/epoll_wait.c:=20 No such file or directory. (gdb) >>>> > > >>>(gdb) (gdb) >>>> > > >>>>> > > >>(gdb) (gdb)=20 TopMemoryContext: 109528 =C2=A0dynahash: 7968 =C2=A0HandleParallelMessages: 7968 =C2=A0dynahash: 7968 =C2=A0dynahash: 7968 =C2=A0dynahash: 7968 =C2=A0dynahash: 24392 =C2=A0dynahash: 24352 =C2=A0RowDescriptionContext: 24352 =C2=A0MessageContext: 7968 =C2=A0dynahash: 7968 =C2=A0dynahash: 32544 =C2=A0TransactionAbortContext: 32544 =C2=A0dynahash: 7968 =C2=A0TopPortalContext: 7968 =C2=A0dynahash: 16160 =C2=A0CacheMemoryContext: 1302944 =C2=A0 CachedPlan: 138016 =C2=A0 CachedPlanSource: 67840 =C2=A0=C2=A0 unnamed prepared statement: 261920 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 relation rules: 32544 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 relation rules: 24352 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 relation rules: 32544 =C2=A0 index info: 1824 =C2=A0 index info: 2848 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 3872 =C2=A0 index info: 1824 =C2=A0 index info: 3872 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 relation rules: 32544 =C2=A0 index info: 1824 =C2=A0 index info: 2848 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 800 =C2=A0 index info: 2848 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 800 =C2=A0 index info: 800 =C2=A0 index info: 2848 =C2=A0 index info: 1824 =C2=A0 index info: 1824 --Type for more, q to quit, c to continue without paging--=C2=A0 in= dex=20 info: 2848 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 800 =C2=A0 index info: 800 =C2=A0 index info: 2848 =C2=A0 index info: 2848 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 800 =C2=A0 index info: 2848 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 2848 =C2=A0 index info: 2848 =C2=A0 index info: 2848 =C2=A0 index info: 800 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 2848 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 2848 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 800 =C2=A0 index info: 1824 =C2=A0 index info: 2848 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0 index info: 1824 =C2=A0WAL record construction: 49544 =C2=A0dynahash: 7968 =C2=A0MdSmgr: 7968 =C2=A0dynahash: 16160 =C2=A0dynahash: 103896 =C2=A0ErrorContext: 7968 (gdb) quit root@0ec98d20bda2:/# cat gdb-context define sum_context_blocks set $context =3D $arg0 set $block =3D ((AllocSet) $context)->blocks set $size =3D 0 while ($block) set $size =3D $size + (((AllocBlock) $block)->endptr - ((char *) $block))= set $block =3D ((AllocBlock) $block)->next end printf "%s: %d\n",((MemoryContext)$context)->name, $size end define walk_contexts set $parent_$arg0 =3D ($arg1) set $indent_$arg0 =3D ($arg0) set $i_$arg0 =3D $indent_$arg0 while ($i_$arg0) printf " " set $i_$arg0 =3D $i_$arg0 - 1 end sum_context_blocks $parent_$arg0 set $child_$arg0 =3D ((MemoryContext) $parent_$arg0)->firstchild set $indent_$arg0 =3D $indent_$arg0 + 1 while ($child_$arg0) walk_contexts $indent_$arg0 $child_$arg0 set $child_$arg0 =3D ((MemoryContext) $child_$arg0)->nextchild end end walk_contexts 0 TopMemoryContext