Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id CA2A23405C8 for ; Wed, 22 Feb 2012 11:32:29 -0400 (AST) Received: from mail-pz0-f46.google.com ([209.85.210.46]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1S0EBA-0000mT-MN for pgsql-sql@postgresql.org; Wed, 22 Feb 2012 15:32:29 +0000 Received: by daed14 with SMTP id d14so133059dae.19 for ; Wed, 22 Feb 2012 07:32:16 -0800 (PST) Received-SPF: pass (google.com: domain of adrian.klaver@gmail.com designates 10.68.226.41 as permitted sender) client-ip=10.68.226.41; Authentication-Results: mr.google.com; spf=pass (google.com: domain of adrian.klaver@gmail.com designates 10.68.226.41 as permitted sender) smtp.mail=adrian.klaver@gmail.com; dkim=pass header.i=adrian.klaver@gmail.com Received: from mr.google.com ([10.68.226.41]) by 10.68.226.41 with SMTP id rp9mr80904673pbc.162.1329924736071 (num_hops = 1); Wed, 22 Feb 2012 07:32:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=from:to:subject:date:user-agent:cc:references:in-reply-to :mime-version:content-type:content-transfer-encoding:message-id; bh=VomAYq8tq5Y9Gt6AVVxBAxvpF6vPbDG7Z+szS0dIUxA=; b=mI+xDFGofD2hPy/8GATL7Q5+T6+rzD6CgKcrO2npUWEPBcoA134YHEueA86VLJZsdQ 0oti40saE8tL0ChkpZjFyTReYkmnvwe+AalFNFENSmz+os7KHZ4rOV9qFzQPqONaK0I0 tH+E1wj/RCV+Qijbo0/wXnVJqJg4b6CtNDSD4= Received: by 10.68.226.41 with SMTP id rp9mr67191458pbc.162.1329924735990; Wed, 22 Feb 2012 07:32:15 -0800 (PST) Received: from tucker.localnet (c-24-17-164-54.hsd1.wa.comcast.net. [24.17.164.54]) by mx.google.com with ESMTPS id o4sm30796474pbq.16.2012.02.22.07.32.13 (version=SSLv3 cipher=OTHER); Wed, 22 Feb 2012 07:32:14 -0800 (PST) From: Adrian Klaver To: pgsql-sql@postgresql.org Subject: Re: on insert rule with default value Date: Wed, 22 Feb 2012 07:32:10 -0800 User-Agent: KMail/1.13.5 (Linux/2.6.32-38-generic; KDE/4.4.5; i686; ; ) Cc: Ron Peterson References: <20120221205130.GG29194@mtholyoke.edu> <20120222135239.GI29194@mtholyoke.edu> In-Reply-To: <20120222135239.GI29194@mtholyoke.edu> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201202220732.10518.adrian.klaver@gmail.com> X-Pg-Spam-Score: -2.6 (--) X-Archive-Number: 201202/73 X-Sequence-Number: 36366 On Wednesday, February 22, 2012 5:52:39 am Ron Peterson wrote: > 2012-02-21_15:51:30-0500 Ron Peterson : > > My rule below does not insert the the same uuid value into the test_log > > table as is created in the test table when I insert a new value. I know > > I've worked through this before, but I'm not remembering why this is. > > What's a right way to do this? > > Obviously I can use a trigger function. I'm mostly wondering if there > are any tricks to accomplishing this with rules (I like the simple > syntax). I suspect the problem here is that 'new' on insert refers to > the function used to calculate the new value, /not/ the new value that > is actually inserted into the table. There are probably reasons for > that; but it would seem nicer to refer to the actual new table value > rather than the expression used to calculate it. My 2c. The simplest explanation I could find is here: http://www.postgresql.org/docs/9.0/interactive/querytree.html "the target list... For INSERT commands, the target list describes the new rows that should go into the result relation. It consists of the expressions in the VALUES clause or the ones from the SELECT clause in INSERT ... SELECT. The first step of the rewrite process adds target list entries for any columns that were not assigned to by the original command but have defaults. Any remaining columns (with neither a given value nor a default) will be filled in by the planner with a constant null expression. " If you want all the gory details read through section 37, in particular 37.3 :) The above is why I use triggers now. It is a lot easier to follow the logic in a trigger than in a rule. > > -Ron- -- Adrian Klaver adrian.klaver@gmail.com