Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VFWUe-0002Ln-Ez for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Aug 2013 21:44:36 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VFWUd-0005gL-QS for pgsql-bugs@arkaria.postgresql.org; Fri, 30 Aug 2013 21:44:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VFWUc-0005gE-QH for pgsql-bugs@postgresql.org; Fri, 30 Aug 2013 21:44:34 +0000 Received: from moutng.kundenserver.de ([212.227.126.186]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VFWUZ-0005AS-Hh for pgsql-bugs@postgresql.org; Fri, 30 Aug 2013 21:44:34 +0000 Received: from anarazel.de (p54AF4A79.dip0.t-ipconnect.de [84.175.74.121]) by mrelayeu.kundenserver.de (node=mreu2) with ESMTP (Nemesis) id 0MUjoe-1VW7YQ0yR2-00YVK1; Fri, 30 Aug 2013 23:44:30 +0200 Received: by anarazel.de (Postfix, from userid 1000) id 9E2D4100A6D; Fri, 30 Aug 2013 23:44:29 +0200 (CEST) Date: Fri, 30 Aug 2013 23:44:29 +0200 From: Andres Freund To: terje@elde.net Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8410: out of binary heap slots Message-ID: <20130830214429.GE8932@awork2.anarazel.de> References: <20130830210015.GA8932@awork2.anarazel.de> <20130830210525.GB8932@awork2.anarazel.de> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="EeQfGwPcQSOJBaQU" Content-Disposition: inline In-Reply-To: <20130830210525.GB8932@awork2.anarazel.de> X-Provags-ID: V02:K0:iPpWOdEBrrgu835pZ+85PYs350HaswQ54i1IPtIdkgy lhR7RB4Q8U625kyciWbqLbuYvYMbbmI9XQcmGiY1sFJWfnEVE5 f0EC8FP7bKsTcC78F/tQrdUqRUe//p5YSfHjG3sluCpE8HpmYZ uXPMbE3ZUG5tXR0IPp7vxFdKPip5Gxf2TyX2uufqZeKJ1lqhL9 679KFZqVjW7JxpK0Htk3gXr//8SaT07NtTXBlQm9VM1VRnaC1D RADQJnf4FVr/g/bxRM5gRG2yubb5XlKoPuEopJxMwxjI5L+En+ kM3+J5CV4kk3eD/tZwR+oDDCw1aZnfPH024GipK0fvY7xuDujY b9+CqT4gSo9640rR+/HCBYcbzjV5VeI/EtUw67Ndf X-Pg-Spam-Score: -1.9 (-) 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 --EeQfGwPcQSOJBaQU Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On 2013-08-30 23:05:25 +0200, Andres Freund wrote: > On 2013-08-30 23:00:15 +0200, Andres Freund wrote: > > Hi, > > > > > > On 2013-08-30 20:46:27 +0000, terje@elde.net wrote: > > > I'm getting "out of binary heap slots", which offcourse spoils the fun of > > > the query. > > > > > Explain analyze gives this plan (again anonymized a bit, but can send proper > > > off-list): > > > > Since I reviewed the patch that introduced that message, I'd be > > interested in getting that. Ideally in a state where I can reproduce the > > issue in a new cluster. > > No need, found the bug. And I think can build a testcase myself. > > ExecReScanMergeAppend resets ms_initialized, but doesn't clear the > binaryheap. Thus no new elements fit. Ok, patch for that attached. Should we add SELECT (SELECT g.i FROM ((SELECT random()::int ORDER BY 1 OFFSET 0) UNION ALL (SELECT random()::int ORDER BY 1 OFFSET 0)) f(i) ORDER BY f.i LIMIT 1) FROM generate_series(1, 10) g(i); as a regression test? I slightly on the "no" side... Greetings, Andres Freund -- Andres Freund http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --EeQfGwPcQSOJBaQU Content-Type: text/x-patch; charset=us-ascii Content-Disposition: attachment; filename="0001-Reset-the-binary-heap-in-merge-append-rescans.patch" From 1f6eae650b475dfe80868c78fcd805e3d49f0d71 Mon Sep 17 00:00:00 2001 From: Andres Freund Date: Fri, 30 Aug 2013 23:38:40 +0200 Subject: [PATCH] Reset the binary heap in merge append rescans Failing to do so can cause queries to return wrong data, error out or crash. This requires adding a new binaryheap_reset() method to the generic binaryheap implementation. Per bugreport from Terje Elde in bug #8410. --- src/backend/executor/nodeMergeAppend.c | 1 + src/backend/lib/binaryheap.c | 18 ++++++++++++++++-- src/include/lib/binaryheap.h | 1 + 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/backend/executor/nodeMergeAppend.c b/src/backend/executor/nodeMergeAppend.c index 5a48f7a..c3edd61 100644 --- a/src/backend/executor/nodeMergeAppend.c +++ b/src/backend/executor/nodeMergeAppend.c @@ -297,5 +297,6 @@ ExecReScanMergeAppend(MergeAppendState *node) if (subnode->chgParam == NULL) ExecReScan(subnode); } + binaryheap_reset(node->ms_heap); node->ms_initialized = false; } diff --git a/src/backend/lib/binaryheap.c b/src/backend/lib/binaryheap.c index 4b4fc94..14fe24f 100644 --- a/src/backend/lib/binaryheap.c +++ b/src/backend/lib/binaryheap.c @@ -37,16 +37,30 @@ binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg) sz = offsetof(binaryheap, bh_nodes) +sizeof(Datum) * capacity; heap = palloc(sz); - heap->bh_size = 0; heap->bh_space = capacity; - heap->bh_has_heap_property = true; heap->bh_compare = compare; heap->bh_arg = arg; + heap->bh_size = 0; + heap->bh_has_heap_property = true; + return heap; } /* + * binaryheap_reset + * + * Resets the heap to an empty state loosing it's ephemeral state, but not the + * parameters passed at allocation. + */ +void +binaryheap_reset(binaryheap *heap) +{ + heap->bh_size = 0; + heap->bh_has_heap_property = true; +} + +/* * binaryheap_free * * Releases memory used by the given binaryheap. diff --git a/src/include/lib/binaryheap.h b/src/include/lib/binaryheap.h index 1e99e72..85cafe4 100644 --- a/src/include/lib/binaryheap.h +++ b/src/include/lib/binaryheap.h @@ -40,6 +40,7 @@ typedef struct binaryheap extern binaryheap *binaryheap_allocate(int capacity, binaryheap_comparator compare, void *arg); +extern void binaryheap_reset(binaryheap *heap); extern void binaryheap_free(binaryheap *heap); extern void binaryheap_add_unordered(binaryheap *heap, Datum d); extern void binaryheap_build(binaryheap *heap); -- 1.8.2.rc2.4.g7799588.dirty --EeQfGwPcQSOJBaQU 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 --EeQfGwPcQSOJBaQU--