pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedconditional insert
36+ messages / 18 participants
[nested] [flat]
* conditional insert
@ 2011-09-05 09:38 Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:45 ` Re: conditional insert Achilleas Mantzios <achill@matrix.gatewaynet.com>
2011-09-05 09:48 ` Re: conditional insert Raymond O'Donnell <rod@iol.ie>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:28 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
0 siblings, 4 replies; 36+ messages in thread
From: Pau Marc Muñoz Torres @ 2011-09-05 09:38 UTC (permalink / raw)
To: pgsql-general
Hi follk
i trying to performe a conditional insert into a table, indeed, what i'm
trying to do is not insert a record into the table if that record exist
googleling i found something like
insert into XX values (1,2,3) where not exist (select ....);
but i'm having and error near where...
anyone knows how do i can perfome this insert?
thanks
p
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
@ 2011-09-05 09:45 ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
3 siblings, 0 replies; 36+ messages in thread
From: Achilleas Mantzios @ 2011-09-05 09:45 UTC (permalink / raw)
To: pgsql-general
Στις Monday 05 September 2011 12:38:34 ο/η Pau Marc Muñoz Torres έγραψε:
> Hi follk
>
> i trying to performe a conditional insert into a table, indeed, what i'm
> trying to do is not insert a record into the table if that record exist
>
thats why primary/unique keys are for.
isolate the columns which you consider to be a correct unique key
and create a unique key on them.
thereis no notion of conditional insert that i know of.
> googleling i found something like
>
> insert into XX values (1,2,3) where not exist (select ....);
>
> but i'm having and error near where...
>
> anyone knows how do i can perfome this insert?
>
> thanks
>
> p
>
--
Achilleas Mantzios
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
@ 2011-09-05 09:48 ` Raymond O'Donnell <rod@iol.ie>
3 siblings, 0 replies; 36+ messages in thread
From: Raymond O'Donnell @ 2011-09-05 09:48 UTC (permalink / raw)
To: Pau Marc Muñoz Torres <paumarc@gmail.com>; +Cc: pgsql-general
On 05/09/2011 10:38, Pau Marc Muñoz Torres wrote:
> Hi follk
>
> i trying to performe a conditional insert into a table, indeed, what
> i'm trying to do is not insert a record into the table if that record exist
>
> googleling i found something like
>
> insert into XX values (1,2,3) where not exist (select ....);
>
> but i'm having and error near where...
>
Shouldn't it be EXISTS, not EXIST?
Anyway, what is the error you're getting?
Ray.
--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
@ 2011-09-05 09:51 ` Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
3 siblings, 1 reply; 36+ messages in thread
From: Thomas Kellerer @ 2011-09-05 09:51 UTC (permalink / raw)
To: pgsql-general
Pau Marc Muñoz Torres, 05.09.2011 11:38:
> Hi follk
>
> i trying to performe a conditional insert into a table, indeed, what i'm trying to do is not insert a record into the table if that record exist
>
> googleling i found something like
>
> insert into XX values (1,2,3) where not exist (select ....);
>
> but i'm having and error near where...
> anyone knows how do i can perfome this insert?
INSERT INTO xxx
SELECT 1,2,3
WHERE NOT EXISTS (SELECT ...)
Regards
Thomas
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
@ 2011-09-05 10:37 ` Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
0 siblings, 1 reply; 36+ messages in thread
From: Pau Marc Muñoz Torres @ 2011-09-05 10:37 UTC (permalink / raw)
To: pgsql-general
i don't see it clear, let me put an example
i got the following table
molec varchar(30)
seq varchar(100)
where I insert my values
lets image that i have a record introduced as ('ubq', 'aadgylpittrs')
how i can prevent to insert another record where molec='ubq' ?
thanks
2011/9/5 Thomas Kellerer <spam_eater@gmx.net>
> Pau Marc Muñoz Torres, 05.09.2011 11:38:
>
> Hi follk
>>
>> i trying to performe a conditional insert into a table, indeed, what
>> i'm trying to do is not insert a record into the table if that record exist
>>
>> googleling i found something like
>>
>> insert into XX values (1,2,3) where not exist (select ....);
>>
>> but i'm having and error near where...
>> anyone knows how do i can perfome this insert?
>>
>
> INSERT INTO xxx
> SELECT 1,2,3
> WHERE NOT EXISTS (SELECT ...)
>
> Regards
> Thomas
>
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/**mailpref/pgsql-general<http://www.postgresql.org/mailpref/pgsql-gener...;
>
--
*Pau Marc Muñoz Torres*
Laboratori de Biologia Computacional
Institut de Biotecnologia i Biomedicina Vicent Villar
Universitat Autonoma de Barcelona
E-08193 Bellaterra (Barcelona)
telèfon: (+34)935 86 89 39*
Email : paumarc.munoz@bioinf.uab.cat*
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
@ 2011-09-05 10:42 ` Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
0 siblings, 1 reply; 36+ messages in thread
From: Sim Zacks @ 2011-09-05 10:42 UTC (permalink / raw)
To: pgsql-general
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html style="direction: ltr;">
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<style>body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
</head>
<body style="direction: ltr;"
bidimailui-detected-decoding-type="latin-charset" bgcolor="#ffffff"
text="#000000">
On 09/05/2011 01:37 PM, Pau Marc Muñoz Torres wrote:
<blockquote
cite="mid:CADFuJLi3K0X1A7OwR+9_neWjKCEXtYYyMP_n=x5z=D2+GNCTNw@mail.gmail.com"
type="cite">i don't see it clear, let me put an example<br>
<br>
i got the following table<br>
<br>
molec varchar(30)<br>
seq varchar(100)<br>
<br>
where I insert my values<br>
<br>
lets image that i have a record introduced as ('ubq',
'aadgylpittrs')<br>
<br>
how i can prevent to insert another record where molec='ubq' ?<br>
<br>
thanks<br>
<br>
</blockquote>
Either put a unique constraint on molec or do<br>
insert into tbl(molec,seq)<br>
select 'ubq', 'aadgylpittrs' where not exists(select molec from tbl
where molec='ubq')<br>
</body>
</html>
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
@ 2011-09-05 10:47 ` Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
0 siblings, 1 reply; 36+ messages in thread
From: Pau Marc Muñoz Torres @ 2011-09-05 10:47 UTC (permalink / raw)
To: Sim Zacks <sim@compulab.co.il>; +Cc: pgsql-general
Ok , thanks Sim, now i see it
P
2011/9/5 Sim Zacks <sim@compulab.co.il>
> **
> On 09/05/2011 01:37 PM, Pau Marc Muñoz Torres wrote:
>
> i don't see it clear, let me put an example
>
> i got the following table
>
> molec varchar(30)
> seq varchar(100)
>
> where I insert my values
>
> lets image that i have a record introduced as ('ubq', 'aadgylpittrs')
>
> how i can prevent to insert another record where molec='ubq' ?
>
> thanks
>
> Either put a unique constraint on molec or do
> insert into tbl(molec,seq)
> select 'ubq', 'aadgylpittrs' where not exists(select molec from tbl where
> molec='ubq')
>
--
*Pau Marc Muñoz Torres*
Laboratori de Biologia Computacional
Institut de Biotecnologia i Biomedicina Vicent Villar
Universitat Autonoma de Barcelona
E-08193 Bellaterra (Barcelona)
telèfon: (+34)935 86 89 39*
Email : paumarc.munoz@bioinf.uab.cat*
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
@ 2011-09-05 11:02 ` J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
0 siblings, 1 reply; 36+ messages in thread
From: J. Hondius @ 2011-09-05 11:02 UTC (permalink / raw)
To: Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
I agree that there are better ways to do this.<br>
But for me this works. (legacy driven situation)<br>
<br>
INSERT INTO tbinitialisatie (col1, col2)
<br>
SELECT 'x', 'y'
<br>
FROM tbinitialisatie
<br>
WHERE not exists (select * from tbinitialisatie where col1 = 'x' and
col2 = 'y')
<br>
LIMIT 1
<br>
<br>
<br>
Pau Marc Muñoz Torres schreef:
<blockquote
cite="mid:CADFuJLjfHUMEuYTPKgG6GtJMfzJeeq3-bkt162RywRVqc7OYgg@mail.gmail.com"
type="cite">Ok , thanks Sim, now i see it<br>
<br>
P<br>
<br>
<div class="gmail_quote">2011/9/5 Sim Zacks <span dir="ltr"><<a
moz-do-not-send="true" href="mailto:sim@compulab.co.il">sim@compulab.co.il</a>></span><br>
<blockquote class="gmail_quote"
style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div style="direction: ltr;" bgcolor="#ffffff" text="#000000">
<div class="im"> On 09/05/2011 01:37 PM, Pau Marc Muñoz Torres
wrote:
<blockquote type="cite">i don't see it clear, let me put an example<br>
<br>
i got the following table<br>
<br>
molec varchar(30)<br>
seq varchar(100)<br>
<br>
where I insert my values<br>
<br>
lets image that i have a record introduced as ('ubq', 'aadgylpittrs')<br>
<br>
how i can prevent to insert another record where molec='ubq' ?<br>
<br>
thanks<br>
<br>
</blockquote>
</div>
Either put a unique constraint on molec or do<br>
insert into tbl(molec,seq)<br>
select 'ubq', 'aadgylpittrs' where not exists(select molec from tbl
where molec='ubq')<br>
</div>
</blockquote>
</div>
<br>
<br clear="all">
<br>
-- <br>
<b>Pau Marc Muñoz Torres</b><br>
<br>
Laboratori de Biologia Computacional <br>
Institut de Biotecnologia i Biomedicina Vicent Villar
<br>
Universitat Autonoma de Barcelona<br>
E-08193 Bellaterra (Barcelona)<br>
<br>
telèfon: (+34)935 86 89 39<b><br>
Email : <a moz-do-not-send="true"
href="mailto:paumarc.munoz@bioinf.uab.cat" target="_blank">paumarc.munoz@bioinf.uab.cat</a></b><br>
</blockquote>
</body>
</html>
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
@ 2011-09-06 18:50 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
0 siblings, 1 reply; 36+ messages in thread
From: Lincoln Yeoh @ 2011-09-06 18:50 UTC (permalink / raw)
To: jhondius@rem.nl; Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
At 07:02 PM 9/5/2011, J. Hondius wrote:
>I agree that there are better ways to do this.
>But for me this works. (legacy driven situation)
>
>INSERT INTO tbinitialisatie (col1, col2)
> SELECT 'x', 'y'
> FROM tbinitialisatie
> WHERE not exists (select * from tbinitialisatie where col1 = 'x'
> and col2 = 'y')
> LIMIT 1
Hi,
That does not work 100%. Try it with two psql instances.
Do:
*** psql #1
begin;
INSERT INTO tbinitialisatie (col1, col2)
SELECT 'x', 'y'
FROM tbinitialisatie
WHERE not exists (select * from tbinitialisatie where col1 = 'x'
and col2 = 'y')
LIMIT 1 ;
*** psql #2
begin;
INSERT INTO tbinitialisatie (col1, col2)
SELECT 'x', 'y'
FROM tbinitialisatie
WHERE not exists (select * from tbinitialisatie where col1 = 'x'
and col2 = 'y')
LIMIT 1 ;
commit;
*** psql #1
commit;
You should find duplicate inserts.
In most cases the "begin" and "commit" are very close together so you
won't notice the problem. But one day you might get unlucky.
Your options are:
a) put a unique constraint and handle the insert errors when they occur
b) lock the entire table first (affects performance: blocks all
selects on that table)
c) use a lock elsewhere (but this requires all applications using the
database to cooperate and use the lock).
d) wait for SQL MERGE to be implemented ( but from what I see the
current proposal seems to require a) or b) anyway:
http://wiki.postgresql.org/wiki/SQL_MERGE )
You could do both a) and b) too. Or both a) and c) (if you don't want
insert errors in the cooperating apps and want to allow other selects
during the transaction).
Regards,
Link.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
@ 2011-09-06 20:45 ` Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
0 siblings, 1 reply; 36+ messages in thread
From: Merlin Moncure @ 2011-09-06 20:45 UTC (permalink / raw)
To: Lincoln Yeoh <lyeoh@pop.jaring.my>; +Cc: jhondius@rem.nl; Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
On Tue, Sep 6, 2011 at 1:50 PM, Lincoln Yeoh <lyeoh@pop.jaring.my> wrote:
> At 07:02 PM 9/5/2011, J. Hondius wrote:
>>
>> I agree that there are better ways to do this.
>> But for me this works. (legacy driven situation)
>>
>> INSERT INTO tbinitialisatie (col1, col2)
>> SELECT 'x', 'y'
>> FROM tbinitialisatie
>> WHERE not exists (select * from tbinitialisatie where col1 = 'x' and col2
>> = 'y')
>> LIMIT 1
>
> Hi,
>
> That does not work 100%. Try it with two psql instances.
>
> Do:
> *** psql #1
> begin;
> INSERT INTO tbinitialisatie (col1, col2)
> SELECT 'x', 'y'
> FROM tbinitialisatie
> WHERE not exists (select * from tbinitialisatie where col1 = 'x' and col2 =
> 'y')
> LIMIT 1 ;
>
> *** psql #2
> begin;
> INSERT INTO tbinitialisatie (col1, col2)
> SELECT 'x', 'y'
> FROM tbinitialisatie
> WHERE not exists (select * from tbinitialisatie where col1 = 'x' and col2 =
> 'y')
> LIMIT 1 ;
> commit;
>
> *** psql #1
> commit;
>
> You should find duplicate inserts.
>
> In most cases the "begin" and "commit" are very close together so you won't
> notice the problem. But one day you might get unlucky.
>
> Your options are:
> a) put a unique constraint and handle the insert errors when they occur
> b) lock the entire table first (affects performance: blocks all selects on
> that table)
> c) use a lock elsewhere (but this requires all applications using the
> database to cooperate and use the lock).
> d) wait for SQL MERGE to be implemented ( but from what I see the current
> proposal seems to require a) or b) anyway:
> http://wiki.postgresql.org/wiki/SQL_MERGE )
b) doesn't block reads if you lock in EXCLUSIVE mode. a) is the best
way to go if you prefer to handle errors on the client and/or
concurrency is important...c) otherwise.
merlin
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
@ 2011-09-06 21:23 ` Merlin Moncure <mmoncure@gmail.com>
2011-09-07 15:45 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-07 19:51 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
0 siblings, 2 replies; 36+ messages in thread
From: Merlin Moncure @ 2011-09-06 21:23 UTC (permalink / raw)
To: Lincoln Yeoh <lyeoh@pop.jaring.my>; +Cc: jhondius@rem.nl; Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
On Tue, Sep 6, 2011 at 3:45 PM, Merlin Moncure <mmoncure@gmail.com> wrote:
> On Tue, Sep 6, 2011 at 1:50 PM, Lincoln Yeoh <lyeoh@pop.jaring.my> wrote:
>> At 07:02 PM 9/5/2011, J. Hondius wrote:
>>>
>>> I agree that there are better ways to do this.
>>> But for me this works. (legacy driven situation)
>>>
>>> INSERT INTO tbinitialisatie (col1, col2)
>>> SELECT 'x', 'y'
>>> FROM tbinitialisatie
>>> WHERE not exists (select * from tbinitialisatie where col1 = 'x' and col2
>>> = 'y')
>>> LIMIT 1
>>
>> Hi,
>>
>> That does not work 100%. Try it with two psql instances.
>>
>> Do:
>> *** psql #1
>> begin;
>> INSERT INTO tbinitialisatie (col1, col2)
>> SELECT 'x', 'y'
>> FROM tbinitialisatie
>> WHERE not exists (select * from tbinitialisatie where col1 = 'x' and col2 =
>> 'y')
>> LIMIT 1 ;
>>
>> *** psql #2
>> begin;
>> INSERT INTO tbinitialisatie (col1, col2)
>> SELECT 'x', 'y'
>> FROM tbinitialisatie
>> WHERE not exists (select * from tbinitialisatie where col1 = 'x' and col2 =
>> 'y')
>> LIMIT 1 ;
>> commit;
>>
>> *** psql #1
>> commit;
>>
>> You should find duplicate inserts.
>>
>> In most cases the "begin" and "commit" are very close together so you won't
>> notice the problem. But one day you might get unlucky.
>>
>> Your options are:
>> a) put a unique constraint and handle the insert errors when they occur
>> b) lock the entire table first (affects performance: blocks all selects on
>> that table)
>> c) use a lock elsewhere (but this requires all applications using the
>> database to cooperate and use the lock).
>> d) wait for SQL MERGE to be implemented ( but from what I see the current
>> proposal seems to require a) or b) anyway:
>> http://wiki.postgresql.org/wiki/SQL_MERGE )
>
> b) doesn't block reads if you lock in EXCLUSIVE mode. a) is the best
> way to go if you prefer to handle errors on the client and/or
> concurrency is important...c) otherwise.
whoops! meant to say b) otherwise! As far as c) goes, that is
essentially an advisory lock for the purpose -- using advisory locks
in place of mvcc locks is pretty weak sauce -- they should be used
when what you are locking doesn't follow mvcc rules.
merlin
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
@ 2011-09-07 15:45 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-07 15:50 ` Re: conditional insert Andrew Sullivan <ajs@crankycanuck.ca>
1 sibling, 1 reply; 36+ messages in thread
From: Lincoln Yeoh @ 2011-09-07 15:45 UTC (permalink / raw)
To: Merlin Moncure <mmoncure@gmail.com>; +Cc: jhondius@rem.nl; Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
At 05:23 AM 9/7/2011, Merlin Moncure wrote:
>On Tue, Sep 6, 2011 at 3:45 PM, Merlin Moncure <mmoncure@gmail.com> wrote:
>
> > b) doesn't block reads if you lock in EXCLUSIVE mode. a) is the best
> > way to go if you prefer to handle errors on the client and/or
> > concurrency is important...c) otherwise.
>
>whoops! meant to say b) otherwise! As far as c) goes, that is
>essentially an advisory lock for the purpose -- using advisory locks
>in place of mvcc locks is pretty weak sauce -- they should be used
>when what you are locking doesn't follow mvcc rules.
>
>merlin
Don't you have to block SELECTs so that the SELECTs get serialized?
Otherwise concurrent SELECTs can occur at the same time, find no
existing rows, then "all" the inserts proceed and you get errors (or dupes).
That's how Postgresql still works right? I haven't really been keeping up.
From what I see this (UPSERT/MERGE) has been a common problem/query
over the years but it's not in a Postgresql FAQ and many people seem
to be using methods that don't actually work. Google shows that many
are even recommending those methods to others. Postgresql might still
get blamed for the resulting problems.
Regards,
Link.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 15:45 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
@ 2011-09-07 15:50 ` Andrew Sullivan <ajs@crankycanuck.ca>
0 siblings, 0 replies; 36+ messages in thread
From: Andrew Sullivan @ 2011-09-07 15:50 UTC (permalink / raw)
To: pgsql-general
On Wed, Sep 07, 2011 at 11:45:11PM +0800, Lincoln Yeoh wrote:
> Don't you have to block SELECTs so that the SELECTs get serialized?
If you want to do that, why wouldn't you just use serializable mode?
A
--
Andrew Sullivan
ajs@crankycanuck.ca
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
@ 2011-09-07 19:51 ` Merlin Moncure <mmoncure@gmail.com>
2011-09-07 20:04 ` Re: conditional insert Andrew Sullivan <ajs@crankycanuck.ca>
2011-09-08 14:07 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-08 14:14 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-08 14:19 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
1 sibling, 4 replies; 36+ messages in thread
From: Merlin Moncure @ 2011-09-07 19:51 UTC (permalink / raw)
To: Lincoln Yeoh <lyeoh@pop.jaring.my>; +Cc: jhondius@rem.nl; Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
On Wed, Sep 7, 2011 at 10:45 AM, Lincoln Yeoh <lyeoh@pop.jaring.my> wrote:
> At 05:23 AM 9/7/2011, Merlin Moncure wrote:
>>
>> On Tue, Sep 6, 2011 at 3:45 PM, Merlin Moncure <mmoncure@gmail.com> wrote:
>>
>> > b) doesn't block reads if you lock in EXCLUSIVE mode. a) is the best
>> > way to go if you prefer to handle errors on the client and/or
>> > concurrency is important...c) otherwise.
>>
>> whoops! meant to say b) otherwise! As far as c) goes, that is
>> essentially an advisory lock for the purpose -- using advisory locks
>> in place of mvcc locks is pretty weak sauce -- they should be used
>> when what you are locking doesn't follow mvcc rules.
>>
>> merlin
>
> Don't you have to block SELECTs so that the SELECTs get serialized?
> Otherwise concurrent SELECTs can occur at the same time, find no existing
> rows, then "all" the inserts proceed and you get errors (or dupes).
>
> That's how Postgresql still works right? I haven't really been keeping up.
yeah -- but you only need to block selects if you are selecting in the
inserting transaction (this is not a full upsert). if both writers
are doing:
begin;
lock table foo exclusive;
insert into foo select ... where ...;
commit;
is good enough. btw even if you are doing upsert pattern
(lock...select for update...insert/update), you'd be fine with
straight exclusive locks because the 'for update' lock takes a higher
lock that is blocked by exclusive. A basic rule of thumb is to try
and not fully block readers unless absolutely necessary...basically
maintenance operations.
> From what I see this (UPSERT/MERGE) has been a common problem/query over the
> years but it's not in a Postgresql FAQ and many people seem to be using
> methods that don't actually work. Google shows that many are even
> recommending those methods to others. Postgresql might still get blamed for
> the resulting problems.
yeah -- there are two basic ways to do upsert -- a) table lock b) row
lock with loop/retry (either in app or server side via procedure). I
greatly prefer a) for simplicity's sake unless you are shooting for
maximum possible concurrency.
@andrew s: going SERIALIZABLE doesn't help if you trying to eliminate
cases that would push you into retrying the transaction.
merlin
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 19:51 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
@ 2011-09-07 20:04 ` Andrew Sullivan <ajs@crankycanuck.ca>
2011-09-07 20:50 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-08 14:31 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
3 siblings, 2 replies; 36+ messages in thread
From: Andrew Sullivan @ 2011-09-07 20:04 UTC (permalink / raw)
To: pgsql-general
On Wed, Sep 07, 2011 at 02:51:32PM -0500, Merlin Moncure wrote:
>
> @andrew s: going SERIALIZABLE doesn't help if you trying to eliminate
> cases that would push you into retrying the transaction.
Well, no, of course. But why not catch the failure and retry? I
guess I just don't get the problem, since I hear people say this all
the time. (I mean, I've also seen places where 'upsert' would be
cool, but it doesn't seem trivial to do in a general way and you can
do this with catch-serialization-error-and-retry, I think?)
A
--
Andrew Sullivan
ajs@crankycanuck.ca
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 19:51 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 20:04 ` Re: conditional insert Andrew Sullivan <ajs@crankycanuck.ca>
@ 2011-09-07 20:50 ` Merlin Moncure <mmoncure@gmail.com>
1 sibling, 0 replies; 36+ messages in thread
From: Merlin Moncure @ 2011-09-07 20:50 UTC (permalink / raw)
To: Andrew Sullivan <ajs@crankycanuck.ca>; pgsql-general
On Wed, Sep 7, 2011 at 3:04 PM, Andrew Sullivan <ajs@crankycanuck.ca> wrote:
> On Wed, Sep 07, 2011 at 02:51:32PM -0500, Merlin Moncure wrote:
>>
>> @andrew s: going SERIALIZABLE doesn't help if you trying to eliminate
>> cases that would push you into retrying the transaction.
>
> Well, no, of course. But why not catch the failure and retry? I
> guess I just don't get the problem, since I hear people say this all
> the time. (I mean, I've also seen places where 'upsert' would be
> cool, but it doesn't seem trivial to do in a general way and you can
> do this with catch-serialization-error-and-retry, I think?)
good points, but consider that savepoints have a certain amount of
performance overhead, and there may be some dependent client side
processing that is non-trivial to roll back. Also, if you have a lot
of contention, things can get nasty very quickly -- a full lock is
reliable, simple, and fast, and can be done in one round trip.
Any solution that doesn't have loops is inherently more robust than
one that does. I'll rest my case on that point -- consider very
carefully that the upsert loop example presented in the docs for years
was vulnerable to an infinite loop condition that was caught by one of
our users in production. That completely turned me off towards that
general method of dealing with these types of problems unless there is
really no other reasonable way to do it.
merlin
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 19:51 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 20:04 ` Re: conditional insert Andrew Sullivan <ajs@crankycanuck.ca>
@ 2011-09-08 14:31 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-08 15:12 ` Re: conditional insert Andrew Sullivan <ajs@crankycanuck.ca>
1 sibling, 1 reply; 36+ messages in thread
From: Lincoln Yeoh @ 2011-09-08 14:31 UTC (permalink / raw)
To: Andrew Sullivan <ajs@crankycanuck.ca>; pgsql-general
At 04:04 AM 9/8/2011, Andrew Sullivan wrote:
>On Wed, Sep 07, 2011 at 02:51:32PM -0500, Merlin Moncure wrote:
> >
> > @andrew s: going SERIALIZABLE doesn't help if you trying to eliminate
> > cases that would push you into retrying the transaction.
>
>Well, no, of course. But why not catch the failure and retry? I
>guess I just don't get the problem, since I hear people say this all
>the time. (I mean, I've also seen places where 'upsert' would be
>cool, but it doesn't seem trivial to do in a general way and you can
>do this with catch-serialization-error-and-retry, I think?)
Doesn't catching the failure and retrying mean writing more code?
Need extra code:
1) to handle the savepoint.
2) to correctly distinguish between "retryable" errors and
"nonretryable" errors.
3) to retry transactions.
More code to debug, test, document and support :).
In contrast, the "lock table, insert if row does not exist, update if
it exists" and rollback everything if "stuff happens" seems simpler
to do correctly.
I personally prefer to leave as much code writing to others (e.g.
those writing Postgresql, programming languages and libraries),
because I assume they are much better at writing code than I am. Plus
I usually don't have to document and support their code ;).
Regards,
Link.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 19:51 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 20:04 ` Re: conditional insert Andrew Sullivan <ajs@crankycanuck.ca>
2011-09-08 14:31 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
@ 2011-09-08 15:12 ` Andrew Sullivan <ajs@crankycanuck.ca>
0 siblings, 0 replies; 36+ messages in thread
From: Andrew Sullivan @ 2011-09-08 15:12 UTC (permalink / raw)
To: pgsql-general
On Thu, Sep 08, 2011 at 10:31:39PM +0800, Lincoln Yeoh wrote:
>
> Doesn't catching the failure and retrying mean writing more code?
Well, yes. OTOH, if you want to use "upsert" and you have to use
other database systems too, then you'll need that other code also,
since it's not standard.
There isn't an easy answer here.
A
--
Andrew Sullivan
ajs@crankycanuck.ca
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 19:51 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
@ 2011-09-08 14:07 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
3 siblings, 0 replies; 36+ messages in thread
From: Lincoln Yeoh @ 2011-09-08 14:07 UTC (permalink / raw)
To: Merlin Moncure <mmoncure@gmail.com>; +Cc: jhondius@rem.nl; Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
At 03:51 AM 9/8/2011, Merlin Moncure wrote:
>yeah -- but you only need to block selects if you are selecting in the
>inserting transaction (this is not a full upsert). if both writers
>are doing:
>begin;
>lock table foo exclusive;
>insert into foo select ... where ...;
>commit;
>
>is good enough. btw even if you are doing upsert pattern
>(lock...select for update...insert/update), you'd be fine with
>straight exclusive locks because the 'for update' lock takes a higher
>lock that is blocked by exclusive. A basic rule of thumb is to try
>and not fully block readers unless absolutely necessary...basically
>maintenance operations.
Yeah it works if all the inserters do the lock table (or select for
update), and provides better performance.
But if you're paranoid and lazy - a full lock will ensure that your
code won't get dupe errors even if someone else's code or manual
control doesn't do the lock table (they might get the dupe errors[1],
but that's their problem ;) ). So your code can safely assume that
any DB errors that occur are those that deserve a full rollback of
everything (which is what Postgresql "likes" by default). This means
fewer scenarios to handle so you don't need to write as much code,
nor document and support as much code ;).
Link.
[1] I'm assuming a unique constraint is present- the locking is to
simplify things.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 19:51 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
@ 2011-09-08 14:14 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
3 siblings, 0 replies; 36+ messages in thread
From: Lincoln Yeoh @ 2011-09-08 14:14 UTC (permalink / raw)
To: Merlin Moncure <mmoncure@gmail.com>; +Cc: jhondius@rem.nl; Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
At 03:51 AM 9/8/2011, Merlin Moncure wrote:
> > Don't you have to block SELECTs so that the SELECTs get serialized?
> > Otherwise concurrent SELECTs can occur at the same time, find no existing
> > rows, then "all" the inserts proceed and you get errors (or dupes).
> >
> > That's how Postgresql still works right? I haven't really been keeping up.
>
>yeah -- but you only need to block selects if you are selecting in the
>inserting transaction (this is not a full upsert). if both writers
>are doing:
>begin;
>lock table foo exclusive;
>insert into foo select ... where ...;
>commit;
>
>is good enough. btw even if you are doing upsert pattern
>(lock...select for update...insert/update), you'd be fine with
>straight exclusive locks because the 'for update' lock takes a higher
>lock that is blocked by exclusive. A basic rule of thumb is to try
>and not fully block readers unless absolutely necessary...basically
>maintenance operations.
Oh wait, now I think I get it. "lock table foo exclusive" will block
the inserts too, so I wouldn't get dupe errors even if other
transactions "blindly" insert dupes at the same time. The other
transactions might get the dupe errors, but mine won't as long as it
selects first and only inserts if there are no rows at that point.
Is that correct?
Link.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:51 ` Re: conditional insert Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Re: conditional insert Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Re: conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` Re: conditional insert J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Re: conditional insert Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
2011-09-07 19:51 ` Re: conditional insert Merlin Moncure <mmoncure@gmail.com>
@ 2011-09-08 14:19 ` Merlin Moncure <mmoncure@gmail.com>
3 siblings, 0 replies; 36+ messages in thread
From: Merlin Moncure @ 2011-09-08 14:19 UTC (permalink / raw)
To: Lincoln Yeoh <lyeoh@pop.jaring.my>; +Cc: jhondius@rem.nl; Pau Marc Muñoz Torres <paumarc@gmail.com>; pgsql-general
On Thu, Sep 8, 2011 at 9:14 AM, Lincoln Yeoh <lyeoh@pop.jaring.my> wrote:
> At 03:51 AM 9/8/2011, Merlin Moncure wrote:
>>
>> > Don't you have to block SELECTs so that the SELECTs get serialized?
>> > Otherwise concurrent SELECTs can occur at the same time, find no
>> > existing
>> > rows, then "all" the inserts proceed and you get errors (or dupes).
>> >
>> > That's how Postgresql still works right? I haven't really been keeping
>> > up.
>>
>> yeah -- but you only need to block selects if you are selecting in the
>> inserting transaction (this is not a full upsert). if both writers
>> are doing:
>> begin;
>> lock table foo exclusive;
>> insert into foo select ... where ...;
>> commit;
>>
>> is good enough. btw even if you are doing upsert pattern
>> (lock...select for update...insert/update), you'd be fine with
>> straight exclusive locks because the 'for update' lock takes a higher
>> lock that is blocked by exclusive. A basic rule of thumb is to try
>> and not fully block readers unless absolutely necessary...basically
>> maintenance operations.
>
> Oh wait, now I think I get it. "lock table foo exclusive" will block the
> inserts too, so I wouldn't get dupe errors even if other transactions
> "blindly" insert dupes at the same time. The other transactions might get
> the dupe errors, but mine won't as long as it selects first and only inserts
> if there are no rows at that point.
>
> Is that correct?
correct -- your transactions never get dup errors and external
transactions only get them if they, say, select without update before
the upsert (which is a bug any way you slice it).
fully blocking readers on a high traffic table is a good way to crash
your application. bring this issue up to any 'sql server admin' and
they'll start to develop a nervous tic...
merlin
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: conditional insert
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
@ 2011-09-05 10:28 ` Sim Zacks <sim@compulab.co.il>
3 siblings, 0 replies; 36+ messages in thread
From: Sim Zacks @ 2011-09-05 10:28 UTC (permalink / raw)
To: pgsql-general
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html style="direction: ltr;">
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<style>body p { margin-bottom: 0cm; margin-top: 0pt; } </style>
</head>
<body style="direction: ltr;"
bidimailui-detected-decoding-type="latin-charset" bgcolor="#ffffff"
text="#000000">
On 09/05/2011 12:38 PM, Pau Marc Muñoz Torres wrote:
<blockquote
cite="mid:CADFuJLjCF6fmxswgS033EFyofmbJFy+J0ToUvwTCDDLYj+nr3Q@mail.gmail.com"
type="cite">Hi follk<br>
<br>
i trying to performe a conditional insert into a table, indeed,
what i'm trying to do is not insert a record into the table if
that record exist<br>
<br>
googleling i found something like<br>
<br>
insert into XX values (1,2,3) where not exist (select ....);<br>
<br>
but i'm having and error near where...<br>
<br>
anyone knows how do i can perfome this insert?<br>
<br>
thanks<br>
<br>
p<br>
</blockquote>
<p>You can either do an Insert(...) select... from...where...</p>
<p>or you can add a rule to the table that checks if the key exists
and if so, do an update or nothing instead.<br>
</p>
</body>
</html>
^ permalink raw reply [nested|flat] 36+ messages in thread
* Conditional INSERT
@ 2019-03-15 17:55 basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:26 ` Re: Conditional INSERT Paul Jungwirth <pj@illuminatedcomputing.com>
2019-03-15 20:02 ` Re: Conditional INSERT Andreas Kretschmer <andreas@a-kretschmer.de>
0 siblings, 3 replies; 36+ messages in thread
From: basti @ 2019-03-15 17:55 UTC (permalink / raw)
To: pgsql-general
Hello,
I want to insert data into table only if condition is true.
For example:
INSERT into mytable (domainid, hostname, txtdata)
VALUES (100,'_acme.challenge.example', 'somedata');
The insert should only be done if Hostname like %_acme.challenge%.
How can it be done? I dont want that the user/script can insert any value.
Best regards.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
@ 2019-03-15 18:10 ` Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2 siblings, 1 reply; 36+ messages in thread
From: Michel Pelletier @ 2019-03-15 18:10 UTC (permalink / raw)
To: basti <mailinglist@unix-solution.de>; +Cc: pgsql-general
Well, the obvious question is, why are you inserting data into your
database you don't want? It makes more sense to just not do the insert.
But, assuming perhaps you have no control over the client, you can create a
BEFORE INSERT trigger that rejects the inserts that don't match your
condition:
https://www.postgresql.org/docs/current/plpgsql-trigger.html#PLPGSQL-DML-TRIGGER
On Fri, Mar 15, 2019 at 10:55 AM basti <mailinglist@unix-solution.de> wrote:
> Hello,
>
> I want to insert data into table only if condition is true.
> For example:
>
> INSERT into mytable (domainid, hostname, txtdata)
> VALUES (100,'_acme.challenge.example', 'somedata');
>
> The insert should only be done if Hostname like %_acme.challenge%.
>
> How can it be done? I dont want that the user/script can insert any value.
>
> Best regards.
>
>
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
@ 2019-03-15 18:17 ` Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
0 siblings, 1 reply; 36+ messages in thread
From: Michael Lewis @ 2019-03-15 18:17 UTC (permalink / raw)
To: Michel Pelletier <pelletier.michel@gmail.com>; +Cc: basti <mailinglist@unix-solution.de>; pgsql-general
>
> On Fri, Mar 15, 2019 at 10:55 AM basti <mailinglist@unix-solution.de>
> wrote:
>
>> Hello,
>>
>> I want to insert data into table only if condition is true.
>> For example:
>>
>> INSERT into mytable (domainid, hostname, txtdata)
>> VALUES (100,'_acme.challenge.example', 'somedata');
>>
>
Alternative to a trigger implementation, if you are generating that INSERT
statement, you can change it to use a sub-select or CTE that contains no
values if the domainid isn't what you like. If you want it to fail with
error, you could add a check constraint. We might need more context on what
you are doing and why to give good advice.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
@ 2019-03-15 18:54 ` basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 1 reply; 36+ messages in thread
From: basti @ 2019-03-15 18:54 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
this is a dns database, and the client is update the _acme-challenge for
LE certificates. I don't want that the client can insert "any" txt record.
the client should only insert data if the hostname start with
_acme-challenge. i have no control on client.
i have try this rule but the server reject this with a endless loop:
CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt
WHERE NEW.hostname like '_acme-challenge%'
DO INSERT INTO t_dnsadmin_records_txt VALUES (
NEW.domainid,
NEW.hostname,
NEW.txtdata
);
On 15.03.19 19:17, Michael Lewis wrote:
> On Fri, Mar 15, 2019 at 10:55 AM basti <mailinglist@unix-solution.de
> <mailto:mailinglist@unix-solution.de>> wrote:
>
> Hello,
>
> I want to insert data into table only if condition is true.
> For example:
>
> INSERT into mytable (domainid, hostname, txtdata)
> VALUES (100,'_acme.challenge.example', 'somedata');
>
>
> Alternative to a trigger implementation, if you are generating that
> INSERT statement, you can change it to use a sub-select or CTE that
> contains no values if the domainid isn't what you like. If you want it
> to fail with error, you could add a check constraint. We might need more
> context on what you are doing and why to give good advice.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
@ 2019-03-15 18:59 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-15 19:05 ` Re: Conditional INSERT Rob Sargent <robjsargent@gmail.com>
2019-03-15 23:23 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
0 siblings, 2 replies; 36+ messages in thread
From: Adrian Klaver @ 2019-03-15 18:59 UTC (permalink / raw)
To: basti <mailinglist@unix-solution.de>; pgsql-general@lists.postgresql.org
On 3/15/19 11:54 AM, basti wrote:
> this is a dns database, and the client is update the _acme-challenge for
> LE certificates. I don't want that the client can insert "any" txt record.
> the client should only insert data if the hostname start with
> _acme-challenge. i have no control on client.
>
> i have try this rule but the server reject this with a endless loop:
To borrow a quote:
"I had a problem so I decided to use a rule, now I have two problems."
Do not use a rule. As suggested upstream use a BEFORE INSERT trigger,
you will be a lot happier.
>
> CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt
> WHERE NEW.hostname like '_acme-challenge%'
> DO INSERT INTO t_dnsadmin_records_txt VALUES (
> NEW.domainid,
> NEW.hostname,
> NEW.txtdata
> );
>
>
> On 15.03.19 19:17, Michael Lewis wrote:
>> On Fri, Mar 15, 2019 at 10:55 AM basti <mailinglist@unix-solution.de
>> <mailto:mailinglist@unix-solution.de>> wrote:
>>
>> Hello,
>>
>> I want to insert data into table only if condition is true.
>> For example:
>>
>> INSERT into mytable (domainid, hostname, txtdata)
>> VALUES (100,'_acme.challenge.example', 'somedata');
>>
>>
>> Alternative to a trigger implementation, if you are generating that
>> INSERT statement, you can change it to use a sub-select or CTE that
>> contains no values if the domainid isn't what you like. If you want it
>> to fail with error, you could add a check constraint. We might need more
>> context on what you are doing and why to give good advice.
>
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
@ 2019-03-15 19:05 ` Rob Sargent <robjsargent@gmail.com>
2019-03-15 19:24 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
1 sibling, 1 reply; 36+ messages in thread
From: Rob Sargent @ 2019-03-15 19:05 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: basti <mailinglist@unix-solution.de>; pgsql-general@lists.postgresql.org
> On Mar 15, 2019, at 12:59 PM, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
>
> On 3/15/19 11:54 AM, basti wrote:
>> this is a dns database, and the client is update the _acme-challenge for
>> LE certificates. I don't want that the client can insert "any" txt record.
>> the client should only insert data if the hostname start with
>> _acme-challenge. i have no control on client.
>> i have try this rule but the server reject this with a endless loop:
>
> To borrow a quote:
>
> "I had a problem so I decided to use a rule, now I have two problems."
>
> Do not use a rule. As suggested upstream use a BEFORE INSERT trigger, you will be a lot happier.
>
>> CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt
>> WHERE NEW.hostname like '_acme-challenge%'
>> DO INSERT INTO t_dnsadmin_records_txt VALUES (
>> NEW.domainid,
>> NEW.hostname,
>> NEW.txtdata
>> );
>> On 15.03.19 19:17, Michael Lewis wrote:
>>> On Fri, Mar 15, 2019 at 10:55 AM basti <mailinglist@unix-solution.de
>>> <mailto:mailinglist@unix-solution.de>> wrote:
>>>
>>> Hello,
>>>
>>> I want to insert data into table only if condition is true.
>>> For example:
>>>
>>> INSERT into mytable (domainid, hostname, txtdata)
>>> VALUES (100,'_acme.challenge.example', 'somedata');
>>>
>>> Alternative to a trigger implementation, if you are generating that
>>> INSERT statement, you can change it to use a sub-select or CTE that
>>> contains no values if the domainid isn't what you like. If you want it
>>> to fail with error, you could add a check constraint. We might need more
>>> context on what you are doing and why to give good advice.
>
>
Does a check constraint not suffice in this situation?
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-15 19:05 ` Re: Conditional INSERT Rob Sargent <robjsargent@gmail.com>
@ 2019-03-15 19:24 ` Michel Pelletier <pelletier.michel@gmail.com>
0 siblings, 0 replies; 36+ messages in thread
From: Michel Pelletier @ 2019-03-15 19:24 UTC (permalink / raw)
To: Rob Sargent <robjsargent@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; basti <mailinglist@unix-solution.de>; pgsql-general <pgsql-general@lists.postgresql.org>
You're right it probably does, unless the constraint needs to do a
sub-query to get the matching pattern, which would require a trigger.
On Fri, Mar 15, 2019 at 12:05 PM Rob Sargent <robjsargent@gmail.com> wrote:
>
>
> On Mar 15, 2019, at 12:59 PM, Adrian Klaver <adrian.klaver@aklaver.com>
> wrote:
>
> On 3/15/19 11:54 AM, basti wrote:
>
> this is a dns database, and the client is update the _acme-challenge for
> LE certificates. I don't want that the client can insert "any" txt record.
> the client should only insert data if the hostname start with
> _acme-challenge. i have no control on client.
> i have try this rule but the server reject this with a endless loop:
>
>
> To borrow a quote:
>
> "I had a problem so I decided to use a rule, now I have two problems."
>
> Do not use a rule. As suggested upstream use a BEFORE INSERT trigger, you
> will be a lot happier.
>
> CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt
> WHERE NEW.hostname like '_acme-challenge%'
> DO INSERT INTO t_dnsadmin_records_txt VALUES (
> NEW.domainid,
> NEW.hostname,
> NEW.txtdata
> );
> On 15.03.19 19:17, Michael Lewis wrote:
>
> On Fri, Mar 15, 2019 at 10:55 AM basti <mailinglist@unix-solution.de
> <mailto:mailinglist@unix-solution.de <mailinglist@unix-solution.de>>>
> wrote:
>
> Hello,
>
> I want to insert data into table only if condition is true.
> For example:
>
> INSERT into mytable (domainid, hostname, txtdata)
> VALUES (100,'_acme.challenge.example', 'somedata');
>
> Alternative to a trigger implementation, if you are generating that
> INSERT statement, you can change it to use a sub-select or CTE that
> contains no values if the domainid isn't what you like. If you want it
> to fail with error, you could add a check constraint. We might need more
> context on what you are doing and why to give good advice.
>
>
>
>
> Does a check constraint not suffice in this situation?
>
>
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
@ 2019-03-15 23:23 ` Ken Tanzer <ken.tanzer@gmail.com>
2019-03-15 23:42 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
1 sibling, 1 reply; 36+ messages in thread
From: Ken Tanzer @ 2019-03-15 23:23 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: basti <mailinglist@unix-solution.de>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Fri, Mar 15, 2019 at 11:59 AM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:
> On 3/15/19 11:54 AM, basti wrote:
> > this is a dns database, and the client is update the _acme-challenge for
> > LE certificates. I don't want that the client can insert "any" txt
> record.
> > the client should only insert data if the hostname start with
> > _acme-challenge. i have no control on client.
> >
> > i have try this rule but the server reject this with a endless loop:
>
> To borrow a quote:
>
> "I had a problem so I decided to use a rule, now I have two problems."
>
> Do not use a rule. As suggested upstream use a BEFORE INSERT trigger,
> you will be a lot happier.
>
> >
> > CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt
> > WHERE NEW.hostname like '_acme-challenge%'
> > DO INSERT INTO t_dnsadmin_records_txt VALUES (
> > NEW.domainid,
> > NEW.hostname,
> > NEW.txtdata
> > );
> >
> >
>
Just curious, but wanted to follow up on whether rules are across-the-board
discouraged? I've seen disparaging comments about them, but I don't see
any indication of that on the create rule page.
The other suggestion in this thread--a foreign key--will throw an error.
Your suggestion of a before trigger might well be better (and if so, why?),
but is there anything particularly wrong or bad about using a rule that
would actually work? Something along these lines:
CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt
WHERE NOT NEW.hostname like '_acme-challenge%'
DO INSTEAD NOTHING;
Thanks,
Ken
--
AGENCY Software
A Free Software data system
By and for non-profits
*http://agency-software.org/ <http://agency-software.org/>*
*https://demo.agency-software.org/client
<https://demo.agency-software.org/client>*
ken.tanzer@agency-software.org
(253) 245-3801
Subscribe to the mailing list
<agency-general-request@lists.sourceforge.net?body=subscribe> to
learn more about AGENCY or
follow the discussion.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-15 23:23 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
@ 2019-03-15 23:42 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-16 00:19 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
0 siblings, 1 reply; 36+ messages in thread
From: Adrian Klaver @ 2019-03-15 23:42 UTC (permalink / raw)
To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: basti <mailinglist@unix-solution.de>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 3/15/19 4:23 PM, Ken Tanzer wrote:
> On Fri, Mar 15, 2019 at 11:59 AM Adrian Klaver
> <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
>
> On 3/15/19 11:54 AM, basti wrote:
> > this is a dns database, and the client is update the
> _acme-challenge for
> > LE certificates. I don't want that the client can insert "any"
> txt record.
> > the client should only insert data if the hostname start with
> > _acme-challenge. i have no control on client.
> >
> > i have try this rule but the server reject this with a endless loop:
>
> To borrow a quote:
>
> "I had a problem so I decided to use a rule, now I have two problems."
>
> Do not use a rule. As suggested upstream use a BEFORE INSERT trigger,
> you will be a lot happier.
>
> >
> > CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt
> > WHERE NEW.hostname like '_acme-challenge%'
> > DO INSERT INTO t_dnsadmin_records_txt VALUES (
> > NEW.domainid,
> > NEW.hostname,
> > NEW.txtdata
> > );
> >
> >
>
>
> Just curious, but wanted to follow up on whether rules are
> across-the-board discouraged? I've seen disparaging comments about
> them, but I don't see any indication of that on the create rule page.
See here:
https://www.postgresql-archive.org/Deprecating-RULES-td5727689.html
The rumor crops periodically that they will be deprecated. I personally
do not see that happening any time soon.
My issue with rules is this:
https://www.postgresql.org/docs/11/rules.html
If you can understand what really goes on in the above you are ahead of
me. Given that my experience is that they do the unexpected as often as
the expected so I stay away from them. Triggers I understand even when
they error, which is the important part.
>
> The other suggestion in this thread--a foreign key--will throw an
> error. Your suggestion of a before trigger might well be better (and if
> so, why?), but is there anything particularly wrong or bad about using a
> rule that would actually work? Something along these lines:
The trouble is simple rarely stays simple and following logic is a lot
easier in a trigger function then a rule. This is down mostly to the
query rewrite that goes on in a rule. That is border line, if not
outright, black magic. Try to follow what happens here:
https://www.postgresql.org/docs/11/rules-update.html
41.4.1.1. A First Rule Step by Step
>
> CREATE RULE insert_acme AS ON INSERT TO t_dnsadmin_records_txt
> WHERE NOT NEW.hostname like '_acme-challenge%'
> DO INSTEAD NOTHING;
>
> Thanks,
> Ken
>
>
>
> --
> AGENCY Software
> A Free Software data system
> By and for non-profits
> /http://agency-software.org//
> /https://demo.agency-software.org/client/
> ken.tanzer@agency-software.org <mailto:ken.tanzer@agency-software.org>
> (253) 245-3801
>
> Subscribe to the mailing list
> <mailto:agency-general-request@lists.sourceforge.net?body=subscribe> to
> learn more about AGENCY or
> follow the discussion.
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-15 23:23 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
2019-03-15 23:42 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
@ 2019-03-16 00:19 ` Ken Tanzer <ken.tanzer@gmail.com>
2019-03-16 00:41 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-16 01:43 ` Re: Conditional INSERT Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 2 replies; 36+ messages in thread
From: Ken Tanzer @ 2019-03-16 00:19 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: basti <mailinglist@unix-solution.de>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On Fri, Mar 15, 2019 at 4:42 PM Adrian Klaver <adrian.klaver@aklaver.com>
wrote:
> > Just curious, but wanted to follow up on whether rules are
> > across-the-board discouraged? I've seen disparaging comments about
> > them, but I don't see any indication of that on the create rule page.
>
> See here:
> https://www.postgresql-archive.org/Deprecating-RULES-td5727689.html
>
> The rumor crops periodically that they will be deprecated. I personally
> do not see that happening any time soon.
>
> My issue with rules is this:
>
> https://www.postgresql.org/docs/11/rules.html
>
> If you can understand what really goes on in the above you are ahead of
> me. Given that my experience is that they do the unexpected as often as
> the expected so I stay away from them. Triggers I understand even when
> they error, which is the important part.
>
> OK, and thanks for the info. I've gleaned that rules are not "deprecated"
in the sense that they are slated for removal, but they are rather
discouraged. Since that's the case, wouldn't it make sense to warn users
about this? That might keep them away from rules, and ease any eventual
deprecation/transition issues you might have by lowering the number of
"rules" in the wild.
In the section on "Rules vs. Triggers" (41.7), it doesn't even hint at
this, and even says:
*"For the things that can be implemented by both, which is best depends on
the usage of the database."*
There _is_ a nice CAUTION box in 41.4 ("Rules on Insert, Update and
Delete"). I would suggest that something like that should go into Rules
vs. Triggers, and then have a short caution box at the top of perhaps every
Rule page (or at the very least for "CREATE RULE") that says something like
"Rules are discouraged. You may be better off with Triggers instead. See
<rules_v_triggers>."
Just my two cents, but that might help more people be aware of the issue
and avoid rules altogether.
Cheers,
Ken
> >
> > --
> > AGENCY Software
> > A Free Software data system
> > By and for non-profits
> > /http://agency-software.org//
> > /https://demo.agency-software.org/client/
> > ken.tanzer@agency-software.org <mailto:ken.tanzer@agency-software.org>
> > (253) 245-3801
> >
> > Subscribe to the mailing list
> > <mailto:agency-general-request@lists.sourceforge.net?body=subscribe> to
> > learn more about AGENCY or
> > follow the discussion.
>
>
> --
> Adrian Klaver
> adrian.klaver@aklaver.com
>
--
AGENCY Software
A Free Software data system
By and for non-profits
*http://agency-software.org/ <http://agency-software.org/>*
*https://demo.agency-software.org/client
<https://demo.agency-software.org/client>*
ken.tanzer@agency-software.org
(253) 245-3801
Subscribe to the mailing list
<agency-general-request@lists.sourceforge.net?body=subscribe> to
learn more about AGENCY or
follow the discussion.
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-15 23:23 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
2019-03-15 23:42 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-16 00:19 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
@ 2019-03-16 00:41 ` Adrian Klaver <adrian.klaver@aklaver.com>
1 sibling, 0 replies; 36+ messages in thread
From: Adrian Klaver @ 2019-03-16 00:41 UTC (permalink / raw)
To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: basti <mailinglist@unix-solution.de>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
On 3/15/19 5:19 PM, Ken Tanzer wrote:
>
>
> On Fri, Mar 15, 2019 at 4:42 PM Adrian Klaver <adrian.klaver@aklaver.com
> <mailto:adrian.klaver@aklaver.com>> wrote:
> OK, and thanks for the info. I've gleaned that rules are not
> "deprecated" in the sense that they are slated for removal, but they are
> rather discouraged. Since that's the case, wouldn't it make sense to
> warn users about this? That might keep them away from rules, and ease
> any eventual deprecation/transition issues you might have by lowering
> the number of "rules" in the wild.
Well then there is this section:
https://www.postgresql.org/docs/11/rules-views.html
So as was mentioned in thread I posted, until someone comes up with a
replacement they are not going away.
>
> In the section on "Rules vs. Triggers" (41.7), it doesn't even hint at
> this, and even says:
>
> /"For the things that can be implemented by both, which is best depends
> on the usage of the database."
> /
>
> There _is_ a nice CAUTION box in 41.4 ("Rules on Insert, Update and
> Delete"). I would suggest that something like that should go into Rules
> vs. Triggers, and then have a short caution box at the top of perhaps
> every Rule page (or at the very least for "CREATE RULE") that says
> something like "Rules are discouraged. You may be better off with
> Triggers instead. See <rules_v_triggers>."
>
> Just my two cents, but that might help more people be aware of the issue
> and avoid rules altogether.
I thought I remember there being something like in a older version of
the docs. I can't find it now, so it might be just my mind playing
tricks on me.
>
> Cheers,
> Ken
>
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-15 23:23 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
2019-03-15 23:42 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-16 00:19 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
@ 2019-03-16 01:43 ` Tom Lane <tgl@sss.pgh.pa.us>
1 sibling, 0 replies; 36+ messages in thread
From: Tom Lane @ 2019-03-16 01:43 UTC (permalink / raw)
To: Ken Tanzer <ken.tanzer@gmail.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; basti <mailinglist@unix-solution.de>; pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Ken Tanzer <ken.tanzer@gmail.com> writes:
> OK, and thanks for the info. I've gleaned that rules are not "deprecated"
> in the sense that they are slated for removal, but they are rather
> discouraged. Since that's the case, wouldn't it make sense to warn users
> about this?
There's no plan to remove them, but we do encourage people to think of
triggers first. That's why the triggers chapter appears first, and why
the "rules vs. triggers" section doesn't really read as evenhanded
(to me anyway).
> In the section on "Rules vs. Triggers" (41.7), it doesn't even hint at
> this, and even says:
> *"For the things that can be implemented by both, which is best depends on
> the usage of the database."*
You're ignoring the sentence immediately before that, which is
Writing such triggers is often easier than writing rules, particularly
if complex logic is required to perform the update.
as well as the one at the end of its (short) paragraph:
However, the trigger approach is conceptually far simpler than the
rule approach, and is easier for novices to get right.
The only case where we're really encouraging people to use rules is
where the overhead of a trigger is unacceptable. Even then, this
whole section is written thinking of per-row triggers. The performance
tradeoffs would likely be quite different if using a per-statement trigger
with transition tables. But that's a very new feature, and I don't think
anyone's done serious performance comparisons of that vs. rules.
regards, tom lane
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
@ 2019-03-15 18:26 ` Paul Jungwirth <pj@illuminatedcomputing.com>
2 siblings, 0 replies; 36+ messages in thread
From: Paul Jungwirth @ 2019-03-15 18:26 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 3/15/19 10:55 AM, basti wrote:
> I want to insert data into table only if condition is true.
> For example:
>
> INSERT into mytable (domainid, hostname, txtdata)
> VALUES (100,'_acme.challenge.example', 'somedata');
>
> The insert should only be done if Hostname like %_acme.challenge%.
I would use `INSERT INTO ... SELECT` for this, instead of `INSERT INTO
... VALUES`. For example:
INSERT INTO mytable (domainid, hostname, txtdata)
SELECT 100, '_acme.challenge.example', 'somedata'
WHERE '_acme.challenge.example' LIKE '%_acme.challenge%'
;
(Presumably in the real code the hostname is parameterized so this isn't
quite as pointless as it looks. :-)
If you are inserting a lot of rows at once you could also SELECT from a
VALUES list:
INSERT INTO mytable (domainid, hostname, txtdata)
SELECT d, h, t
FROM (VALUES
(100, '_acme.challenge.example', 'somedata'),
(200, 'bar.example.com', 'somedata'),
(300, 'foo.example.com', 'somedata'),
(400, '_acme.challenge.example', 'somedata')
) x(d, h, t)
WHERE h LIKE '%_acme.challenge%'
;
I hope that helps!
Yours,
--
Paul ~{:-)
pj@illuminatedcomputing.com
^ permalink raw reply [nested|flat] 36+ messages in thread
* Re: Conditional INSERT
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
@ 2019-03-15 20:02 ` Andreas Kretschmer <andreas@a-kretschmer.de>
2 siblings, 0 replies; 36+ messages in thread
From: Andreas Kretschmer @ 2019-03-15 20:02 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Am 15.03.19 um 18:55 schrieb basti:
> Hello,
>
> I want to insert data into table only if condition is true.
> For example:
>
> INSERT into mytable (domainid, hostname, txtdata)
> VALUES (100,'_acme.challenge.example', 'somedata');
>
> The insert should only be done if Hostname like %_acme.challenge%.
>
>
you can use a check-constraint:
create table mytable(hostname text, check(hostname like
'%_acme.challenge%'));
I think you can see the idea...
Regards, Andreas
--
2ndQuadrant - The PostgreSQL Support Company.
www.2ndQuadrant.com
^ permalink raw reply [nested|flat] 36+ messages in thread
end of thread, other threads:[~2019-03-16 01:43 UTC | newest]
Thread overview: 36+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-09-05 09:38 conditional insert Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 09:45 ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
2011-09-05 09:48 ` Raymond O'Donnell <rod@iol.ie>
2011-09-05 09:51 ` Thomas Kellerer <spam_eater@gmx.net>
2011-09-05 10:37 ` Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 10:42 ` Sim Zacks <sim@compulab.co.il>
2011-09-05 10:47 ` Pau Marc Muñoz Torres <paumarc@gmail.com>
2011-09-05 11:02 ` J. Hondius <jhondius@rem.nl>
2011-09-06 18:50 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-06 20:45 ` Merlin Moncure <mmoncure@gmail.com>
2011-09-06 21:23 ` Merlin Moncure <mmoncure@gmail.com>
2011-09-07 15:45 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-07 15:50 ` Andrew Sullivan <ajs@crankycanuck.ca>
2011-09-07 19:51 ` Merlin Moncure <mmoncure@gmail.com>
2011-09-07 20:04 ` Andrew Sullivan <ajs@crankycanuck.ca>
2011-09-07 20:50 ` Merlin Moncure <mmoncure@gmail.com>
2011-09-08 14:31 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-08 15:12 ` Andrew Sullivan <ajs@crankycanuck.ca>
2011-09-08 14:07 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-08 14:14 ` Lincoln Yeoh <lyeoh@pop.jaring.my>
2011-09-08 14:19 ` Merlin Moncure <mmoncure@gmail.com>
2011-09-05 10:28 ` Sim Zacks <sim@compulab.co.il>
2019-03-15 17:55 Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:10 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 18:17 ` Re: Conditional INSERT Michael Lewis <mlewis@entrata.com>
2019-03-15 18:54 ` Re: Conditional INSERT basti <mailinglist@unix-solution.de>
2019-03-15 18:59 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-15 19:05 ` Re: Conditional INSERT Rob Sargent <robjsargent@gmail.com>
2019-03-15 19:24 ` Re: Conditional INSERT Michel Pelletier <pelletier.michel@gmail.com>
2019-03-15 23:23 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
2019-03-15 23:42 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-16 00:19 ` Re: Conditional INSERT Ken Tanzer <ken.tanzer@gmail.com>
2019-03-16 00:41 ` Re: Conditional INSERT Adrian Klaver <adrian.klaver@aklaver.com>
2019-03-16 01:43 ` Re: Conditional INSERT Tom Lane <tgl@sss.pgh.pa.us>
2019-03-15 18:26 ` Re: Conditional INSERT Paul Jungwirth <pj@illuminatedcomputing.com>
2019-03-15 20:02 ` Re: Conditional INSERT Andreas Kretschmer <andreas@a-kretschmer.de>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox