Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id C90ED1337999 for ; Mon, 21 Feb 2011 20:31:08 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 46008-04 for ; Tue, 22 Feb 2011 00:31:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 4098D13364DE for ; Mon, 21 Feb 2011 20:31:01 -0400 (AST) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id p1M0V0RU009876; Mon, 21 Feb 2011 19:31:00 -0500 (EST) To: Maxim Boguk cc: Robert Haas , pgsql-bugs@postgresql.org Subject: Re: BUG #5798: Some weird error with pl/pgsql procedure In-reply-to: <26057.1298311836@sss.pgh.pa.us> References: <201012210248.oBL2m9sp090535@wwwmaster.postgresql.org> <28329.1298131693@sss.pgh.pa.us> <10910.1298236066@sss.pgh.pa.us> <26057.1298311836@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Mon, 21 Feb 2011 13:10:36 -0500" Date: Mon, 21 Feb 2011 19:31:00 -0500 Message-ID: <9875.1298334660@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201102/178 X-Sequence-Number: 29450 I wrote: > Ugh. That quick little "ExecRemoveJunk" is a lot more dangerous than it > looks. I had actually looked at this before, but concluded it was OK > because I couldn't reproduce the problem with a trigger in place. > I guess I wasn't unlucky enough to have the chance pointer equality > occur. On closer inspection, the palloc collision is actually extremely likely, because what will happen is we'll pfree the old tuple and immediately palloc the new one, and if the new one is of sufficiently similar size that it needs the same size of alloc chunk, it's *guaranteed* to get that same chunk back, because of the LIFO free-chunk chains in aset.c. The reason that the problem is hard to reproduce is that most triggers (certainly those written in plpgsql) will give back newly allocated tuples even when you return the unmodified NEW tuple. The only way to expose the problem is for ExecBRUpdateTrigger's trigger-calling loop to not replace the "newtuple", and the easiest way for that to happen is if all the triggers are disabled. So that's why you're seeing it when fooling with the replication-role setting. I was able to reproduce the failure by creating a trigger with a false WHEN condition, and of course there are other ways to prevent a trigger from being called too. regards, tom lane