Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Ted Byers
Thanks all. I tried the appended code in a trigger function, but postgresql won't take it. It complains that assets.quantity is not a scalar. However, the WHERE clause in that select statement guarantees that at most only one record will be returned. An open position on a given kind of asset

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Vivek Khera
On Dec 7, 2007, at 11:42 AM, Colin Wetherbee wrote: You can do this with a conditional. Something like the following should work. IF NOT (a query matching your data returns rows) THEN INSERT (your new data) There exists a race condition here unless you've locked your tables.

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Colin Wetherbee
Vivek Khera wrote: On Dec 7, 2007, at 11:42 AM, Colin Wetherbee wrote: You can do this with a conditional. Something like the following should work. IF NOT (a query matching your data returns rows) THEN INSERT (your new data) There exists a race condition here unless you've locked your

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Vivek Khera
On Dec 10, 2007, at 5:04 PM, Colin Wetherbee wrote: For what it's worth, the real algorithm would be as follows. I hadn't had enough coffee yet, and I forgot the UPDATE bit. IF (a query matching your old data returns rows) THEN UPDATE with your new data ELSE INSERT your new data Still

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Ted Byers
--- Vivek Khera [EMAIL PROTECTED] wrote: On Dec 10, 2007, at 5:04 PM, Colin Wetherbee wrote: For what it's worth, the real algorithm would be as follows. I hadn't had enough coffee yet, and I forgot the UPDATE bit. IF (a query matching your old data returns rows) THEN

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Erik Jones
On Dec 10, 2007, at 4:48 PM, Ted Byers wrote: --- Vivek Khera [EMAIL PROTECTED] wrote: On Dec 10, 2007, at 5:04 PM, Colin Wetherbee wrote: For what it's worth, the real algorithm would be as follows. I hadn't had enough coffee yet, and I forgot the UPDATE bit. IF (a query matching

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Ted Byers
Thanks Erik In a stored procedure you'd just execute the UPDATE and then check the FOUND variable to see if it found a row to update: UPDATE table_name SET foo='bar' WHERE id=5; IF NOT FOUND THEN INSERT INTO table_name (id, foo) VALUES (5, 'bar'); END IF; To be clear, if I

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Richard Broersma Jr
--- On Mon, 12/10/07, Ted Byers [EMAIL PROTECTED] wrote: but how do you do it using SQL in an RDBMS? I believe that there is an ANSI SQL command MERGE that is yet to be implemented into PostgreSQL. Regards, Richard Broersma Jr. ---(end of

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Alvaro Herrera
Richard Broersma Jr wrote: --- On Mon, 12/10/07, Ted Byers [EMAIL PROTECTED] wrote: but how do you do it using SQL in an RDBMS? I believe that there is an ANSI SQL command MERGE that is yet to be implemented into PostgreSQL. IIRC the standard's definition of MERGE is still subject

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Colin Wetherbee
Vivek Khera wrote: On Dec 10, 2007, at 5:04 PM, Colin Wetherbee wrote: For what it's worth, the real algorithm would be as follows. I hadn't had enough coffee yet, and I forgot the UPDATE bit. IF (a query matching your old data returns rows) THEN UPDATE with your new data ELSE INSERT your

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-10 Thread Trevor Talbot
On 12/10/07, Colin Wetherbee [EMAIL PROTECTED] wrote: Vivek Khera wrote: On Dec 10, 2007, at 5:04 PM, Colin Wetherbee wrote: IF (a query matching your old data returns rows) THEN UPDATE with your new data ELSE INSERT your new data Still exists race condition. Your race comes from

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-08 Thread John D. Burger
So two design patterns for a makeshift UPSERT have been presented - one is to check beforehand, and only insert if the item isn't present already, the other is to do the insert blindly and let PG check for you, and catch any exceptions. I'm also wondering what people's ideas are for a sort

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-08 Thread Steve Atkins
On Dec 8, 2007, at 7:54 AM, John D. Burger wrote: So two design patterns for a makeshift UPSERT have been presented - one is to check beforehand, and only insert if the item isn't present already ... which will give the wrong results if there's any concurrent updates... , the other is

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-07 Thread Ted Byers
--- Erik Jones [EMAIL PROTECTED] wrote: On Dec 6, 2007, at 2:36 PM, Ted Byers wrote: [snip] What you want to do here for handling the update v. insert is called an UPSERT. Basically, what you do is run the update as if the row exists and catch the exception that is thrown if it

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-07 Thread Steve Atkins
On Dec 7, 2007, at 6:29 AM, Ted Byers wrote: --- Erik Jones [EMAIL PROTECTED] wrote: On Dec 6, 2007, at 2:36 PM, Ted Byers wrote: [snip] What you want to do here for handling the update v. insert is called an UPSERT. Basically, what you do is run the update as if the row exists and catch

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-07 Thread Colin Wetherbee
Ted Byers wrote: --- Erik Jones [EMAIL PROTECTED] wrote: On Dec 6, 2007, at 2:36 PM, Ted Byers wrote: [snip] What you want to do here for handling the update v. insert is called an UPSERT. Basically, what you do is run the update as if the row exists and catch the exception that is

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-07 Thread Erik Jones
On Dec 7, 2007, at 8:29 AM, Ted Byers wrote: --- Erik Jones [EMAIL PROTECTED] wrote: On Dec 6, 2007, at 2:36 PM, Ted Byers wrote: [snip] What you want to do here for handling the update v. insert is called an UPSERT. Basically, what you do is run the update as if the row exists and catch

[GENERAL] SQL design pattern for a delta trigger?

2007-12-06 Thread Ted Byers
IS there such a thing? I can be the first to consider this. What I am aiming for is a solution with a couple coupled tables, one of which represents state through time and the other represents transactions or deltas on the state. With one field (a floating point number) in the state table (or

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-06 Thread Ted Byers
--- Ted Byers [EMAIL PROTECTED] wrote: IS there such a thing? I can be the first to consider this. OOPS. The mind is faster than the fingers. That should have been I can NOT be the first to consider this. Ted ---(end of broadcast)--- TIP

Re: [GENERAL] SQL design pattern for a delta trigger?

2007-12-06 Thread Erik Jones
On Dec 6, 2007, at 2:36 PM, Ted Byers wrote: IS there such a thing? I can be the first to consider this. What I am aiming for is a solution with a couple coupled tables, one of which represents state through time and the other represents transactions or deltas on the state. With one field