Re: Promise index tuples for UPSERT

From: Anssi Kääriäinen <anssi(dot)kaariainen(at)thl(dot)fi>
To: Peter Geoghegan <pg(at)heroku(dot)com>
Cc: Heikki Linnakangas <hlinnakangas(at)vmware(dot)com>, Simon Riggs <simon(at)2ndquadrant(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>, Andres Freund <andres(at)2ndquadrant(dot)com>
Subject: Re: Promise index tuples for UPSERT
Date: 2014-10-08 10:05:53
Message-ID: 1412762753.8545.124.camel@TTY32
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Wed, 2014-10-08 at 02:22 -0700, Peter Geoghegan wrote:
> On Wed, Oct 8, 2014 at 1:25 AM, Heikki Linnakangas
> <hlinnakangas(at)vmware(dot)com> wrote:
> > Instead of naming the index, you should name the columns, and the system can
> > look up the index or indexes that match those columns.
>
> It's not totally clear that we need *any* WITHIN clause, BTW. I'm not
> dead set on it. It was something I mainly added at Kevin's request. I
> do see the risks, though.

To be usable in Django ORM's .save() method there must be an option to
use the primary key index, and only the primary key index for conflict
resolution.

Assume an author table with id SERIAL PRIMARY KEY, email TEXT UNIQUE,
age INTEGER, then when saving an object, Django must update the row with
matching primary key value, or otherwise do an insert. Doing an update
of matching email column isn't allowed. So, Django can't use the query:

INSERT INTO author values(1, 'tom(at)example(dot)com', 35)
ON CONFLICT UPDATE
SET email = conflicting(email), age = conflicting(age);

If the table contains data (id=2, email='tom(at)example(dot)com', age=34), the
query would update tom's age to 35 when it should have resulted in
unique constraint violation.

Other ORMs have similar save/persist implementations, that is objects
are persisted on primary key identity alone.

- Anssi

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Marti Raudsepp 2014-10-08 10:06:15 Re: INSERT ... ON CONFLICT {UPDATE | IGNORE}
Previous Message Heikki Linnakangas 2014-10-08 09:54:00 Re: BUG: *FF WALs under 9.2 (WAS: .ready files appearing on slaves)