Re: GIN amcheck leaks memory in gin_check_parent_keys_consistency

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: Michael Paquier <michael(at)paquier(dot)xyz>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: GIN amcheck leaks memory in gin_check_parent_keys_consistency
Date: 2026-07-03 08:14:12
Message-ID: CALdSSPhbu2z8suPVHQy_fnOU-8LgZPMM5qPrxPn+9usNWZVOPA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Fri, 3 Jul 2026 at 12:56, Michael Paquier <michael(at)paquier(dot)xyz> wrote:
>
> On Fri, Jul 03, 2026 at 02:20:08AM +0500, Kirill Reshke wrote:
> > I was debugging the GIN index issue today and noticed my postgresql
> > session which I used to run amcheck eats too much memory. PFA fix.
>
> Hmm.. Still need to do some measurements with a large GIN index to
> get an idea, but a quick lookup at the code points that you are right.
> This could stack quite a lot in a single pass even if we have a
> dedicated memory context while in gin_check_parent_keys_consistency().
> Will measure and check.
> --
> Michael

I have 34GB GIN index where this allocation is frequent in amcheck. I
confirmed this using gdb sampling. I can maybe create synthetic
example, but have little time right now...

[PROD]root(at)sas-tkbm8k25egi7113g ~ # bt -p 1057384
Thu Jul 2 20:09:00 2026

This GDB supports auto-downloading debuginfo from the following URLs:
https://debuginfod.ubuntu.com
Enable debuginfod for this session? (y or [n]) [answered N; input not
from terminal]
Debuginfod has been disabled.
To make this setting permanent, add 'set debuginfod enabled off' to .gdbinit.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x000055b09f178e01 in MemoryChunkSetHdrMask (methodid=MCTX_ASET_ID,
value=<optimized out>, block=0x7effe094f010, chunk=<optimized out>) at
utils/mmgr/./build/../src/include/utils/memutils_memorychunk.h:180
#0 0x000055b09f178e01 in MemoryChunkSetHdrMask
(methodid=MCTX_ASET_ID, value=<optimized out>, block=0x7effe094f010,
chunk=<optimized out>) at
utils/mmgr/./build/../src/include/utils/memutils_memorychunk.h:180
#1 AllocSetAllocChunkFromBlock (fidx=<optimized out>,
chunk_size=<optimized out>, size=32, block=0x7effe094f010,
context=0x55b0a0e33e90) at
utils/mmgr/./build/../src/backend/utils/mmgr/aset.c:788
#2 AllocSetAlloc (context=0x55b0a0e33e90, size=32, flags=<optimized
out>) at utils/mmgr/./build/../src/backend/utils/mmgr/aset.c:1054
#3 0x000055b09eb9c411 in palloc (size=32) at
utils/mmgr/./build/../src/backend/utils/mmgr/mcxt.c:1367
#4 CopyIndexTuple (source=source(at)entry=0x7f0403f68e18) at
access/common/./build/../src/backend/access/common/indextuple.c:553
#5 0x00007f057d4f958a in gin_check_parent_keys_consistency
(rel=rel(at)entry=0x55b0a0e59858, heaprel=heaprel(at)entry=0x55b0a0e54cf8,
callback_state=callback_state(at)entry=0x0,
readonly=readonly(at)entry=false) at
./build/../contrib/amcheck/verify_gin.c:641
#6 0x00007f057d4f8f1d in amcheck_lock_relation_and_check
(indrelid=17422, am_id=2742, check=0x7f057d4f91d0
<gin_check_parent_keys_consistency>, lockmode=1, state=0x0) at
./build/../contrib/amcheck/verify_common.c:132
#7 0x00007f057d4fe054 in gin_index_check (fcinfo=<optimized out>) at
./build/../contrib/amcheck/verify_gin.c:83
#8 0x000055b09edb66ad in ExecMakeTableFunctionResult
(setexpr=0x55b0a0e244d0, econtext=0x55b0a0e24338,
argContext=<optimized out>, expectedDesc=0x55b0a0e24bc8,
randomAccess=false) at
executor/./build/../src/backend/executor/execSRF.c:234
#9 0x000055b09edca31a in FunctionNext (node=0x55b0a0e24130) at
executor/./build/../src/backend/executor/nodeFunctionscan.c:94
#10 0x000055b09eda8345 in ExecProcNode (node=0x55b0a0e24130) at
executor/./build/../src/include/executor/executor.h:315
#11 ExecutePlan (dest=0x55b0a0c7ae58, direction=<optimized out>,
numberTuples=0, sendTuples=<optimized out>, operation=CMD_SELECT,
queryDesc=0x55b0a0e2af88) at
executor/./build/../src/backend/executor/execMain.c:1711
#12 standard_ExecutorRun (queryDesc=0x55b0a0e2af88,
direction=<optimized out>, count=0) at
executor/./build/../src/backend/executor/execMain.c:366
#13 0x00007f057d54b005 in pgss_ExecutorRun (queryDesc=0x55b0a0e2af88,
direction=ForwardScanDirection, count=0) at
./build/../contrib/pg_stat_statements/pg_stat_statements.c:1036
#14 0x00007f057d53cd14 in ?? () from
/usr/lib/postgresql/18/lib/pg_stat_kcache.so
#15 0x00007f057d52a1f3 in ?? () from
/usr/lib/postgresql/18/lib/pg_stat_query_plans.so
#16 0x00007f057d5226e3 in explain_ExecutorRun
(queryDesc=0x55b0a0e2af88, direction=ForwardScanDirection, count=0) at
./build/../contrib/auto_explain/auto_explain.c:335
#17 0x00007f057d50be0e in ?? () from /usr/lib/postgresql/18/lib/pgaudit.so
#18 0x000055b09efc2601 in ExecutorRun (count=0,
direction=ForwardScanDirection, queryDesc=0x55b0a0e2af88) at
executor/./build/../src/backend/executor/execMain.c:301
#19 PortalRunSelect (portal=0x55b0a0d92ca8, forward=<optimized out>,
count=0, dest=<optimized out>) at
tcop/./build/../src/backend/tcop/pquery.c:921
#20 0x000055b09efc40a8 in PortalRun (portal=0x55b0a0d92ca8,
count=9223372036854775807, isTopLevel=<optimized out>,
dest=0x55b0a0c7ae58, altdest=0x55b0a0c7ae58, qc=0x7ffdcdc40200) at
tcop/./build/../src/backend/tcop/pquery.c:765
#21 0x000055b09efbc109 in exec_simple_query
(query_string=0x55b0a0c79208 "select * from
gin_index_check('market_cashier.i_payment_order_ids');") at
tcop/./build/../src/backend/tcop/postgres.c:1278
#22 0x000055b09efbe789 in PostgresMain (dbname=<optimized out>,
username=<optimized out>) at
tcop/./build/../src/backend/tcop/postgres.c:4781
#23 0x000055b09efbf23f in BackendMain (startup_data=<optimized out>,
startup_data_len=<optimized out>) at
tcop/./build/../src/backend/tcop/backend_startup.c:124
#24 0x000055b09ef07b25 in postmaster_child_launch
(child_type=B_BACKEND, child_slot=3, startup_data=0x7ffdcdc407e0,
startup_data_len=24, client_sock=<optimized out>) at
postmaster/./build/../src/backend/postmaster/launch_backend.c:290
#25 0x000055b09ef12e29 in BackendStartup (client_sock=0x7ffdcdc40800)
at postmaster/./build/../src/backend/postmaster/postmaster.c:3569
#26 ServerLoop () at
postmaster/./build/../src/backend/postmaster/postmaster.c:1703
#27 0x000055b09ef09680 in PostmasterMain (argc=9, argv=<optimized
out>) at postmaster/./build/../src/backend/postmaster/postmaster.c:1401
#28 0x000055b09eb8e20b in main (argc=9, argv=0x55b0a0be22f0) at
main/./build/../src/backend/main/main.c:227
[Inferior 1 (process 1057384) detached]

index definition is simple

"i_payment_order_ids" gin (order_ids) where order_ids is bigint[]

--
Best regards,
Kirill Reshke

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Ewan Young 2026-07-03 08:19:18 Re: Mark class_descr strings for translation
Previous Message Zhijie Hou (Fujitsu) 2026-07-03 08:08:13 RE: Re-read subscription state after lock in AlterSubscription