Re: mysql replace in postgreSQL?

From: Lincoln Yeoh <lyeoh(at)pop(dot)jaring(dot)my>
To: David Fetter <david(at)fetter(dot)org>
Cc: blackwater dev <blackwaterdev(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: mysql replace in postgreSQL?
Date: 2005-10-31 16:58:32
Message-ID: 5.2.1.1.1.20051101001751.0358a550@localhost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

At 08:24 AM 10/30/2005 -0800, David Fetter wrote:

> > >http://developer.postgresql.org/docs/postgres/plpgsql-control-structure
> s.html#PLPGSQL-ERROR-TRAPPING
> >
> > Erm, doesn't it have the same race conditions?
>
>No, don't believe it does. Have you found some?

Depends on how you do things.

As I mentioned, it's only fine if you have the relevant uniqueness constraint.

For example, if instead of:

CREATE TABLE db (a INT PRIMARY KEY, b TEXT);

You do:
CREATE TABLE db (a INT, b TEXT);

Then create the function as in the docs.

Then open up two psql sessions.

Session #1
begin;
SELECT merge_db(1, 'david');
select * from db;

Session #2
begin;
SELECT merge_db(1, 'dennis');
select * from db;

Then, do commit in both sessions.

You'll end up with duplicates.

What actually protected the data before was the uniqueness constraint due
to the primary key.

BUT, if you already have the uniqueness constraint, you won't have a
problem mis-inserting duplicates with any of the typical naive methods either.

Not saying the example in the docs is wrong, but it might be misleading to
people who don't fully understand it. That doesn't seem too far fetched to
me given that a fair number here have suggested the usual variations of
"select, update if exist insert if not" and assumed they will work.

Would those methods be fine in non MVCC databases?

Regards,
Link.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Simon Riggs 2005-10-31 16:58:54 Re: Starting PostgreSQL 8.0.4 with more memory [FreeBSD
Previous Message Alex Turner 2005-10-31 16:50:29 Re: Oracle 10g Express - any danger for Postgres?