Re: [GENERAL] Implementing replace function

2010-11-02 Thread Martijn van Oosterhout
On Tue, Nov 02, 2010 at 10:46:42AM +0100, Matthieu Huin wrote: > Hello, > > If you expect your data to reach some kind of "critical size" at some > point ( ie updates will be more likely than inserts at that point ), you > can optimize your UPSERT code by trying to UPDATE before INSERTing. >

Re: [GENERAL] Implementing replace function

2010-11-02 Thread Matthieu Huin
Hello, If you expect your data to reach some kind of "critical size" at some point ( ie updates will be more likely than inserts at that point ), you can optimize your UPSERT code by trying to UPDATE before INSERTing. Otherwise trying to INSERT first should decrease the average UPSERT executi

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Raymond O'Donnell
On 31/10/2010 17:28, Alexander Farber wrote: I've created a function now (below) and can call it as well, but how can I see it at the psql prompt? Is there a \d command for that or should I dump the database to see my declarations? You can do \df public.*, assuming that your function is in the

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Rob Sargent
\df *update* Alexander Farber wrote: I've created a function now (below) and can call it as well, but how can I see it at the psql prompt? Is there a \d command for that or should I dump the database to see my declarations? And is my function atomic? I.e. can't it happen, that FOUND is not true

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Alexander Farber
I've created a function now (below) and can call it as well, but how can I see it at the psql prompt? Is there a \d command for that or should I dump the database to see my declarations? And is my function atomic? I.e. can't it happen, that FOUND is not true, but then another session calls a INSER

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Pavel Stehule
2010/10/31 Alexander Farber : > And would a pure SQL-function solution to call > an INSERT followed by an UPDATE in its body > and ignoring the error? (don't know how ignore it > best though, so that I don't ignore other critical errors) > You must not ignore errors in SQL - it's not T-SQL :) Pav

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Pavel Stehule
2010/10/31 Alexander Farber : > Thanks for all the comments. > > Do I need to use BEGIN/COMMIT in my plpgsql-function for UPSERT or are > functions atomic? If you use a code from documentation, then you don't need explicit transaction - every SQL run inside outer implicit transaction in PostgreSQL

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Alexander Farber
[corrected typo, sorry] And wouldn't a pure SQL-function solution be: to call an INSERT followed by an UPDATE in its body and ignoring the error? (don't know how ignore that error best though, so that I don't ignore other critical errors) -- Sent via pgsql-general mailing list (pgsql-general@pos

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Alexander Farber
And would a pure SQL-function solution to call an INSERT followed by an UPDATE in its body and ignoring the error? (don't know how ignore it best though, so that I don't ignore other critical errors) -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your sub

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Alexander Farber
Thanks for all the comments. Do I need to use BEGIN/COMMIT in my plpgsql-function for UPSERT or are functions atomic? Regards Alex -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Thomas Kellerer
Alexander Farber wrote on 31.10.2010 09:22: Hello Postgres users, to mimic the MySQL-REPLACE statement I need to try to UPDATE a record and if that fails - INSERT it. There is actually an example of this in the PG manual ;) http://www.postgresql.org/docs/current/static/plpgsql-control-struct

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Pavel Stehule
2010/10/31 Dmitriy Igrishin : > Okay, Pavel, will wait for 9.1 :-) > > It is a common case - insert new row if it cannot be updated. you can find (probably) MERGE statement in 9.1. Pavel > > 2010/10/31 Pavel Stehule >> >> Hello >> >> 2010/10/31 Dmitriy Igrishin : >> > Hey Alexander, Pavel >> >

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Dmitriy Igrishin
Okay, Pavel, will wait for 9.1 :-) It is a common case - insert new row if it cannot be updated. 2010/10/31 Pavel Stehule > Hello > > 2010/10/31 Dmitriy Igrishin : > > Hey Alexander, Pavel > > > > The solution like below should works IMO, but it does not. > > insert into pref_users(id, first_

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Pavel Stehule
Hello 2010/10/31 Dmitriy Igrishin : > Hey Alexander, Pavel > > The solution like below should works IMO, but it does not. >   insert into pref_users(id, first_name, last_name, >     female, avatar, city, last_ip) >     select $1, $2, $3, $4, $5, $6, $7 >   where not exists > (update p

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Dmitriy Igrishin
Hey Alexander, Pavel The solution like below should works IMO, but it does not. insert into pref_users(id, first_name, last_name, female, avatar, city, last_ip) select $1, $2, $3, $4, $5, $6, $7 where not exists (update pref_users set first_name = $2, l

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Pavel Stehule
2010/10/31 Alexander Farber : > Thanks Pavel, but I have an SQL procedure and not plpgsql? it's not possible in sql. But plpgsql is same level like stored procedures language from mysql. > > On Sun, Oct 31, 2010 at 9:34 AM, Pavel Stehule > wrote: >>> But how can I detect that the UPDATE has fai

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Alexander Farber
Thanks Pavel, but I have an SQL procedure and not plpgsql? On Sun, Oct 31, 2010 at 9:34 AM, Pavel Stehule wrote: >> But how can I detect that the UPDATE has failed in my SQL procedure? >> > > see: > http://www.postgresql.org/docs/8.4/interactive/plpgsql-control-structures.html > near to end of p

Re: [GENERAL] Implementing replace function

2010-10-31 Thread Pavel Stehule
Hello 2010/10/31 Alexander Farber : > Hello Postgres users, > > to mimic the MySQL-REPLACE statement I need > to try to UPDATE a record and if that fails - INSERT it. > > But how can I detect that the UPDATE has failed in my SQL procedure? > see: http://www.postgresql.org/docs/8.4/interactive/pl

[GENERAL] Implementing replace function

2010-10-31 Thread Alexander Farber
Hello Postgres users, to mimic the MySQL-REPLACE statement I need to try to UPDATE a record and if that fails - INSERT it. But how can I detect that the UPDATE has failed in my SQL procedure? begin transaction; create table pref_users ( id varchar(32) primary key