pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Rules aren't doing what I expect
5+ messages / 3 participants
[nested] [flat]

* Rules aren't doing what I expect
@ 2000-08-09 16:04  Mark Volpe <volpe.mark@epamail.epa.gov>
  0 siblings, 2 replies; 5+ messages in thread

From: Mark Volpe @ 2000-08-09 16:04 UTC (permalink / raw)
  To: pgsql-sql

Hi again,

I have a table with a trigger that can potentially modify a row before it gets
inserted or updated:

	CREATE TABLE t1 (a int);
	CREATE FUNCTION t1_validate() RETURNS opaque AS
	'
		BEGIN
		IF (NEW.a>10) THEN NEW.a=10; END IF;
		IF (NEW.a<0) THEN NEW.a=0; END IF;
		RETURN NEW;
		END;
	' LANGUAGE 'plpgsql';
	CREATE TRIGGER t1_trig BEFORE INSERT OR UPDATE
		ON t1 FOR EACH ROW EXECUTE PROCEDURE t1_validate();

I have another table that tracks changes in the first table with rules:

	CREATE TABLE t1_log (old_a int, new_a int);
	CREATE RULE t1_insert AS ON INSERT TO t1
		DO INSERT INTO t1_log VALUES(NULL, NEW.a);
	CREATE RULE t1_update AS ON UPDATE TO t1
		DO INSERT INTO t1_log VALUES(OLD.a, NEW.a);
	CREATE RULE t1_delete AS ON DELETE TO t1
		DO INSERT INTO t1_log VALUES(OLD.a, NULL);

When I try this out, however, the rule seems to use the original value, rather
than the "corrected" value.

	INSERT INTO t1 VALUES(2);
	INSERT INTO t1 VALUES(8);
	INSERT INTO t1 VALUES(15);
	SELECT * FROM t1;

	 a  
	----
	  2
	  8
	 10

The table t1 shows the corrected value of 10, but,

	SELECT * FROM FROM t1_log;
	
	 old_a | new_a 
	-------+-------
	       |     2
	       |     8
	       |    15
	
The t1_log table doesn't show what was actually inserted into t1!
Are there any changes I can make to the logic above so that t1_log can
show the correct value?

Thanks,
Mark



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Rules aren't doing what I expect
@ 2000-08-10 02:18  Ang Chin Han <angch@pintoo.com>
  parent: Mark Volpe <volpe.mark@epamail.epa.gov>
  1 sibling, 1 reply; 5+ messages in thread

From: Ang Chin Han @ 2000-08-10 02:18 UTC (permalink / raw)
  To: Mark Volpe <volpe.mark@epamail.epa.gov>; +Cc: pgsql-sql

On Wed, Aug 09, 2000 at 12:04:13PM -0400, Mark Volpe wrote:

> I have a table with a trigger that can potentially modify a row before it gets
> inserted or updated:

[snip]

> I have another table that tracks changes in the first table with rules:

AFAIK, rules get rewritten first, before triggers are invoked,
so your rules are getting the values before your trigger changes them.

> The t1_log table doesn't show what was actually inserted into t1!
> Are there any changes I can make to the logic above so that t1_log can
> show the correct value?

Either somehow rewrite your trigger as a rule, or stick another
trigger to change the value before getting into your log table.
You might be able to reuse your trigger function, I think, just
point the trigger to that function. A bit inefficient, since it
gets called twice.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Rules aren't doing what I expect
@ 2000-08-10 14:22  Mark Volpe <volpe.mark@epamail.epa.gov>
  parent: Ang Chin Han <angch@pintoo.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Mark Volpe @ 2000-08-10 14:22 UTC (permalink / raw)
  To: Ang Chin Han <angch@pintoo.com>; +Cc: pgsql-sql

The actual trigger function I'm working with is over two screens long and
rather expensive to be calling twice! Perhaps I need to add another trigger
that updates the log table with the correct values after the fact. Recursive
triggers, fun! Thanks for the help,

Mark

Ang Chin Han wrote:
> 
> Either somehow rewrite your trigger as a rule, or stick another
> trigger to change the value before getting into your log table.
> You might be able to reuse your trigger function, I think, just
> point the trigger to that function. A bit inefficient, since it
> gets called twice.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Rules aren't doing what I expect
@ 2000-08-11 14:10  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Mark Volpe <volpe.mark@epamail.epa.gov>
  1 sibling, 1 reply; 5+ messages in thread

From: Tom Lane @ 2000-08-11 14:10 UTC (permalink / raw)
  To: Mark Volpe <volpe.mark@epamail.epa.gov>; +Cc: pgsql-sql

Mark Volpe <volpe.mark@epamail.epa.gov> writes:
> When I try this out, however, the rule seems to use the original
> value, rather than the "corrected" value.

Queries added by non-INSTEAD rules are always performed before the
initially-given query, so you're right, the rule will see the unmodified
value.

I'd suggest folding the log-entry-making into your trigger, actually.
If you have a trigger anyway then the insert into t1_log is only one
more line in that trigger procedure...

			regards, tom lane



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: Rules aren't doing what I expect
@ 2000-08-11 16:03  Mark Volpe <volpe.mark@epamail.epa.gov>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 5+ messages in thread

From: Mark Volpe @ 2000-08-11 16:03 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-sql

Tom Lane wrote:
> 
> Queries added by non-INSTEAD rules are always performed before the
> initially-given query, so you're right, the rule will see the unmodified
> value.
> 
> I'd suggest folding the log-entry-making into your trigger, actually.
> If you have a trigger anyway then the insert into t1_log is only one
> more line in that trigger procedure...
> 
>                         regards, tom lane

Thanks for the explanation, Tom.  I left out part of my story though. :)  I
would like normal users to be able to modify t1 but not t1_log, and doing what
you said would require INSERT permission on t1_log. So what I did was go ahead
and allow INSERT permission, but create before and after triggers on t1_log
that check the inserted values against reality... but that brings up another
question -

If I do an UPDATE on t1, it calls a trigger which eventually does:
INSERT INTO t1_log VALUES(OLD.a, NEW.a);

If t1_log has before and after triggers, the before trigger will always see
the old row in t1, and the after trigger will always see the new data, right?
At least that's what I'm seeing.  The "visibility of data changes" document
was kinda confusing...

Mark



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2000-08-11 16:03 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2000-08-09 16:04 Rules aren't doing what I expect Mark Volpe <volpe.mark@epamail.epa.gov>
2000-08-10 02:18 ` Ang Chin Han <angch@pintoo.com>
2000-08-10 14:22   ` Mark Volpe <volpe.mark@epamail.epa.gov>
2000-08-11 14:10 ` Tom Lane <tgl@sss.pgh.pa.us>
2000-08-11 16:03   ` Mark Volpe <volpe.mark@epamail.epa.gov>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox