473,406 Members | 2,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

questions on rules

I'm trying to set up some basic rules to log inserts, updates, and
deletes to tables in an inheritance hierarchy (by inserting records
into a log table), and I've got a couple of questions.

(1) Is it possible to create a rule on a base table and have it
operate for all derived tables? I'd like to just create 3 rules
(insert/update/delete) on the base table and have them apply to all
inherited tables. Can this be done?

(2) I've got a very simple update rule-- create rule log_updates as on
update to foo do insert into audit_log(table_oid, id, log_what) values
(foo.tableoid, NEW.foo_id, 'U'); I had hoped that this would create a
single entry in my audit_log table for each row updated. However, it
seems to fire for each record in the "foo" table, even if the update
affected only one row! What am I doing wrong?

Any help would be very much appreciated. Thanks!

Tim Perrigo

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #1
10 1435
On Monday 26 April 2004 20:12, Timothy Perrigo wrote:
I'm trying to set up some basic rules to log inserts, updates, and
deletes to tables in an inheritance hierarchy (by inserting records
into a log table), and I've got a couple of questions.


I think you want to look at triggers rather than rules here.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #2
On Monday 26 April 2004 20:12, Timothy Perrigo wrote:
I'm trying to set up some basic rules to log inserts, updates, and
deletes to tables in an inheritance hierarchy (by inserting records
into a log table), and I've got a couple of questions.


I think you want to look at triggers rather than rules here.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #3
Thanks for the reply. Do you know if triggers defined on a base table
fire for operations on inherited tables? (I.e., if I have an after
insert trigger on table "base", and a table "derived" that inherits
from base, will inserts into derived cause the trigger on base to
fire?) If so (this is the behavior I would like), is there a way to
get the tableoid of the table which caused the trigger to fire?

I really appreciate the assistance.

Tim

On Apr 27, 2004, at 7:26 AM, Richard Huxton wrote:
On Monday 26 April 2004 20:12, Timothy Perrigo wrote:
I'm trying to set up some basic rules to log inserts, updates, and
deletes to tables in an inheritance hierarchy (by inserting records
into a log table), and I've got a couple of questions.


I think you want to look at triggers rather than rules here.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #4
Thanks for the reply. Do you know if triggers defined on a base table
fire for operations on inherited tables? (I.e., if I have an after
insert trigger on table "base", and a table "derived" that inherits
from base, will inserts into derived cause the trigger on base to
fire?) If so (this is the behavior I would like), is there a way to
get the tableoid of the table which caused the trigger to fire?

I really appreciate the assistance.

Tim

On Apr 27, 2004, at 7:26 AM, Richard Huxton wrote:
On Monday 26 April 2004 20:12, Timothy Perrigo wrote:
I'm trying to set up some basic rules to log inserts, updates, and
deletes to tables in an inheritance hierarchy (by inserting records
into a log table), and I've got a couple of questions.


I think you want to look at triggers rather than rules here.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #5
On Tuesday 27 April 2004 13:40, Timothy Perrigo wrote:
Thanks for the reply. Do you know if triggers defined on a base table
fire for operations on inherited tables? (I.e., if I have an after
insert trigger on table "base", and a table "derived" that inherits
from base, will inserts into derived cause the trigger on base to
fire?)
Hmm - don't know this I'm afraid.
If so (this is the behavior I would like), is there a way to
get the tableoid of the table which caused the trigger to fire?


Here I can help. Check the plpgsql section of the manuals, and there you'll
find a list of special variables available to trigger functions. These
include table and trigger name.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #6
On Tuesday 27 April 2004 13:40, Timothy Perrigo wrote:
Thanks for the reply. Do you know if triggers defined on a base table
fire for operations on inherited tables? (I.e., if I have an after
insert trigger on table "base", and a table "derived" that inherits
from base, will inserts into derived cause the trigger on base to
fire?)
Hmm - don't know this I'm afraid.
If so (this is the behavior I would like), is there a way to
get the tableoid of the table which caused the trigger to fire?


Here I can help. Check the plpgsql section of the manuals, and there you'll
find a list of special variables available to trigger functions. These
include table and trigger name.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #7
It seems that triggers are not inherited, so to get the functionality I
want I'll have to create a trigger for each table. If anyone knows
another way, please let me know!

After you pointed me in the right direction, I was able to create a
trigger procedure which can be called from triggers on various tables
and will log the operation (including the affected table's oid and
name). The procedure is listed below. Thanks for the help!

Tim

create or replace function add_log_entry() returns TRIGGER as '
BEGIN
insert into audit_log(table_oid, table_name, id, operation) values
(TG_RELID, TG_RELNAME, NEW.id, TG_OP);
return NEW;
END;
' language 'plpgsql';

On Apr 27, 2004, at 8:18 AM, Richard Huxton wrote:
On Tuesday 27 April 2004 13:40, Timothy Perrigo wrote:
Thanks for the reply. Do you know if triggers defined on a base table
fire for operations on inherited tables? (I.e., if I have an after
insert trigger on table "base", and a table "derived" that inherits
from base, will inserts into derived cause the trigger on base to
fire?)


Hmm - don't know this I'm afraid.
If so (this is the behavior I would like), is there a way to
get the tableoid of the table which caused the trigger to fire?


Here I can help. Check the plpgsql section of the manuals, and there
you'll
find a list of special variables available to trigger functions. These
include table and trigger name.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #8
It seems that triggers are not inherited, so to get the functionality I
want I'll have to create a trigger for each table. If anyone knows
another way, please let me know!

After you pointed me in the right direction, I was able to create a
trigger procedure which can be called from triggers on various tables
and will log the operation (including the affected table's oid and
name). The procedure is listed below. Thanks for the help!

Tim

create or replace function add_log_entry() returns TRIGGER as '
BEGIN
insert into audit_log(table_oid, table_name, id, operation) values
(TG_RELID, TG_RELNAME, NEW.id, TG_OP);
return NEW;
END;
' language 'plpgsql';

On Apr 27, 2004, at 8:18 AM, Richard Huxton wrote:
On Tuesday 27 April 2004 13:40, Timothy Perrigo wrote:
Thanks for the reply. Do you know if triggers defined on a base table
fire for operations on inherited tables? (I.e., if I have an after
insert trigger on table "base", and a table "derived" that inherits
from base, will inserts into derived cause the trigger on base to
fire?)


Hmm - don't know this I'm afraid.
If so (this is the behavior I would like), is there a way to
get the tableoid of the table which caused the trigger to fire?


Here I can help. Check the plpgsql section of the manuals, and there
you'll
find a list of special variables available to trigger functions. These
include table and trigger name.

--
Richard Huxton
Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #9
On Apr 26, 2004, at 3:12 PM, Timothy Perrigo wrote:
I'm trying to set up some basic rules to log inserts, updates, and
deletes to tables in an inheritance hierarchy (by inserting records
into a log table), and I've got a couple of questions.

(1) Is it possible to create a rule on a base table and have it
operate for all derived tables? I'd like to just create 3 rules
(insert/update/delete) on the base table and have them apply to all
inherited tables. Can this be done?
I've never tried this myself, but I feel pretty good about saying the
answer is "NO". :( Most other postgres features (esp. triggers) don't
inherit either.
(2) I've got a very simple update rule-- create rule log_updates as on
update to foo do insert into audit_log(table_oid, id, log_what) values
(foo.tableoid, NEW.foo_id, 'U');
Ever just tried to do this from psql:
SELECT foo.tableoid;

You get a resultset with a row for every row in table foo. That's
essentially what your INSERT statement is doing. It's as if you wrote:
INSERT INTO audit_log(table_oid, id, what) SELECT tableoid,
NEW.foo_id, 'U' FROM foo;

What you want to do in your rule, I think, is something like this:
INSERT INTO audit_log(table_oid, id, what) values ( (select tableoid
from foo limit 1), NEW.foo_id, 'U');

There might be a different way to lookup the tableoid for table "foo",
but it would likely require using 'foo' as a quoted string against a
query in pg_class, so the above might make things clearer.

eric

ps, never knew about the "tableoid" field until just now. how
interesting.

I had hoped that this would create a single entry in my audit_log
table for each row updated. However, it seems to fire for each record
in the "foo" table, even if the update affected only one row! What am
I doing wrong?

Any help would be very much appreciated. Thanks!

Tim Perrigo
---------------------------(end of
broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #10
On Apr 26, 2004, at 3:12 PM, Timothy Perrigo wrote:
I'm trying to set up some basic rules to log inserts, updates, and
deletes to tables in an inheritance hierarchy (by inserting records
into a log table), and I've got a couple of questions.

(1) Is it possible to create a rule on a base table and have it
operate for all derived tables? I'd like to just create 3 rules
(insert/update/delete) on the base table and have them apply to all
inherited tables. Can this be done?
I've never tried this myself, but I feel pretty good about saying the
answer is "NO". :( Most other postgres features (esp. triggers) don't
inherit either.
(2) I've got a very simple update rule-- create rule log_updates as on
update to foo do insert into audit_log(table_oid, id, log_what) values
(foo.tableoid, NEW.foo_id, 'U');
Ever just tried to do this from psql:
SELECT foo.tableoid;

You get a resultset with a row for every row in table foo. That's
essentially what your INSERT statement is doing. It's as if you wrote:
INSERT INTO audit_log(table_oid, id, what) SELECT tableoid,
NEW.foo_id, 'U' FROM foo;

What you want to do in your rule, I think, is something like this:
INSERT INTO audit_log(table_oid, id, what) values ( (select tableoid
from foo limit 1), NEW.foo_id, 'U');

There might be a different way to lookup the tableoid for table "foo",
but it would likely require using 'foo' as a quoted string against a
query in pg_class, so the above might make things clearer.

eric

ps, never knew about the "tableoid" field until just now. how
interesting.

I had hoped that this would create a single entry in my audit_log
table for each row updated. However, it seems to fire for each record
in the "foo" table, even if the update affected only one row! What am
I doing wrong?

Any help would be very much appreciated. Thanks!

Tim Perrigo
---------------------------(end of
broadcast)---------------------------
TIP 8: explain analyze is your friend

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Boris Ammerlaan | last post by:
This notice is posted about every week. I'll endeavor to use the same subject line so that those of you who have seen it can kill-file the subject; additionally, Supersedes: headers are used to...
19
by: Gérard Talbot | last post by:
posted to: alt.html and comp.infosystems.www.authoring.html followup-to: comp.infosystems.www.authoring.html Hello all, I have 2 questions about validations. 1- What's basically the...
41
by: Gérard Talbot | last post by:
Cross-posted to: comp.infosystems.www.authoring.html and alt.html Followup-to: comp.infosystems.www.authoring.html 1- One day, I stumbled across a website that offers to validate webpages. What...
53
by: ROSY | last post by:
hello, response if u ,on all level of questions::: 1.how a self deletable .exe file deleted on some future date & time without invoking the .exe itself? 2.if we want that any wildcard...
0
by: Timothy Perrigo | last post by:
I'm trying to set up some basic rules to log inserts, updates, and deletes to tables in an inheritance hierarchy (by inserting records into a log table), and I've got a couple of questions. (1) ...
3
by: Phil Lee | last post by:
Hi, I have a few questions regarding web services in .NET 2 1) Why, when I run code analysis do I get a source controlled files named {guid}/codeanalysislog.xml...
8
by: NH | last post by:
Hi, I'm looking for some opinions and advice on designing ASP.Net apps. Let me explain my approach to how I currently design and hopefully you can give me some advice. I create systems for a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.