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 1fzZ7M-0006dX-AI for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Sep 2018 03:13:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fzZ7K-00039A-7D for pgsql-bugs@arkaria.postgresql.org; Tue, 11 Sep 2018 03:13:30 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzZ7J-000393-Tm for pgsql-bugs@lists.postgresql.org; Tue, 11 Sep 2018 03:13:30 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fzZ7G-0007aO-H6 for pgsql-bugs@lists.postgresql.org; Tue, 11 Sep 2018 03:13:28 +0000 Received: from [192.168.127.1] (port=17720 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 1fzZ7D-0009GG-JF; Tue, 11 Sep 2018 03:13:23 +0000 Received: from [127.0.0.1] (port=44393 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1fzZ7C-0006BB-N4; Tue, 11 Sep 2018 03:13:22 +0000 From: Andrew Gierth To: pgsql-bugs@lists.postgresql.org, Teodor Sigaev Subject: Re: BUG #15378: SP-GIST memory context screwup? In-Reply-To: <153663176628.23136.11901365223750051490@wrigleys.postgresql.org> (PG Bug reporting form's message of "Tue, 11 Sep 2018 02:09:26 +0000") Message-ID: <87bm94rba5.fsf@news-spur.riddles.org.uk> References: <153663176628.23136.11901365223750051490@wrigleys.postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Tue, 11 Sep 2018 04:13:21 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk [CCing Teodor as committer of ccd6eb49a] >>>>> "PG" == PG Bug reporting form writes: PG> I found this while analyzing a report from IRC that initially PG> looked like a PostGIS bug, but which I now think is breakage in PG> spgist: PG> spgrescan starts out by doing PG> MemoryContextReset(so->traversalCxt); PG> then later it calls resetSpGistScanOpaque(so); PG> which calls freeScanStack(so) PG> which calls freeScanStackEntry(so) PG> which does: PG> if (stackEntry->traversalValue) PG> pfree(stackEntry->traversalValue); PG> But stackEntry->traversalValue, if not NULL, is supposed to have PG> been allocated in so->traversalCxt, and so it's already gone. [...] PG> Unfortunately I don't think this can be demonstrated with the PG> built-in spgist opclasses, which don't allocate traversalValues. Turns out I was looking in the wrong place, and in fact we can reproduce this easily (at least on an assert build): create table boxes (b box); insert into boxes 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 on boxes using spgist (b); select * from (values (box(point(5,5),point(8,8))),(box(point(2,2),point(12,12)))) v(b) cross join lateral (select * from boxes where boxes.b && v.b limit 1) pt; So this logic was added in ccd6eb49a and was wrong from the start. Testing suggests that removing the offending pfree does indeed fix the issue; any objections? -- Andrew (irc:RhodiumToad)