Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8Kuk-00047h-HM for pgsql-sql@arkaria.postgresql.org; Thu, 21 Feb 2013 01:25:34 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U8Kuk-00036K-1D for pgsql-sql@arkaria.postgresql.org; Thu, 21 Feb 2013 01:25:34 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8Kuj-000367-5N for pgsql-sql@postgresql.org; Thu, 21 Feb 2013 01:25:33 +0000 Received: from isis.morrow.me.uk ([204.109.63.142]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U8Kud-0000Sw-Ui for pgsql-sql@postgresql.org; Thu, 21 Feb 2013 01:25:31 +0000 Received: from anubis.morrow.me.uk (host109-150-212-220.range109-150.btcentralplus.com [109.150.212.220]) (Authenticated sender: mauzo) by isis.morrow.me.uk (Postfix) with ESMTPSA id 88F07450CD; Thu, 21 Feb 2013 01:25:26 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.7.4 isis.morrow.me.uk 88F07450CD DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=morrow.me.uk; s=dkim201101; t=1361409926; bh=euAf9RL1hDhc1r/Ii7++Qpvfykz5EAz9Ok0Eq9pNWRU=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=afKyGXqKoQfBqqHe7PKgInshGEcN/a4qVM1nY4FgyoxM3GTfu34ajS2zJRLCJwBl8 nP0QHPjVnWwrPL5HnZ11CKGsC/M/lxRvGUDdnHkEljCIPQm9u4u7A3Cy/58v+6jR6p PW1I9AdQ8de4jLRq5oDnRvDL+Ayfy13ircCNS19Y= X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.97.6 at isis.morrow.me.uk Received: by anubis.morrow.me.uk (Postfix, from userid 5001) id 84F3E9A7B; Thu, 21 Feb 2013 01:25:24 +0000 (GMT) Date: Thu, 21 Feb 2013 01:25:24 +0000 From: Ben Morrow To: Sergey Konoplev Cc: pgsql-sql Subject: Re: Volatile functions in WITH Message-ID: <20130221012523.GD29651@anubis.morrow.me.uk> References: <20130217075859.GE8029@anubis.morrow.me.uk> <20130220081905.GA95525@anubis.morrow.me.uk> <20130220181603.GB29651@anubis.morrow.me.uk> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org At 12PM -0800 on 20/02/13 you (Sergey Konoplev) wrote: > On Wed, Feb 20, 2013 at 10:16 AM, Ben Morrow wrote: > >> If you got mixed up with plpgsql anyway what is the reason of making > >> this WITH query constructions instead of implementing everything in a > >> plpgsql trigger on DELETE on exp then? > > > > I'm not sure what you mean. "exp" isn't a table, it's a WITH CTE. The > > Sorry, I meant "item" of course, "exp" was a typo. OK. > > statement is deleting some entries from "item", and replacing some of > > them with new entries, based on the information in the "item_expired" > > view. I can't do anything with a trigger on "item", since there are > > other circumstances where items are deleted that shouldn't trigger > > replacement. > > Okay, I see. > > If the case is specific you can make a simple plpgsql function that > will process it like FOR _row IN DELETE ... RETORNING * LOOP ... > RETURN NEXT _row; END LOOP; Yes, I *know* I can write a function if I have to. I can also send the whole lot down to the client and do the inserts from there, or use a temporary table. I was hoping to avoid that, since the plain INSERT case works perfectly well. > > select * > > from (select j.type, random() r from item j) i > > where i.type = 1 > > > > the planner will transform it into > > > > select i.type, random() r > > from item i > > where i.type = 1 > > > > before planning, so even though random() is volatile it will only get > > called for rows of item with type = 1. > > Yes, functions are executed depending on the resulting plan "A query > using a volatile function will re-evaluate the function at every row > where its value is needed". > > > I don't know if this happens, or may sometimes happen, or might happen > > in the future, for rows eliminated because of DISTINCT. > > It is a good point. Nothing guarantees it in a perspective. Optimizer > guarantees a stable result but not the way it is reached. Well, it makes functions which perform DML a lot less useful, so I wonder whether this is intentional behaviour. Ben -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql