Re: Insert Rule

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Curtis Scheer <Curtis(at)DAYCOS(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Insert Rule
Date: 2006-09-01 14:53:58
Message-ID: 4223.1157122438@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Curtis Scheer <Curtis(at)DAYCOS(dot)com> writes:
> However, when I try to insert a record into foo with any other value besides
> 1 it actually inserts the record but doesn't return the # of rows affected.

Works for me:

regression=# create table foo (foovalue int);
CREATE TABLE
regression=# CREATE OR REPLACE RULE rule_foovalue AS ON INSERT TO foo
regression-# WHERE new.foovalue = 1 DO
regression-# select random();
CREATE RULE
regression=# insert into foo values(1);
random
-------------------
0.584614597726613
(1 row)

INSERT 0 1
regression=# insert into foo values(2);
random
--------
(0 rows)

INSERT 0 1
regression=#

Perhaps your client-side software is being distracted by the SELECT
result and not noticing the following INSERT result?

You might be better off casting this as a NOTHING rule to avoid that:

regression=# create function check_for_one(int) returns bool as $$
regression$# begin
regression$# raise notice 'check %', $1;
regression$# return false;
regression$# end$$ language plpgsql;
CREATE FUNCTION
regression=# CREATE OR REPLACE RULE rule_foovalue AS ON INSERT TO foo
WHERE check_for_one(foovalue) DO INSTEAD NOTHING;
CREATE RULE
regression=# insert into foo values(1);
NOTICE: check 1
INSERT 0 1
regression=# insert into foo values(2);
NOTICE: check 2
INSERT 0 1
regression=#

I made my test function just throw a NOTICE so I could verify it got
called, but you could easily make it throw an error instead.

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Martijn van Oosterhout 2006-09-01 14:55:14 Re: [pgsql-advocacy] Thought provoking piece on
Previous Message Csaba Nagy 2006-09-01 14:41:00 Re: Strange error related to temporary tables