Lists: | Postg스포츠 토토SQLpgsql-patches |
---|
From: | David Fetter <david(at)fetter(dot)org> |
---|---|
To: | PG Hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Best practices: MERGE |
Date: | 2005-03-08 03:34:49 |
Message-ID: | 20050308033449.GC25775@fetter.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
Folks,
Although the SQL:2003 command MERGE has not yet been implemented in
PostgreSQL, I'm guessing that there are best practices for how to
implement the MERGE functionality.
To recap, MERGE means (roughly) INSERT the tuple if no tuple matches
certain criteria, otherwise UPDATE using similar criteria.
The "correct" solution, as far as I can tell, is to acquire a LOCK on
the table IN SHARE MODE at the beginning of the transaction, but this
has (at least for many applications) unacceptable performance
characteristics. Accepting that there is a slight risk of a race
condition when *not* locking the table at the beginning of the
transaction, what procedure minimizes this risk and recovers well from
said race condition, should it occur?
TIA for any hints, tips or pointers on this :)
Cheers,
D
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778
Remember to vote!
From: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> |
---|---|
To: | David Fetter <david(at)fetter(dot)org> |
Cc: | PG Hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: Best practices: MERGE |
Date: | 2005-03-08 03:45:19 |
Message-ID: | 422D1FCF.4080103@familyhealth.com.au |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
> The "correct" solution, as far as I can tell, is to acquire a LOCK on
> the table IN SHARE MODE at the beginning of the transaction, but this
> has (at least for many applications) unacceptable performance
> characteristics. Accepting that there is a slight risk of a race
> condition when *not* locking the table at the beginning of the
> transaction, what procedure minimizes this risk and recovers well from
> said race condition, should it occur?
IN SHARE MODE is not enough, you can get deadlocks. You require IN
SHARE ROW EXCLUSIVE MODE. other than that, it's a sucky solution
because it breaks concurrency. In pgsql 8, you can do it using pl/pgsql
exception handling.
Chris
From: | David Fetter <david(at)fetter(dot)org> |
---|---|
To: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> |
Cc: | PG Hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: Best practices: MERGE |
Date: | 2005-03-08 04:08:10 |
Message-ID: | 20050308040810.GA30907@fetter.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | Postg스포츠 토토SQL pgsql-patches |
On Tue, Mar 08, 2005 at 11:45:19AM +0800, Christopher Kings-Lynne wrote:
> >The "correct" solution, as far as I can tell, is to acquire a LOCK
> >on the table IN SHARE MODE at the beginning of the transaction, but
> >this has (at least for many applications) unacceptable performance
> >characteristics. Accepting that there is a slight risk of a race
> >condition when *not* locking the table at the beginning of the
> >transaction, what procedure minimizes this risk and recovers well
> >from said race condition, should it occur?
>
> IN SHARE MODE is not enough, you can get deadlocks. You require IN
> SHARE ROW EXCLUSIVE MODE. other than that, it's a sucky solution
> because it breaks concurrency. In pgsql 8, you can do it using
> pl/pgsql exception handling.
Luckily, PG 8 is available for this. Do you have a short example?
Cheers,
D
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778
Remember to vote!
From: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> |
---|---|
To: | David Fetter <david(at)fetter(dot)org> |
Cc: | PG Hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: Best practices: MERGE |
Date: | 2005-03-08 04:27:21 |
Message-ID: | 422D29A9.20103@familyhealth.com.au |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | Postg윈 토토SQL : pgsql-patches |
> Luckily, PG 8 is available for this. Do you have a short example?
No, and I think it should be in the manual as an example.
You will need to enter a loop that uses exception handling to detect
unique_violation.
Chris
From: | David Fetter <david(at)fetter(dot)org> |
---|---|
To: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> |
Cc: | PG Hackers <pgsql-hackers(at)postgresql(dot)org>, PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org> |
Subject: | Re: Best practices: MERGE |
Date: | 2005-03-08 08:25:46 |
Message-ID: | 20050308082546.GA31516@fetter.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | Postg윈 토토SQL : pgsql-patches |
On Tue, Mar 08, 2005 at 12:27:21PM +0800, Christopher Kings-Lynne wrote:
> >Luckily, PG 8 is available for this. Do you have a short example?
>
> No, and I think it should be in the manual as an example.
>
> You will need to enter a loop that uses exception handling to detect
> unique_violation.
Pursuant to an IRC discussion to which Dennis Bjorklund and
Christopher Kings-Lynne made most of the contributions, please find
enclosed an example patch demonstrating an UPSERT-like capability.
Cheers,
D
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778
Remember to vote!
Attachment | Content-Type | Size |
---|---|---|
upsert.diff | text/plain | 1.3 KB |
From: | Simon Riggs <simon(at)2ndquadrant(dot)com> |
---|---|
To: | David Fetter <david(at)fetter(dot)org> |
Cc: | PG Hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: Best practices: MERGE |
Date: | 2005-03-08 08:28:06 |
Message-ID: | 1110270486.6117.226.camel@localhost.localdomain |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
On Mon, 2005-03-07 at 19:34 -0800, David Fetter wrote:
> Although the SQL:2003 command MERGE has not yet been implemented in
> PostgreSQL, I'm guessing that there are best practices for how to
> implement the MERGE functionality.
>
> To recap, MERGE means (roughly) INSERT the tuple if no tuple matches
> certain criteria, otherwise UPDATE using similar criteria.
Don't understand that way round...
I thought the logic was:
UPDATE WHERE ..... (locate row)
IF NOT FOUND THEN
INSERT (new row)
You can create a procedure to do that, but MERGE would work better.
ISTM that would require writing some new code that was a mix of
heap_update and heap_insert logic for the low level stuff would be
required. The existing heap_update code is most similar, since the logic
is roughly
UPDATE WHERE.... (locate row)
IF FOUND THEN
INSERT (new row version)
though with various changes to row visibility stuff.
One might aim to do this in two stages:
1. initially support a single row upsert such as MySQL's REPLACE command
2. a full implementation of MERGE that used set logic as per the spec
...
Best Regards, Simon Riggs
From: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> |
---|---|
To: | Simon Riggs <simon(at)2ndquadrant(dot)com> |
Cc: | David Fetter <david(at)fetter(dot)org>, PG Hackers <pgsql-hackers(at)postgresql(dot)org> |
Subject: | Re: Best practices: MERGE |
Date: | 2005-03-08 08:32:57 |
Message-ID: | 422D6339.8010502@familyhealth.com.au |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
> You can create a procedure to do that, but MERGE would work better.
>
> ISTM that would require writing some new code that was a mix of
> heap_update and heap_insert logic for the low level stuff would be
> required. The existing heap_update code is most similar, since the logic
> is roughly
>
> UPDATE WHERE.... (locate row)
> IF FOUND THEN
> INSERT (new row version)
>
> though with various changes to row visibility stuff.
>
> One might aim to do this in two stages:
> 1. initially support a single row upsert such as MySQL's REPLACE command
> 2. a full implementation of MERGE that used set logic as per the spec
>
> ...
The main issue is dealing with merging into unique index race conditions.
Chris
From: | Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> |
---|---|
To: | David Fetter <david(at)fetter(dot)org> |
Cc: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>, PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org> |
Subject: | Re: [HACKERS] Best practices: MERGE |
Date: | 2005-04-19 03:37:30 |
Message-ID: | 200504190337.j3J3bUq28952@candle.pha.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
Patch applied. Thanks. Sorry for the delay in applying.
---------------------------------------------------------------------------
David Fetter wrote:
> On Tue, Mar 08, 2005 at 12:27:21PM +0800, Christopher Kings-Lynne wrote:
> > >Luckily, PG 8 is available for this. Do you have a short example?
> >
> > No, and I think it should be in the manual as an example.
> >
> > You will need to enter a loop that uses exception handling to detect
> > unique_violation.
>
> Pursuant to an IRC discussion to which Dennis Bjorklund and
> Christopher Kings-Lynne made most of the contributions, please find
> enclosed an example patch demonstrating an UPSERT-like capability.
>
> Cheers,
> D
> --
> David Fetter david(at)fetter(dot)org http://fetter.org/
> phone: +1 510 893 6100 mobile: +1 415 235 3778
>
> Remember to vote!
[ Attachment, skipping... ]
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: the planner will ignore your desire to choose an index scan if your
> joining column's datatypes do not match
--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
From: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> |
---|---|
To: | Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> |
Cc: | David Fetter <david(at)fetter(dot)org>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>, PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org> |
Subject: | Re: [HACKERS] Best practices: MERGE |
Date: | 2005-04-19 03:44:40 |
Message-ID: | 42647EA8.2080307@familyhealth.com.au |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
Is that broken?
http://momjian.postgresql.org/main/writings/pgsql/sgml/build.html
Chris
Bruce Momjian wrote:
> Patch applied. Thanks. Sorry for the delay in applying.
>
> ---------------------------------------------------------------------------
>
>
> David Fetter wrote:
>
>>On Tue, Mar 08, 2005 at 12:27:21PM +0800, Christopher Kings-Lynne wrote:
>>
>>>>Luckily, PG 8 is available for this. Do you have a short example?
>>>
>>>No, and I think it should be in the manual as an example.
>>>
>>>You will need to enter a loop that uses exception handling to detect
>>>unique_violation.
>>
>>Pursuant to an IRC discussion to which Dennis Bjorklund and
>>Christopher Kings-Lynne made most of the contributions, please find
>>enclosed an example patch demonstrating an UPSERT-like capability.
>>
>>Cheers,
>>D
>>--
>>David Fetter david(at)fetter(dot)org http://fetter.org/
>>phone: +1 510 893 6100 mobile: +1 415 235 3778
>>
>>Remember to vote!
>
>
> [ Attachment, skipping... ]
>
>
>>---------------------------(end of broadcast)---------------------------
>>TIP 9: the planner will ignore your desire to choose an index scan if your
>> joining column's datatypes do not match
>
>
From: | Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> |
---|---|
To: | Christopher Kings-Lynne <chriskl(at)familyhealth(dot)com(dot)au> |
Cc: | David Fetter <david(at)fetter(dot)org>, PG Hackers <pgsql-hackers(at)postgresql(dot)org>, PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org> |
Subject: | Re: [HACKERS] Best practices: MERGE |
Date: | 2005-04-19 03:55:44 |
Message-ID: | 200504190355.j3J3tiu02153@candle.pha.pa.us |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
Thanks, fixed.
---------------------------------------------------------------------------
Christopher Kings-Lynne wrote:
> Is that broken?
>
> http://momjian.postgresql.org/main/writings/pgsql/sgml/build.html
>
> Chris
>
> Bruce Momjian wrote:
> > Patch applied. Thanks. Sorry for the delay in applying.
> >
> > ---------------------------------------------------------------------------
> >
> >
> > David Fetter wrote:
> >
> >>On Tue, Mar 08, 2005 at 12:27:21PM +0800, Christopher Kings-Lynne wrote:
> >>
> >>>>Luckily, PG 8 is available for this. Do you have a short example?
> >>>
> >>>No, and I think it should be in the manual as an example.
> >>>
> >>>You will need to enter a loop that uses exception handling to detect
> >>>unique_violation.
> >>
> >>Pursuant to an IRC discussion to which Dennis Bjorklund and
> >>Christopher Kings-Lynne made most of the contributions, please find
> >>enclosed an example patch demonstrating an UPSERT-like capability.
> >>
> >>Cheers,
> >>D
> >>--
> >>David Fetter david(at)fetter(dot)org http://fetter.org/
> >>phone: +1 510 893 6100 mobile: +1 415 235 3778
> >>
> >>Remember to vote!
> >
> >
> > [ Attachment, skipping... ]
> >
> >
> >>---------------------------(end of broadcast)---------------------------
> >>TIP 9: the planner will ignore your desire to choose an index scan if your
> >> joining column's datatypes do not match
> >
> >
>
--
Bruce Momjian | http://candle.pha.pa.us
pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
+ If your life is a hard drive, | 13 Roberts Road
+ Christ can be your backup. | Newtown Square, Pennsylvania 19073
From: | David Fetter <david(at)fetter(dot)org> |
---|---|
To: | Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us> |
Cc: | PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org> |
Subject: | Re: [HACKERS] Best practices: MERGE |
Date: | 2005-10-04 16:41:03 |
Message-ID: | 20051004164103.GC11397@fetter.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
On Mon, Apr 18, 2005 at 11:55:44PM -0400, Bruce Momjian wrote:
>
> Thanks, fixed.
Could you apply this to the 8.0 docs, too? The exception handling
works in 8.0, and I know at least two places where it's in production
:)
Cheers,
D
>
> ---------------------------------------------------------------------------
>
> Christopher Kings-Lynne wrote:
> > Is that broken?
> >
> > http://momjian.postgresql.org/main/writings/pgsql/sgml/build.html
> >
> > Chris
> >
> > Bruce Momjian wrote:
> > > Patch applied. Thanks. Sorry for the delay in applying.
> > >
> > > ---------------------------------------------------------------------------
> > >
> > >
> > > David Fetter wrote:
> > >
> > >>On Tue, Mar 08, 2005 at 12:27:21PM +0800, Christopher Kings-Lynne wrote:
> > >>
> > >>>>Luckily, PG 8 is available for this. Do you have a short example?
> > >>>
> > >>>No, and I think it should be in the manual as an example.
> > >>>
> > >>>You will need to enter a loop that uses exception handling to detect
> > >>>unique_violation.
> > >>
> > >>Pursuant to an IRC discussion to which Dennis Bjorklund and
> > >>Christopher Kings-Lynne made most of the contributions, please find
> > >>enclosed an example patch demonstrating an UPSERT-like capability.
> > >>
> > >>Cheers,
> > >>D
> > >>--
> > >>David Fetter david(at)fetter(dot)org http://fetter.org/
> > >>phone: +1 510 893 6100 mobile: +1 415 235 3778
> > >>
> > >>Remember to vote!
> > >
> > >
> > > [ Attachment, skipping... ]
> > >
> > >
> > >>---------------------------(end of broadcast)---------------------------
> > >>TIP 9: the planner will ignore your desire to choose an index scan if your
> > >> joining column's datatypes do not match
> > >
> > >
> >
>
> --
> Bruce Momjian | http://candle.pha.pa.us
> pgman(at)candle(dot)pha(dot)pa(dot)us | (610) 359-1001
> + If your life is a hard drive, | 13 Roberts Road
> + Christ can be your backup. | Newtown Square, Pennsylvania 19073
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org
--
David Fetter david(at)fetter(dot)org http://fetter.org/
phone: +1 510 893 6100 mobile: +1 415 235 3778
Remember to vote!
From: | Neil Conway <neilc(at)samurai(dot)com> |
---|---|
To: | David Fetter <david(at)fetter(dot)org> |
Cc: | Bruce Momjian <pgman(at)candle(dot)pha(dot)pa(dot)us>, PostgreSQL Patches <pgsql-patches(at)postgresql(dot)org> |
Subject: | Re: [HACKERS] Best practices: MERGE |
Date: | 2005-10-06 21:23:57 |
Message-ID: | 1128633837.23118.28.camel@localhost.localdomain |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-hackers pgsql-patches |
On Tue, 2005-04-10 at 09:41 -0700, David Fetter wrote:
> Could you apply this to the 8.0 docs, too?
Applied, with fixes.
-Neil