Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 54DE6632712 for ; Wed, 26 May 2010 15:26:21 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 84915-08 for ; Wed, 26 May 2010 18:26:14 +0000 (UTC) X-Greylist: delayed 00:09:50.042126 by SQLgrey-1.7.6 Received: from relay.pcl-ipout01.plus.net (relay.pcl-ipout01.plus.net [212.159.7.99]) by mail.postgresql.org (Postfix) with ESMTP id C9A70632514 for ; Wed, 26 May 2010 15:26:13 -0300 (ADT) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av8EAAMC/UvUOFPl/2dsb2JhbACSDowMccI4hRME Received: from plesk-mail01.plus.net ([212.56.83.229]) by relay.pcl-ipout01.plus.net with ESMTP; 26 May 2010 19:16:21 +0100 Received: (qmail 26445 invoked from network); 26 May 2010 18:16:21 +0000 Received: from host81-147-64-21.range81-147.btcentralplus.com (HELO osiris.mauzo.dyndns.org) (81.147.64.21) by plesk-mail01.plus.net with SMTP; 26 May 2010 18:16:20 +0000 Received: (qmail 55122 invoked by uid 1001); 26 May 2010 18:16:20 -0000 Date: Wed, 26 May 2010 19:16:20 +0100 From: Ben Morrow To: pgsql-sql@postgresql.org Subject: Rules and sequences Message-ID: <20100526181620.GA54996@osiris.mauzo.dyndns.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.701 tagged_above=-5 required=5 tests=BAYES_40=-0.001, RCVD_IN_DNSWL_LOW=-0.7 X-Spam-Level: X-Archive-Number: 201005/114 X-Sequence-Number: 34162 I am trying to implement a fairly standard 'audit table' setup, but using rules instead of triggers (since it should be more efficient). However, I'm running into problems when one of the audited tables has a 'serial' column that is allowed to default: create table foo (id serial, bar text); create table audit (ix bigserial, rec text); create rule audit_insert as on insert to foo do also insert into audit (rec) values ((new.*)::text); insert into foo (bar) values ('baz'); select * from foo; id | bar ----+----- 1 | baz (1 row) select * from audit; ix | rec ----+--------- 1 | (2,baz) (1 row) I can see why this is happening (the rule is essentially a macro, so the NEW expression gets expanded twice, including the nextval call, so the sequence is incremented twice), but is there any way to prevent it? Some way of 'materialising' the NEW row so it is just plain values rather than a list of expressions? Ben