Thread: Conditional INSERT: if not exists
I want to insert a row unless it exists already. Do I have to write a stored procedure to do this? Thanks, Don
Don Morrison wrote: > I want to insert a row unless it exists already. Do I have to write a > stored procedure to do this? Hi, Don. Just do the insert. If it already exists, it simply isn't inserted. You will, of course, receive an error that the row exists, but that isn't a problem. If you need to do this in a transaction, you can simply set savepoints and roll back to the savepoint if an insert fails (and you expected that it could). Sean
One way would be to create a Primary Index containing all columns in the table...? For example - a table called "grps" with 3 colums: CREATE TABLE grps ( code varchar(4) NOT NULL, description text NOT NULL, category varchar(3), CONSTRAINT groups_pkey PRIMARY KEY (code,description,category) ) An insert on this table will fail if the values for each column in the row you are attempting to insert already matches and existing row - Example: code | description | category ------+----------------------------------------+---------- 0001 | GROUP NOT DEFINED | OT1 0009 | NEWLY CREATED PRODUCTS | OT1 0010 | GROUP NOT SET | OT1 Then attempting: INSERT INTO grps VALUES ('0001', 'GROUP NOT DEFINED', 'OT1'); This will fail as it already matches the first row in the table. This WILL work (with a different value in the third column): INSERT INTO grps VALUES ('0001', 'GROUP NOT DEFINED', 'OT2'); Cheers, -p -----Original Message----- From: pgsql-novice-owner@postgresql.org [mailto:pgsql-novice-owner@postgresql.org] On Behalf Of Don Morrison Sent: Tuesday, 22 August 2006 10:43 To: pgsql-novice@postgresql.org Subject: [NOVICE] Conditional INSERT: if not exists I want to insert a row unless it exists already. Do I have to write a stored procedure to do this? Thanks, Don ---------------------------(end of broadcast)--------------------------- TIP 4: Have you searched our list archives? http://archives.postgresql.org *******************Confidentiality and Privilege Notice******************* The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email. Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments
> > I want to insert a row unless it exists already. Do I have to write a > > stored procedure to do this? >[...] > isn't a problem. If you need to do this in a transaction, you can > simply set savepoints and roll back to the savepoint if an insert fails > (and you expected that it could). Yes, that's the problem: I have it in one transaction which I don't want to fail on error. I'll look into the savepoint method. Specifically the transaction I'm writing is a "Z SQL Method" in Zope (a page templating system and web server).
> > > I want to insert a row unless it exists already. Do I have to write a > > > stored procedure to do this? > >[...] > > isn't a problem. If you need to do this in a transaction, you can > > simply set savepoints and roll back to the savepoint if an insert fails > > (and you expected that it could). > Yes, that's the problem: I have it in one transaction which I don't > want to fail on error. I'll look into the savepoint method. > Specifically the transaction I'm writing is a "Z SQL Method" in Zope > (a page templating system and web server). I'll have to upgrade to PostgreSQL 8.X. :( 7.X doesn't have savepoint.
Hi, why not simply put a where condition in you insert : insert into table values (a,b) where not exists (select a,b from table) Don Morrison a écrit : > I want to insert a row unless it exists already. Do I have to write a > stored procedure to do this? > > Thanks, > Don > > ---------------------------(end of broadcast)--------------------------- > TIP 4: Have you searched our list archives? > > http://archives.postgresql.org >
> why not simply put a where condition in you insert : > > insert into table values (a,b) > where not exists (select a,b from table) The WHERE clause does not fit into the INSERT syntax in that way: http://www.postgresql.org/docs/8.1/interactive/sql-insert.html INSERT INTO table [ ( column [, ...] ) ] { DEFAULT VALUES | VALUES ( { expression | DEFAULT } [, ...] ) | query } My problem: if the insert fails because the value already exists, then this starts a rollback of my entire transaction. The solution I'm trying is to create a nested transaction with a savepoint right before the insert, thus catching the rollback with the nested transaction...I'm not sure the nested transaction is necessary...maybe just the savepoint. Example: ...other outer transaction work here... BEGIN; SAVEPOINT insert_may_fail; INSERT INTO table ...; COMMIT; ...continue next outer transaction work here...
On Wed, Aug 23, 2006 at 12:48:53 -0700, Don Morrison <donmorrison@gmail.com> wrote: > > My problem: if the insert fails because the value already exists, then > this starts a rollback of my entire transaction. The solution I'm If you know there won't be concurrent inserts or deletes affecting the row of interest there is a way to do this in the INSERT statement. > transaction...I'm not sure the nested transaction is necessary...maybe > just the savepoint. Example: Using save points creates nested transactions, so you have that overhead by whatever name it is called.
> If you know there won't be concurrent inserts or deletes affecting the > row of interest there is a way to do this in the INSERT statement. I cannot rule out concurrent inserts. > > transaction...I'm not sure the nested transaction is necessary...maybe > > just the savepoint. Example: > Using save points creates nested transactions, so you have that overhead by > whatever name it is called. Overhead yes, that was not the question. The question was is the extra nesting necessary?
On 8/23/06 5:05 PM, "Don Morrison" <donmorrison@gmail.com> wrote: >> If you know there won't be concurrent inserts or deletes affecting the >> row of interest there is a way to do this in the INSERT statement. > > I cannot rule out concurrent inserts. > >>> transaction...I'm not sure the nested transaction is necessary...maybe >>> just the savepoint. Example: >> Using save points creates nested transactions, so you have that overhead by >> whatever name it is called. > > Overhead yes, that was not the question. The question was is the > extra nesting necessary? The answer is yes, I think, if you need your inserts to be done within a transaction (as opposed to serially). Sean
> > Overhead yes, that was not the question. The question was is the > > extra nesting necessary? > > The answer is yes, I think, if you need your inserts to be done within a > transaction (as opposed to serially). Thanks for the input everybody. -Don
> isn't a problem. If you need to do this in a transaction, you can > simply set savepoints and roll back to the savepoint if an insert fails > (and you expected that it could). Unfortunately, for some reason it does a rollback of the entire outer transaction too, not just the nested one. :( Anybody know why? If I use stored procedures will it be easier?
> > isn't a problem. If you need to do this in a transaction, you can > > simply set savepoints and roll back to the savepoint if an insert fails > > (and you expected that it could). > > Unfortunately, for some reason it does a rollback of the entire outer > transaction too, not just the nested one. :( Anybody know why? > > If I use stored procedures will it be easier? > When I try to insert a duplicate row, this generates an IntegrityError on my unique index, which rolls back my entire transaction, when I really want it to generate a ROLLBACK TO SAVEPOINT for the most recent savepoint, so only that insert is ignored. Does anyone know a configuration trick to get it to do this, or do I have to write a stored procedure where I explicitly call ROLLBACK TO SAVEPOINT?
Don Morrison wrote: > My problem: if the insert fails because the value already exists, then > this starts a rollback of my entire transaction. The solution I'm > trying is to create a nested transaction with a savepoint right before > the insert, thus catching the rollback with the nested > transaction...I'm not sure the nested transaction is necessary...maybe > just the savepoint. Example: You need to: SAVEPOINT insert_may_fail try: INSERT INTO ... RELEASE SAVEPOINT except: ROLLBACK TO SAVEPOINT UPDATE ... You can catch the exception either in Python, or if you are using a ZSQL method, using <dtml-try>. -- Stuart Bishop <stuart@stuartbishop.net> http://www.stuartbishop.net/