X-Original-To: pgsql-admin-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 17B43329C83 for ; Wed, 22 Sep 2004 16:27:31 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 13958-08 for ; Wed, 22 Sep 2004 15:27:26 +0000 (GMT) Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by svr1.postgresql.org (Postfix) with ESMTP id 72426329C6B for ; Wed, 22 Sep 2004 16:27:25 +0100 (BST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.1/8.13.1) with ESMTP id i8MFQirM049264 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 22 Sep 2004 09:26:47 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.1/8.13.1) with ESMTP id i8MFQi5A099279; Wed, 22 Sep 2004 09:26:44 -0600 (MDT) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.1/8.13.1/Submit) id i8MFQhS8099278; Wed, 22 Sep 2004 09:26:43 -0600 (MDT) (envelope-from mfuhr) Date: Wed, 22 Sep 2004 09:26:43 -0600 From: Michael Fuhr To: Bradley Kieser Cc: pgsql-admin@postgresql.org Subject: Re: Getting the OID of inserted row in a rule Message-ID: <20040922152643.GA92429@winnie.fuhr.org> References: <41517C66.9040709@kieser.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <41517C66.9040709@kieser.net> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200409/206 X-Sequence-Number: 14909 On Wed, Sep 22, 2004 at 02:21:42PM +0100, Bradley Kieser wrote: > I have a rule on a view that needs to insert into two tables. The one > table has a serial ID as its unique key. The second table links to the > first one in one of its columns. > > I would prefer to keep this as a rule-based solution and not have to > write a function as I hope to relicate the solution across many views. > > I need to either be able to select nextval() the ID for the first table > and somehow store this in the rule (but I don't see that rules support > variables) or I need to somehow obtain the OID from the first insert in > order to select back the ID that was assigned during the insert and pass > it to the second insert (trivial to do as the second insert is then > simply a select-based insert, provided that I know the OID of that first > row!). The second insert should be able to use currval() to get the current value of the sequence used in the first insert. Here's an example: CREATE RULE v_ins AS ON INSERT TO v DO INSTEAD ( INSERT INTO a (xxx) VALUES (NEW.xxx); INSERT INTO b (col1, col2, col3) VALUES (NEW.col1, NEW.col2, currval('a_id_seq')) ); -- Michael Fuhr http://www.fuhr.org/~mfuhr/