Re: [SQL] Rules and sequences

2010-05-27 Thread A. Kretschmer
In response to Tom Lane : Ben Morrow b...@morrow.me.uk writes: I am trying to implement a fairly standard 'audit table' setup, but using rules instead of triggers (since it should be more efficient). Rules are sufficiently tricky that I would never, ever rely on them for auditing. Use a

Re: [SQL] Rules and sequences

2010-05-27 Thread Ben Morrow
Quoth t...@sss.pgh.pa.us (Tom Lane): Ben Morrow b...@morrow.me.uk writes: I am trying to implement a fairly standard 'audit table' setup, but using rules instead of triggers (since it should be more efficient). Rules are sufficiently tricky that I would never, ever rely on them for

[SQL] Rules and sequences

2010-05-26 Thread Ben Morrow
I am trying to implement a fairly standard 'audit table' setup, but using rules instead of triggers (since it should be more efficient). However, I'm running into problems when one of the audited tables has a 'serial' column that is allowed to default: create table foo (id serial, bar text);

Re: [SQL] Rules and sequences

2010-05-26 Thread Tom Lane
Ben Morrow b...@morrow.me.uk writes: I am trying to implement a fairly standard 'audit table' setup, but using rules instead of triggers (since it should be more efficient). Rules are sufficiently tricky that I would never, ever rely on them for auditing. Use a simple AFTER trigger instead.

Re: [SQL] Rules, functions and RETURNING

2009-09-18 Thread Nico Mandery
Adrian Klaver wrote: On Thursday 17 September 2009 8:35:52 am Nico Mandery wrote: Hello list, I am trying to wirte a rule which calls a PLPgSQL-function upon an Insert in a table. Here is a somewhat simplified example of what i got so far: CREATE TABLE mytable ( mytable_id serial

Re: [SQL] Rules, functions and RETURNING

2009-09-17 Thread Adrian Klaver
On Thursday 17 September 2009 8:35:52 am Nico Mandery wrote: Hello list, I am trying to wirte a rule which calls a PLPgSQL-function upon an Insert in a table. Here is a somewhat simplified example of what i got so far: CREATE TABLE mytable ( mytable_id serial PRIMARY KEY,

[SQL] Rules with sequence columns

2007-03-28 Thread Ray Madigan
I have the following situation that I would appreciate your input on: I have a table with a column that I use to uniquely identify its rows. The table also has a rule on insert that stores the row identifier into another table for reference at some other point. The table is defined as CREATE

Re: [SQL] Rules with sequence columns

2007-03-28 Thread Tom Lane
Ray Madigan [EMAIL PROTECTED] writes: Does anyone have any suggestion? Use a trigger to propagate the data to the other table. You can't make this work reliably with a rule, because rules are macros and hence inherently subject to double-evaluation problems when dealing with volatile functions.

Re: [SQL] RULES on SELECT with JDBC/perlDBI from other RDBMS products?

2005-10-07 Thread Richard Huxton
Bath, David wrote: Folks, I'm looking at using pg to be the main platform for integrating info from other RDBMS products (particularly Oracle) as pg seems to be the most flexible RDBMS around. Disregarding writing to foreign products, query-plan efficiencies, or differences of SQL dialect, I'd

Re: [SQL] RULES on SELECT with JDBC/perlDBI from other RDBMS

2005-10-07 Thread Sean Davis
On 10/6/05 9:07 PM, Bath, David [EMAIL PROTECTED] wrote: Folks, I'm looking at using pg to be the main platform for integrating info from other RDBMS products (particularly Oracle) as pg seems to be the most flexible RDBMS around. Disregarding writing to foreign products, query-plan

[SQL] RULES on SELECT with JDBC/perlDBI from other RDBMS products?

2005-10-06 Thread Bath, David
Folks, I'm looking at using pg to be the main platform for integrating info from other RDBMS products (particularly Oracle) as pg seems to be the most flexible RDBMS around. Disregarding writing to foreign products, query-plan efficiencies, or differences of SQL dialect, I'd like to have a way

Re: [SQL] rules

2004-06-30 Thread Jan Norman Pedersen
Anslag Stephan Szabo [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] On Wed, 19 May 2004, Jie Liang wrote: According to the document of rule: CREATE RULE rulename AS ON delete TO mytablename DO ( delete from aaa where id=OLD.id; Delete from bbb where id=OLD.id; Delete

[SQL] rules

2004-05-19 Thread Jie Liang
According to the document of rule: CREATE RULE rulename AS ON delete TO mytablename DO ( delete from aaa where id=OLD.id; Delete from bbb where id=OLD.id; Delete from ccc where id=OLD.id ); Should work, but it doesn't, what wrong with it? Even I use { } Jie Liang

Re: [SQL] rules

2004-05-19 Thread Stephan Szabo
On Wed, 19 May 2004, Jie Liang wrote: According to the document of rule: CREATE RULE rulename AS ON delete TO mytablename DO ( delete from aaa where id=OLD.id; Delete from bbb where id=OLD.id; Delete from ccc where id=OLD.id ); Should work, but it doesn't, what wrong with it? What is

[SQL] rules and return values question

2003-11-28 Thread Mujdat Pakkan
We have an interesting case where we want to use Postgres both as a database and a front end to a proprietary database. For the latter, we wrote functions that access the proprietary database. Then we defined views on the proprietary database and wrote rules for insert/update/delete on those views

Re: [SQL] rules and return values question

2003-11-28 Thread Rod Taylor
tables but it introduces too much overhead. In any case, we need the return value (or an exception) from the function but there does not seem a way to return it. Can't you use erreport() to return an exception message? ---(end of broadcast)---

Re: [SQL] Rules: passing new/old to functions

2003-06-26 Thread Tom Lane
Gunter Diehl [EMAIL PROTECTED] writes: create funtion f1(v, v) returns void as '...' create rule vupt as on update to v do instead select f1(new, old) While accepting this definitions, pg says it can not handle whole-tuple references at runtime. FWIW, the old case works fine. new doesn't

Re: [SQL] Rules/Trigges Trade-offs

2002-12-09 Thread Jean-Luc Lachance
Josh, Thanks for the info. I need to change an insert into an update when the key already exists. I have been using a rules to test it on a small set (table) and it works. Rules can't use indexes just scared me. I will have to test on a larger set. Also, I had the impression that if a trigger

Re: [SQL] Rules/Trigges Trade-offs

2002-12-09 Thread Josh Berkus
Ian, Anyway, I have a similar requirement, to intercept insert/update/delete and redirect the data if a condition is met. Right now I am trying an INSTEAD rule that puts the condtion in the WHERE of the rule definition. It seems to work OK, but if the condition has exeptions where I would

Re: [SQL] Rules/Trigges Trade-offs

2002-12-09 Thread Josh Berkus
Ian, Thanks! I would have, but my messages bounce from SQL, even though I am subscribed ( I get the messages, for crying out loud!) send an e-mail to [EMAIL PROTECTED] PS RECORD doesn't work but I think that is because I am on 7.2.1 OK. I may have tested that on 7.4 devel by

[SQL] Rules/Trigges Trade-offs

2002-12-06 Thread Jean-Luc Lachance
Hi all! Is there a guideline on the use of rules compared to triggers when both can be use to achieve the same result? JLL ---(end of broadcast)--- TIP 4: Don't 'kill -9' the postmaster

Re: [SQL] Rules/Trigges Trade-offs

2002-12-06 Thread Bruce Momjian
My book has a section on that: http://www.postgresql.org/docs/awbook.html Triggers are mostly for testing/modifying the row being inserted/updated, while rules are better for affecting other rows or other tables. Jean-Luc Lachance wrote: Hi all! Is there a guideline on the use of

Re: [SQL] Rules/Trigges Trade-offs

2002-12-06 Thread Jean-Luc Lachance
Thanks for the info. Do you mean that if an update affects more than one row I should use triggers because the rules will be executed only once? JLL Richard Huxton wrote: On Friday 06 Dec 2002 4:03 pm, Jean-Luc Lachance wrote: Hi all! Is there a guideline on the use of rules compared

Re: [SQL] Rules/Trigges Trade-offs

2002-12-06 Thread Bruce Momjian
No, the rule will affect all the rows using one query. --- Jean-Luc Lachance wrote: Thanks for the info. Do you mean that if an update affects more than one row I should use triggers because the rules will be executed

Re: [SQL] Rules/Trigges Trade-offs

2002-12-06 Thread Bruce Momjian
Josh Berkus wrote: Bruce, Richard, Triggers are mostly for testing/modifying the row being inserted/updated, while rules are better for affecting other rows or other tables. Hmmm. Thought that there were also some other criteria: 1) Rules can't use indexes to do their processing,

Re: [SQL] Rules/Trigges Trade-offs

2002-12-06 Thread Josh Berkus
Bruce, 1) Rules can't use indexes to do their processing, so Rules which query large secondary tables can be a bad idea (maybe this has changed?) I don't think this is true. Rewrite is before optimizer so it should be optimized just the same. I was speaking if the Rule has to do a

Re: [SQL] Rules/Trigges Trade-offs

2002-12-06 Thread Bruce Momjian
Josh Berkus wrote: Bruce, 1) Rules can't use indexes to do their processing, so Rules which query large secondary tables can be a bad idea (maybe this has changed?) I don't think this is true. Rewrite is before optimizer so it should be optimized just the same. I was

Re: [SQL] Rules/Trigges Trade-offs

2002-12-06 Thread Bruce Momjian
Bruce Momjian wrote: 3) There are no AFTER Rules, making, for example, a rule with a table check on the new data impractical, so you'd want to use Triggers or Constraints We have changed ordering in 7.3 where I think INSERT rules are _after_ the insert. How would that

Re: [SQL] Rules and Triggers: another question

2002-09-12 Thread Michael Paesold
Jerome Chochon wrote: Thanks for your answer but i have another question. Which one is the faster ? If i write this trigger... CREATE TRIGGER trigger_name BEFORE DELETE ON table_name FOR EACH ROW EXECUTE PROCEDURE function(); and this rule: CREATE RULE name_rule AS ON DELETE TO

[SQL] Rules and Triggers

2002-09-10 Thread Michael Paesold
Hi all, can you tell me in what order rules and triggers are executed? First, what comes first, the rules, or the triggers? And then, in what order are all the rules / triggers executed? Regards, Michael Paesold ---(end of broadcast)--- TIP 6:

Re: [SQL] Rules and Triggers

2002-09-10 Thread Adam Erickson
them in when you created them but I pulled that straight out of my arse. :) -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On Behalf Of Michael Paesold Sent: Tuesday, September 10, 2002 1:53 PM To: [EMAIL PROTECTED] Subject: [SQL] Rules and Triggers Hi all

Re: [SQL] Rules and Triggers

2002-09-10 Thread Michael Paesold
Adam Erickson wrote: Correct me if I'm wrong, but rules constrain the SQL (ie. validation). Triggers are carried out after the SQL is executed and the data is modified. So, to answer your question, I think rules come first. As to which order the rules / triggers are executed probably

Re: [SQL] Rules and Triggers

2002-09-10 Thread Ross J. Reedstrom
On Tue, Sep 10, 2002 at 09:45:16PM +0200, Michael Paesold wrote: Adam Erickson wrote: Correct me if I'm wrong, but rules constrain the SQL (ie. validation). Triggers are carried out after the SQL is executed and the data is modified. So, to answer your question, I think rules come

[SQL] rules / triggers on insert. why after?

2002-07-12 Thread Ahti Legonkov
Hi, I have the following things in my database: CREATE SEQUENCE REO_ID_seq INCREMENT 1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1; CREATE TABLE reo ( REO_ID int4 DEFAULT nextval('REO_ID_seq'::text) NOT NULL, TYPE varchar(64) NOT NULL, CONSTRAINT REO_ID_reo_ukey UNIQUE (REO_ID),

Re: [SQL] rules / triggers on insert. why after?

2002-07-12 Thread Jan Wieck
Ahti Legonkov wrote: Does anyone know why since postgres 7.2 the rules are executed *after* the insert? Because people where still complaining that they changed to execute *before* in v6.4. Jan -- #==# # It's easier to

Re: [SQL] rules / triggers on insert. why after?

2002-07-12 Thread Tom Lane
Ahti Legonkov [EMAIL PROTECTED] writes: CREATE TABLE reo ( REO_ID int4 DEFAULT nextval('REO_ID_seq'::text) NOT NULL, TYPE varchar(64) NOT NULL, CONSTRAINT REO_ID_reo_ukey UNIQUE (REO_ID), CONSTRAINT reo_pkey PRIMARY KEY (REO_ID) ); CREATE TABLE lreo ( CITY varchar(64),

[SQL] RULES

2001-05-22 Thread J.Fernando Moyano
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Well ... i'm testing carefully the two rules == CREATE RULE piezas_add AS ON insert TO piezas DO update materia_prima set usadas=(usadas+1) where n_material=new.n_material; CREATE RULE piezas_delete AS ON delete

Re: [SQL] RULES

2001-05-22 Thread Ross J. Reedstrom
On Tue, May 22, 2001 at 10:44:06AM +0300, Mart?n Marqu?s wrote: On Mar 22 May 2001 20:19, J.Fernando Moyano wrote: Deleting is broken if it deletes more than one row. The rule is executed only one time each delete command, and not one time each deleted row. It's this OK ??? Am i wrong

[SQL] rules

2001-04-26 Thread Martín Marqués
Is it posible to make a rule execute more then one query? Something like: CREATE RULE rule_name AS ON INSERT TO table1 DO INSTEAD INSERT INTO table2 VALUES (new.value1,new.value2) INSERT INTO table3 VALUES (x,y) If not, is there a way to do this? Triggers maybe? Saludos... :-) -- El mejor

Re: [SQL] rules

2001-04-26 Thread Jan Wieck
Martín Marqués wrote: Is it posible to make a rule execute more then one query? Something like: CREATE RULE rule_name AS ON INSERT TO table1 DO INSTEAD INSERT INTO table2 VALUES (new.value1,new.value2) INSERT INTO table3 VALUES (x,y) Yes: CREATE RULE rule_name AS ON INSERT

[SQL] Rules and transactions

2001-01-23 Thread Brett Schwarz
If I have a rule, is the rule inside a tranaction along with the table that it references. For example, if I have a rule that deletes an entry from table B, whenever an entry in table A gets deleted, then is the delete for table A and table B wrapped inside the same transaction? Same question I

Re: [SQL] Rules

2001-01-02 Thread Jan Wieck
Peeter Smitt wrote: Hi I'm trying to make updateable view useing rules. CREATE RULE update_rule AS ON UPDATE TO table DO INSTEAD SELECT fun1(new); Thing is that backend gives this error. ERROR: parser: parse error at or near ")" What i'm doing wrong? Are there any other ways to

[HACKERS] Re: [SQL] Rules with Conditions: Bug, or Misunderstanding

2000-12-04 Thread Jan Wieck
Tom Lane wrote: Jan Wieck [EMAIL PROTECTED] writes: Tom Lane wrote: Hm. Perhaps the "cannot update view" test is too strict --- it's not bright enough to realize that the two rules together cover all cases, so it complains that you *might* be trying to update the view. As the code

[HACKERS] Re: [SQL] Rules with Conditions: Bug, or Misunderstanding

2000-12-03 Thread Jan Wieck
Tom Lane wrote: "Joel Burton" [EMAIL PROTECTED] writes: create rule dev_ins as on update to dev_col_comments where old.description isnull do instead insert into pg_description ( objoid, description) values (old.att_oid, new.description); create rule dev_upd as on update to

Re: [SQL] Rules with Conditions: Bug, or Misunderstanding

2000-12-01 Thread Tom Lane
"Joel Burton" [EMAIL PROTECTED] writes: In any event, though, the rule above crashes my backend, as do simpler versions I wrote that try your CREATE RULE DO INSTEAD ( INSERT; UPDATE; ) idea. Ugh :-( What information can I provide to the list to troubleshoot this? A gdb backtrace from the

Re: [HACKERS] Re: [SQL] Rules with Conditions: Bug, or Misunderstanding

2000-11-30 Thread Mark Hollomon
On Wednesday 29 November 2000 19:42, Tom Lane wrote: Hm. Perhaps the "cannot update view" test is too strict --- it's not bright enough to realize that the two rules together cover all cases, so it complains that you *might* be trying to update the view. As the code stands, you must

Re: [HACKERS] Re: [SQL] Rules with Conditions: Bug, or Misunderstanding

2000-11-30 Thread Tom Lane
Mark Hollomon [EMAIL PROTECTED] writes: On Wednesday 29 November 2000 19:42, Tom Lane wrote: Hm. Perhaps the "cannot update view" test is too strict --- it's not bright enough to realize that the two rules together cover all cases, so it complains that you *might* be trying to update the

[SQL] Rules with Conditions: Bug, or Misunderstanding

2000-11-29 Thread Joel Burton
Am I misunderstanding how to use rule w/conditionals, or is there a bug in this? -- I love to use Pgsql comments, but find the 'comment on field...' language a bit of a pain for documenting a large database at the last minute. So, I wrote a query that pulls together all the fields in a

Re: [SQL] Rules with Conditions: Bug, or Misunderstanding

2000-11-29 Thread Tom Lane
"Joel Burton" [EMAIL PROTECTED] writes: create rule dev_ins as on update to dev_col_comments where old.description isnull do instead insert into pg_description ( objoid, description) values (old.att_oid, new.description); create rule dev_upd as on update to dev_col_comments where

Re: [SQL] Rules aren't doing what I expect

2000-08-11 Thread Tom Lane
Mark Volpe [EMAIL PROTECTED] 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.

Re: [SQL] Rules aren't doing what I expect

2000-08-10 Thread Mark Volpe
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:

Re: [SQL] Rules aren't doing what I expect

2000-08-09 Thread Ang Chin Han
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

[SQL] Rules, Select, Union

2000-08-06 Thread Itai Zukerman
I'm doing this: CREATE TABLE data ( val int4 ); CREATE TABLE pos ( ) INHERITS ( data ); CREATE TABLE neg ( ) INHERITS ( data ); CREATE RULE data_to_pos AS ON INSERT TO data WHERE NEW.val 0 DO INSTEAD INSERT INTO pos ( val ) VALUES ( NEW.val ); CREATE RULE data_to_neg AS ON

[SQL] RULES on PostgreSQL

2000-07-24 Thread Jesus Aneiros
Hello all, Forgive my ignorance. I'm trying to understand the RULES in PostgreSQL and I have several questions. Why the rules if they are not part of SQL92 or SQL3? Where do they come from? Are they from before the SQL history of Postgres? Many things that can be done with RULES can be done with