Fix query-duration memory leak with GIN rescans.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 30 Jan 2015 16:58:23 +0000 (17:58 +0100)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Fri, 30 Jan 2015 16:58:23 +0000 (17:58 +0100)
commit68fa75f3188c050ec62804f2bfacd3ea85404743
tree523949956426b00a1d401c38228bfceaa48e123c
parentcff1bd2a3c36fdf014367dd97f140495ef6ed47e
Fix query-duration memory leak with GIN rescans.

The requiredEntries / additionalEntries arrays were not freed in
freeScanKeys() like other per-key stuff.

It's not obvious, but startScanKey() was only ever called after the keys
have been initialized with ginNewScanKey(). That's why it doesn't need to
worry about freeing existing arrays. The ginIsNewKey() test in gingetbitmap
was never true, because ginrescan free's the existing keys, and it's not OK
to call gingetbitmap twice in a row without calling ginrescan in between.
To make that clear, remove the unnecessary ginIsNewKey(). And just to be
extra sure that nothing funny happens if there is an existing key after all,
call freeScanKeys() to free it if it exists. This makes the code more
straightforward.

(I'm seeing other similar leaks in testing a query that rescans an GIN index
scan, but that's a different issue. This just fixes the obvious leak with
those two arrays.)

Backpatch to 9.4, where GIN fast scan was added.
src/backend/access/gin/ginget.c
src/backend/access/gin/ginscan.c
src/include/access/gin_private.h