]> granicus.if.org Git - postgresql/commitdiff
Avoid palloc in critical section in GiST WAL-logging.
authorHeikki Linnakangas <heikki.linnakangas@iki.fi>
Thu, 3 Apr 2014 12:09:37 +0000 (15:09 +0300)
committerHeikki Linnakangas <heikki.linnakangas@iki.fi>
Thu, 3 Apr 2014 12:44:09 +0000 (15:44 +0300)
Memory allocation can fail if you run out of memory, and inside a critical
section that will lead to a PANIC. Use conservatively-sized arrays in stack
instead.

There was previously no explicit limit on the number of pages a GiST split
can produce, it was only limited by the number of LWLocks that can be held
simultaneously (100 at the moment). This patch adds an explicit limit of 75
pages. That should be plenty, a typical split shouldn't produce more than
2-3 page halves.

The bug has been there forever, but only backpatch down to 9.1. The code
was changed significantly in 9.1, and it doesn't seem worth the risk or
trouble to adapt this for 9.0 and 8.4.

src/backend/access/gist/README
src/backend/access/gist/gist.c
src/backend/access/gist/gistxlog.c
src/include/access/gist_private.h

index 4bcac1f2c795d07874bb2f6da0413e4d684e5496..dd4c9fa70a028a894c73eac1917c2cc9c3d4f97e 100644 (file)
@@ -135,7 +135,7 @@ that didn't need to be split.
 
 This differs from the insertion algorithm in the original paper. In the
 original paper, you first walk down the tree until you reach a leaf page, and
-then you adjust the downlink in the parent, and propagating the adjustment up,
+then you adjust the downlink in the parent, and propagate the adjustment up,
 all the way up to the root in the worst case. But we adjust the downlinks to
 cover the new key already when we walk down, so that when we reach the leaf
 page, we don't need to update the parents anymore, except to insert the
index 7ccf5d5966f3730a2a920127cd47a52294e90383..c0011a3bbaa25a08bb3729e94efb8f40521d3f17 100644 (file)
@@ -206,6 +206,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
                GistNSN         oldnsn = {0, 0};
                SplitedPageLayout rootpg;
                bool            is_rootsplit;
+               int                     npage;
 
                is_rootsplit = (blkno == GIST_ROOT_BLKNO);
 
@@ -226,6 +227,19 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
                itvec = gistjoinvector(itvec, &tlen, itup, ntup);
                dist = gistSplit(rel, page, itvec, tlen, giststate);
 
+               /*
+                * Check that split didn't produce too many pages.
+                */
+               npage = 0;
+               for (ptr = dist; ptr; ptr = ptr->next)
+                       npage++;
+               /* in a root split, we'll add one more page to the list below */
+               if (is_rootsplit)
+                       npage++;
+               if (npage > GIST_MAX_SPLIT_PAGES)
+                       elog(ERROR, "GiST page split into too many halves (%d, maximum %d)",
+                                npage, GIST_MAX_SPLIT_PAGES);
+
                /*
                 * Set up pages to work with. Allocate new buffers for all but the
                 * leftmost page. The original page becomes the new leftmost page, and
index 4440499d48ab004ff9205842f66c38ac2657801a..8cdd78c83aaf91c7191f0b4673babe97c11702ff 100644 (file)
@@ -432,7 +432,7 @@ gistXLogSplit(RelFileNode node, BlockNumber blkno, bool page_is_leaf,
                          BlockNumber origrlink, GistNSN orignsn,
                          Buffer leftchildbuf, bool markfollowright)
 {
-       XLogRecData *rdata;
+       XLogRecData rdata[GIST_MAX_SPLIT_PAGES * 2 + 2];
        gistxlogPageSplit xlrec;
        SplitedPageLayout *ptr;
        int                     npage = 0,
@@ -441,8 +441,12 @@ gistXLogSplit(RelFileNode node, BlockNumber blkno, bool page_is_leaf,
 
        for (ptr = dist; ptr; ptr = ptr->next)
                npage++;
-
-       rdata = (XLogRecData *) palloc(sizeof(XLogRecData) * (npage * 2 + 2));
+       /*
+        * the caller should've checked this already, but doesn't hurt to check
+        * again.
+        */
+       if (npage > GIST_MAX_SPLIT_PAGES)
+               elog(ERROR, "GiST page split into too many halves");
 
        xlrec.node = node;
        xlrec.origblkno = blkno;
@@ -492,7 +496,6 @@ gistXLogSplit(RelFileNode node, BlockNumber blkno, bool page_is_leaf,
 
        recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_SPLIT, rdata);
 
-       pfree(rdata);
        return recptr;
 }
 
@@ -515,14 +518,12 @@ gistXLogUpdate(RelFileNode node, Buffer buffer,
                           IndexTuple *itup, int ituplen,
                           Buffer leftchildbuf)
 {
-       XLogRecData *rdata;
+       XLogRecData rdata[MaxIndexTuplesPerPage + 3];
        gistxlogPageUpdate xlrec;
        int                     cur,
                                i;
        XLogRecPtr      recptr;
 
-       rdata = (XLogRecData *) palloc(sizeof(XLogRecData) * (3 + ituplen));
-
        xlrec.node = node;
        xlrec.blkno = BufferGetBlockNumber(buffer);
        xlrec.ntodelete = ntodelete;
@@ -569,6 +570,5 @@ gistXLogUpdate(RelFileNode node, Buffer buffer,
 
        recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE, rdata);
 
-       pfree(rdata);
        return recptr;
 }
index d9b1fa2fec59111c0f1e2db950e8a6a9e8e61ba7..53f794f3b9e083b70c5d371966eacd00f6778b59 100644 (file)
 #include "utils/rbtree.h"
 #include "utils/hsearch.h"
 
+/*
+ * Maximum number of "halves" a page can be split into in one operation.
+ * Typically a split produces 2 halves, but can be more if keys have very
+ * different lengths, or when inserting multiple keys in one operation (as
+ * when inserting downlinks to an internal node).  There is no theoretical
+ * limit on this, but in practice if you get more than a handful page halves
+ * in one split, there's something wrong with the opclass implementation.
+ * GIST_MAX_SPLIT_PAGES is an arbitrary limit on that, used to size some
+ * local arrays used during split.  Note that there is also a limit on the
+ * number of buffers that can be held locked at a time, MAX_SIMUL_LWLOCKS,
+ * so if you raise this higher than that limit, you'll just get a different
+ * error.
+ */
+#define GIST_MAX_SPLIT_PAGES           75
+
 /* Buffer lock modes */
 #define GIST_SHARE     BUFFER_LOCK_SHARE
 #define GIST_EXCLUSIVE BUFFER_LOCK_EXCLUSIVE