Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzlZd-0007wU-2X for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Sep 2018 16:31:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fzlZb-00087a-70 for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Sep 2018 16:31:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzlZb-00087T-0b for pgsql-bugs@lists.postgresql.org; Tue, 11 Sep 2018 16:31:31 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzlZX-0005mW-Ir for pgsql-bugs@lists.postgresql.org; Tue, 11 Sep 2018 16:31:30 +0000 Received: from [192.168.127.1] (port=41192 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1fzlZV-000A4D-Ch; Tue, 11 Sep 2018 16:31:25 +0000 Received: from [127.0.0.1] (port=42085 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1fzlZU-000BBV-O2; Tue, 11 Sep 2018 16:31:24 +0000 From: Andrew Gierth To: Alexander Korotkov Cc: pgsql-bugs@lists.postgresql.org, Teodor Sigaev Subject: Re: BUG #15378: SP-GIST memory context screwup? In-Reply-To: <87y3c8oy1g.fsf@news-spur.riddles.org.uk> (Andrew Gierth's message of "Tue, 11 Sep 2018 16:47:37 +0100") Message-ID: <87tvmwov48.fsf@news-spur.riddles.org.uk> References: <153663176628.23136.11901365223750051490@wrigleys.postgresql.org> <87bm94rba5.fsf@news-spur.riddles.org.uk> <87y3c8oy1g.fsf@news-spur.riddles.org.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Tue, 11 Sep 2018 17:31:24 +0100 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --=-=-= Content-Type: text/plain >>>>> "Andrew" == Andrew Gierth writes: Andrew> So the simplest fix would be to move the memory context reset Andrew> to just after the freeScanStack in resetSpGistScanOpaque. And Andrew> with retail freeing going on during the scan, it makes less Andrew> sense to try and avoid it during rescan, double ugh. Proposed patch attached. -- Andrew (irc:RhodiumToad) --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=spgmem.patch Content-Description: spgist memory fix diff --git a/src/backend/access/spgist/spgscan.c b/src/backend/access/spgist/spgscan.c index c728080812..5260d5017d 100644 --- a/src/backend/access/spgist/spgscan.c +++ b/src/backend/access/spgist/spgscan.c @@ -74,6 +74,13 @@ resetSpGistScanOpaque(SpGistScanOpaque so) freeScanStack(so); + /* + * clear traversal context before proceeding to the next scan; this must + * not happen before the freeScanStack above, else we get double-free + * crashes. + */ + MemoryContextReset(so->traversalCxt); + if (so->searchNulls) { /* Stack a work item to scan the null index entries */ @@ -212,9 +219,6 @@ spgrescan(IndexScanDesc scan, ScanKey scankey, int nscankeys, { SpGistScanOpaque so = (SpGistScanOpaque) scan->opaque; - /* clear traversal context before proceeding to the next scan */ - MemoryContextReset(so->traversalCxt); - /* copy scankeys into local storage */ if (scankey && scan->numberOfKeys > 0) { diff --git a/src/test/regress/expected/spgist.out b/src/test/regress/expected/spgist.out index 2d75bbf8dc..9364b88bc2 100644 --- a/src/test/regress/expected/spgist.out +++ b/src/test/regress/expected/spgist.out @@ -23,6 +23,24 @@ delete from spgist_point_tbl where id % 2 = 1; -- would exercise it) delete from spgist_point_tbl where id < 10000; vacuum spgist_point_tbl; +-- Test rescan paths (cf. bug #15378) +-- use box and && rather than point, so that rescan happens when the +-- traverse stack is non-empty +create table spgist_box_tbl(id serial, b box); +insert into spgist_box_tbl(b) +select box(point(i,j),point(i+s,j+s)) + from generate_series(1,100,5) i, + generate_series(1,100,5) j, + generate_series(1,10) s; +create index spgist_box_idx on spgist_box_tbl using spgist (b); +select count(*) + from (values (point(5,5)),(point(8,8)),(point(12,12))) v(p) + where exists(select * from spgist_box_tbl b where b.b && box(v.p,v.p)); + count +------- + 3 +(1 row) + -- The point opclass's choose method only uses the spgMatchNode action, -- so the other actions are not tested by the above. Create an index using -- text opclass, which uses the others actions. diff --git a/src/test/regress/sql/spgist.sql b/src/test/regress/sql/spgist.sql index 77b43a2d3e..c72cf42a33 100644 --- a/src/test/regress/sql/spgist.sql +++ b/src/test/regress/sql/spgist.sql @@ -30,6 +30,21 @@ delete from spgist_point_tbl where id < 10000; vacuum spgist_point_tbl; +-- Test rescan paths (cf. bug #15378) +-- use box and && rather than point, so that rescan happens when the +-- traverse stack is non-empty + +create table spgist_box_tbl(id serial, b box); +insert into spgist_box_tbl(b) +select box(point(i,j),point(i+s,j+s)) + from generate_series(1,100,5) i, + generate_series(1,100,5) j, + generate_series(1,10) s; +create index spgist_box_idx on spgist_box_tbl using spgist (b); + +select count(*) + from (values (point(5,5)),(point(8,8)),(point(12,12))) v(p) + where exists(select * from spgist_box_tbl b where b.b && box(v.p,v.p)); -- The point opclass's choose method only uses the spgMatchNode action, -- so the other actions are not tested by the above. Create an index using --=-=-=--