From: Heikki Linnakangas <hlinnakangas@vmware.com>
To: Pg Bugs <pgsql-bugs@postgresql.org>
Subject: GiST does palloc's in critical section
Date: Wed, 02 Apr 2014 11:22:03 +0300
Message-ID: <533BC8AB.6010401@vmware.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
I just noticed that the GiST functions that write WAL records,
gistXLogSplit and gistXLogUpdate, call palloc while in a critical
section. That's bad because a palloc can fail if you run out of memory,
and if that happens in a critical section, you get a PANIC. It's a small
risk in practice, but we ought to fix it anyway.
The functions went through some refactoring in 9.1, but earlier versions
had the same issue.
Most XLogInsert callers allocate all the memory they need in stack, as
local variables. That's how we should fix these too. We'll need some
upper limit on the amount of memory needed, and use that to size the
variables.
gistXLogSplit needs two XLogRecData for each page in the split. There is
no theoretical limit for that, although in practice more than 3-4 would
be extraordinary. Currently, it's only limited by the number of LWLocks
that can be held simultaneously. Let's add an explicit limit on that.
So, attached is a patch to fix this. This should apply to 9.1 - master
easily, but 9.0 and 8.4 will need some adjustment.
- Heikki
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:fix-gist-palloc-in-crit-section.patch (text/x-diff, 4.8K) downloaddiff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.cindex 99cbcaa..864ec4e 100644--- a/src/backend/access/gist/gist.c+++ b/src/backend/access/gist/gist.c@@ -220,6 +220,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
GistNSN oldnsn = 0;
SplitedPageLayout rootpg;
bool is_rootsplit;
+ int npage;
is_rootsplit = (blkno == GIST_ROOT_BLKNO);
@@ -240,6 +241,15 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
itvec = gistjoinvector(itvec, &tlen, itup, ntup);
dist = gistSplit(rel, page, itvec, tlen, giststate);
+ /* Count the number of pages the original page was split into */+ npage = 0;+ for (ptr = dist; ptr; ptr = ptr->next)+ 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
@@ -402,7 +412,7 @@ gistplacetopage(Relation rel, Size freespace, GISTSTATE *giststate,
/* Write the WAL record */
if (RelationNeedsWAL(rel))
recptr = gistXLogSplit(rel->rd_node, blkno, is_leaf,
- dist, oldrlink, oldnsn, leftchildbuf,+ dist, npage, oldrlink, oldnsn, leftchildbuf,
markfollowright);
else
recptr = gistGetFakeLSN(rel);
diff --git a/src/backend/access/gist/gistxlog.c b/src/backend/access/gist/gistxlog.cindex 17946bf..512c2f5 100644--- a/src/backend/access/gist/gistxlog.c+++ b/src/backend/access/gist/gistxlog.c@@ -375,21 +375,23 @@ gist_xlog_cleanup(void)
*/
XLogRecPtr
gistXLogSplit(RelFileNode node, BlockNumber blkno, bool page_is_leaf,
- SplitedPageLayout *dist,+ SplitedPageLayout *dist, int npage,
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,- cur;+ int cur;
XLogRecPtr recptr;
- 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 (%d, maximum %d)",+ npage, GIST_MAX_SPLIT_PAGES);
xlrec.node = node;
xlrec.origblkno = blkno;
@@ -439,7 +441,6 @@ gistXLogSplit(RelFileNode node, BlockNumber blkno, bool page_is_leaf,
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_SPLIT, rdata);
- pfree(rdata);
return recptr;
}
@@ -462,14 +463,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;
@@ -516,6 +515,5 @@ gistXLogUpdate(RelFileNode node, Buffer buffer,
recptr = XLogInsert(RM_GIST_ID, XLOG_GIST_PAGE_UPDATE, rdata);
- pfree(rdata);
return recptr;
}
diff --git a/src/include/access/gist_private.h b/src/include/access/gist_private.hindex cae6dbc..2d04af6 100644--- a/src/include/access/gist_private.h+++ b/src/include/access/gist_private.h@@ -22,6 +22,22 @@
#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'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
@@ -447,7 +463,7 @@ extern XLogRecPtr gistXLogUpdate(RelFileNode node, Buffer buffer,
extern XLogRecPtr gistXLogSplit(RelFileNode node,
BlockNumber blkno, bool page_is_leaf,
- SplitedPageLayout *dist,+ SplitedPageLayout *dist, int npage,
BlockNumber origrlink, GistNSN oldnsn,
Buffer leftchild, bool markfollowright);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: hlinnakangas@vmware.com
Subject: Re: GiST does palloc's in critical section
In-Reply-To: <533BC8AB.6010401@vmware.com>
References: <533BC8AB.6010401@vmware.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox