Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WVGRH-00044a-Tp for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Apr 2014 08:22:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WVGRF-000167-Vq for pgsql-bugs@arkaria.postgresql.org; Wed, 02 Apr 2014 08:22:26 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WVGRE-000160-Uw for pgsql-bugs@postgresql.org; Wed, 02 Apr 2014 08:22:25 +0000 Received: from smtp-outbound-1.vmware.com ([208.91.2.12]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WVGR3-0007Sr-GE for pgsql-bugs@postgresql.org; Wed, 02 Apr 2014 08:22:21 +0000 Received: from sc9-mailhost2.vmware.com (sc9-mailhost2.vmware.com [10.113.161.72]) by smtp-outbound-1.vmware.com (Postfix) with ESMTP id A1E952877B for ; Wed, 2 Apr 2014 01:22:06 -0700 (PDT) Received: from zcs-prod-ext-mta-1.vmware.com (zcs-prod-ext-mta-1.vmware.com [10.113.62.223]) by sc9-mailhost2.vmware.com (Postfix) with ESMTP id 94D22B12AC for ; Wed, 2 Apr 2014 01:22:06 -0700 (PDT) Received: from zcs-prod-ext-mta-1.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTP id D1FA7120030 for ; Wed, 2 Apr 2014 01:22:00 -0700 (PDT) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-1.vmware.com (Postfix) with ESMTPSA for ; Wed, 2 Apr 2014 01:21:59 -0700 (PDT) Message-ID: <533BC8AB.6010401@vmware.com> Date: Wed, 02 Apr 2014 11:22:03 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20131103 Icedove/17.0.10 MIME-Version: 1.0 To: Pg Bugs Subject: GiST does palloc's in critical section Content-Type: multipart/mixed; boundary="------------000301060503060205000001" X-Pg-Spam-Score: -7.5 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------000301060503060205000001 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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 --------------000301060503060205000001 Content-Type: text/x-diff; name="fix-gist-palloc-in-crit-section.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="fix-gist-palloc-in-crit-section.patch" diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c index 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.c index 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.h index 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); --------------000301060503060205000001 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------000301060503060205000001--