Re: Potential memory leak in contrib/intarray's g_intbig_compress
Tom Lane <tgl@sss.pgh.pa.us>
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Matthias van de Meent <boekewurm+postgres@gmail.com>
Cc: PostgreSQL Hackers <pgsql-hackers@postgresql.org>,
Konstantin Knizhnik <knizhnik@garret.ru>
Date: 2023-07-13T16:22:08Z
Lists: pgsql-hackers
I wrote: > Matthias van de Meent <boekewurm+postgres@gmail.com> writes: >> My collegue Konstantin (cc-ed) noticed that the GiST code of intarray >> may leak memory in certain index operations: > Can you demonstrate an actual problem here, that is a query-lifespan leak? > IMO, the coding rule in the GiST and GIN AMs is that the AM code is > responsible for running all opclass-supplied functions in suitably > short-lived memory contexts, so that leaks within those functions > don't cause problems. I tried adding "MemoryContextStats(CurrentMemoryContext);" at the top of g_intbig_compress() and running the intarray regression tests (which do reach the pfree in question). This confirmed that the compress function is always called in the "GiST temporary context" made by createTempGistContext. Also, the amount of memory reported as consumed didn't seem to vary when I removed the pfree, which indicates that we do manage to reset that context often enough that leakage here doesn't matter. It's hard to make an exact comparison because of GiST's habit of randomizing page-split decisions, so that the sequence of calls to the compress function isn't identical from one run to the next. But at least in the cases exercised by the regression tests, we do not need that pfree --- and if you believe the comment for createTempGistContext, it would be a GiST bug not an intarray bug if we did. I'll go remove the pfree. Perhaps there is a TODO item here to improve the documentation about these memory management conventions. regards, tom lane
Commits
-
Remove unnecessary pfree() in g_intbig_compress().
- 9f70f6d4c5bc 11.21 landed
- 8d8f3714988b 12.16 landed
- 7fffcc2ee910 13.12 landed
- a6991f763df8 14.9 landed
- 5cb4619896d8 15.4 landed
- e27f3f52c289 16.0 landed
- 8aac9759bb85 17.0 landed