Help with Update Rule on View - 2nd Attempt

From: "Lenorovitz, Joel" <Joel(dot)Lenorovitz(at)usap(dot)gov>
To: <pgsql-general(at)postgresql(dot)org>, <pgsql-novice(at)postgresql(dot)org>
Subject: Help with Update Rule on View - 2nd Attempt
Date: 2006-12-07 19:44:55
Message-ID: 7119BB016BDF6445B20A4B9F14F50B2D44A8CE@WILSON.usap.gov
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-novice


I tried to post this the other day, but didn't get any responses and
never saw it show up in the digest. Here it is again if anyone can
offer any insight:

I'm trying to create a schema in which there will be simple a view for
each table that will have the same columns and can be acted on in the
same way as the underlying table

An example of one table and its view would be:

CREATE TABLE test (id int, text_field varchar(100)); CREATE VIEW _test
AS SELECT * FROM test;

I'd like to be able to create both the views and the insert, update,
delete rules for the views in an automated fashion via a script that
uses the information schema to get all of the table names. All is fine
and good with the insert and delete rules and no problem to
automatically generate this:

CREATE RULE _test_oi_rule AS ON INSERT TO _test DO INSTEAD INSERT INTO
test VALUES (NEW.*); CREATE RULE _test_od_rule AS ON DELETE TO _test DO
INSTEAD DELETE FROM test WHERE id = OLD.id;

However, I'm not sure how to create the update rule without having to go
through the gory task of specifying each column by name. Yes, I could
also use the information schema to automate this as well, but it just
seems ugly. Is there any way to create an update rule that's something
like this:

CREATE RULE _test_ou_rule AS ON UPDATE TO _test SET test.* = NEW.*;
-- or even better a command that will only update changed columns (i.e.,
WHERE NEW.* <> OLD.*)

I imagine I could instead delete the old record and insert the new one,
but that doesn't seem right either and seems like could be perilous.
Maybe I'm overlooking something obvious, but any help to find a nice
clean solution would be appreciated.

Thanks,
JL

Browse pgsql-general by date

  From Date Subject
Next Message Ragnar 2006-12-07 20:07:44 Re: Online index builds
Previous Message Thomas H. 2006-12-07 19:23:32 Re: Vote for your favorite database

Browse pgsql-novice by date

  From Date Subject
Next Message K. Kelly Close 2006-12-08 04:06:41 Re: PostgreSQL back end to Excel ap
Previous Message Daniel T. Staal 2006-12-07 14:37:07 Re: Sort question ! ! !