Re: Switching from MySQL: ON DUPLICATE KEY UPDATE, plpgsql function

From: "Chris Spotts" <rfusca(at)gmail(dot)com>
To: "'APseudoUtopia'" <apseudoutopia(at)gmail(dot)com>, "'David Kerr'" <dmk(at)mr-paradox(dot)net>
Cc: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Switching from MySQL: ON DUPLICATE KEY UPDATE, plpgsql function
Date: 2009-07-01 11:32:28
Message-ID: 002f01c9fa3f$9e54efe0$dafecfa0$@com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

> >
> > Wouldn't you just be looking for something like:
> >
> > BEGIN;
> > EXECUTE 'insert into forums_readposts values ('...')';
> > EXCEPTION when unique_violation THEN
> > EXECUTE 'update forums_readposts set lastpostread =
> '...' ';
> > END;

> > The logic as i read your post is. If the user's never done a
> "catchup" operation
> > before, this will create the record. If he has, then it will update
> this record
> > to reflect the new transid.
> >
> > Dave
> >
>
> Hmm, yeah. I'm new to PostgreSQL, and have little experience with
> MySQL other than basic queries and joins. I didn't get to the part in
> the docs that covers EXECUTE yet, haha. That looks like it would do
> exactly what I want it to.
[Spotts, Christopher]
There's a good example and some documentation of what Dave said in the manual at the bottom of this page.
http://www.postgresql.org/docs/current/static/plpgsql-control-structures.html
You should really only need to "EXECUTE" here if you have some dynamically determined columns or tables. The "magic" here is a combination of "IF found" and "EXCEPTION WHEN unique violation"

In response to

Browse pgsql-general by date

  From Date Subject
Next Message durumdara 2009-07-01 12:33:33 Re: [Fwd: Re: Python client + select = locked resources???]
Previous Message Tony Webb 2009-07-01 11:24:45 Big Delete Consistently Causes a Crash