postgresql/src/include/access
Tom Lane bde361fef5 Fix memory leak and other bugs in ginPlaceToPage() & subroutines.
Commit 36a35c550a turned the interface between ginPlaceToPage and
its subroutines in gindatapage.c and ginentrypage.c into a royal mess:
page-update critical sections were started in one place and finished in
another place not even in the same file, and the very same subroutine
might return having started a critical section or not.  Subsequent patches
band-aided over some of the problems with this design by making things
even messier.

One user-visible resulting problem is memory leaks caused by the need for
the subroutines to allocate storage that would survive until ginPlaceToPage
calls XLogInsert (as reported by Julien Rouhaud).  This would not typically
be noticeable during retail index updates.  It could be visible in a GIN
index build, in the form of memory consumption swelling to several times
the commanded maintenance_work_mem.

Another rather nasty problem is that in the internal-page-splitting code
path, we would clear the child page's GIN_INCOMPLETE_SPLIT flag well before
entering the critical section that it's supposed to be cleared in; a
failure in between would leave the index in a corrupt state.  There were
also assorted coding-rule violations with little immediate consequence but
possible long-term hazards, such as beginning an XLogInsert sequence before
entering a critical section, or calling elog(DEBUG) inside a critical
section.

To fix, redefine the API between ginPlaceToPage() and its subroutines
by splitting the subroutines into two parts.  The "beginPlaceToPage"
subroutine does what can be done outside a critical section, including
full computation of the result pages into temporary storage when we're
going to split the target page.  The "execPlaceToPage" subroutine is called
within a critical section established by ginPlaceToPage(), and it handles
the actual page update in the non-split code path.  The critical section,
as well as the XLOG insertion call sequence, are both now always started
and finished in ginPlaceToPage().  Also, make ginPlaceToPage() create and
work in a short-lived memory context to eliminate the leakage problem.
(Since a short-lived memory context had been getting created in the most
common code path in the subroutines, this shouldn't cause any noticeable
performance penalty; we're just moving the overhead up one call level.)

In passing, fix a bunch of comments that had gone unmaintained throughout
all this klugery.

Report: <571276DD.5050303@dalibo.com>
2016-04-20 14:25:15 -04:00
..
amapi.h Revert CREATE INDEX ... INCLUDING ... 2016-04-08 21:52:13 +03:00
amvalidate.h Improve index AMs' opclass validation procedures. 2016-01-21 19:47:15 -05:00
attnum.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
brin.h Restructure index access method API to hide most of it at the C level. 2016-01-17 19:36:59 -05:00
brin_internal.h Restructure index access method API to hide most of it at the C level. 2016-01-17 19:36:59 -05:00
brin_page.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
brin_pageops.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
brin_revmap.h Add the "snapshot too old" feature 2016-04-08 14:36:30 -05:00
brin_tuple.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
brin_xlog.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
clog.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
commit_ts.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
genam.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
generic_xlog.h Improve API of GenericXLogRegister(). 2016-04-12 11:42:06 -04:00
gin.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
gin_private.h Fix memory leak and other bugs in ginPlaceToPage() & subroutines. 2016-04-20 14:25:15 -04:00
gist.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
gist_private.h Restructure index access method API to hide most of it at the C level. 2016-01-17 19:36:59 -05:00
gistscan.h Restructure index access method API to hide most of it at the C level. 2016-01-17 19:36:59 -05:00
hash.h Restructure index access method API to hide most of it at the C level. 2016-01-17 19:36:59 -05:00
heapam.h Change the format of the VM fork to add a second bit per page. 2016-03-01 21:49:41 -05:00
heapam_xlog.h Change the format of the VM fork to add a second bit per page. 2016-03-01 21:49:41 -05:00
hio.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
htup.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
htup_details.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
itup.h Revert CREATE INDEX ... INCLUDING ... 2016-04-08 21:52:13 +03:00
multixact.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
nbtree.h Add the "snapshot too old" feature 2016-04-08 14:36:30 -05:00
parallel.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
printtup.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
reloptions.h Restructure index access method API to hide most of it at the C level. 2016-01-17 19:36:59 -05:00
relscan.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
rewriteheap.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
rmgr.h Add rmgr callback to name xlog record types for display purposes. 2014-09-19 16:20:29 +02:00
rmgrlist.h Generic Messages for Logical Decoding 2016-04-06 10:05:41 +01:00
sdir.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
skey.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
slru.h Make all built-in lwlock tranche IDs fixed. 2016-02-02 06:45:55 -05:00
spgist.h Introduce traversalValue for SP-GiST scan 2016-03-30 18:29:28 +03:00
spgist_private.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
stratnum.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
subtrans.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
sysattr.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
timeline.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
transam.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
tsmapi.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
tupconvert.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
tupdesc.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
tupmacs.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
tuptoaster.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
twophase.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
twophase_rmgr.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
valid.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
visibilitymap.h Change the format of the VM fork to add a second bit per page. 2016-03-01 21:49:41 -05:00
xact.h Add new replication mode synchronous_commit = 'remote_apply'. 2016-03-29 21:29:49 -04:00
xlog.h Implement backup API functions for non-exclusive backups 2016-04-05 20:03:49 +02:00
xlog_fn.h Implement backup API functions for non-exclusive backups 2016-04-05 20:03:49 +02:00
xlog_internal.h Remove trailing commas in enums. 2016-04-14 19:25:16 -07:00
xlogdefs.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
xloginsert.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
xlogreader.h Enable logical slots to follow timeline switches 2016-03-30 20:07:05 -03:00
xlogrecord.h Update copyright for 2016 2016-01-02 13:33:40 -05:00
xlogutils.h XLogReader general code cleanup 2016-03-30 18:56:13 -03:00