X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 940A732C295 for ; Fri, 15 Oct 2004 14:15:23 +0100 (BST) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 11675-06 for ; Fri, 15 Oct 2004 13:15:14 +0000 (GMT) Received: from fusion.bennyvision.com (fusion.bennyvision.com [216.161.119.28]) by svr1.postgresql.org (Postfix) with SMTP id 2080732C11D for ; Fri, 15 Oct 2004 14:15:15 +0100 (BST) Received: (qmail 3609 invoked from network); 15 Oct 2004 13:15:13 -0000 Received: from localhost (HELO webmail.stinkweasel.net) (127.0.0.1) by fusion.bennyvision.com with SMTP; 15 Oct 2004 13:15:13 -0000 Received: from 63.227.74.41 (SquirrelMail authenticated user benny); by webmail.stinkweasel.net with HTTP; Fri, 15 Oct 2004 08:14:51 -0500 (CDT) Message-ID: <64883.63.227.74.41.1097846113.squirrel@63.227.74.41> In-Reply-To: <87hdow4ld1.fsf@stark.xeocode.com> References: <56362.63.227.74.41.1097813923.squirrel@63.227.74.41> <87hdow4ld1.fsf@stark.xeocode.com> Date: Fri, 15 Oct 2004 08:14:51 -0500 (CDT) Subject: Re: Inserting into table only if the row does not already From: "C. Bensend" To: pgsql-sql@postgresql.org User-Agent: SquirrelMail/1.4.3a X-Mailer: SquirrelMail/1.4.3a MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Priority: 3 (Normal) Importance: Normal X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200410/99 X-Sequence-Number: 19202 > You just have to put it in the select list as a constant. If you're > feeling > generous to the next programmer to read it you could put "AS column1" > after > each one, but the column name doesn't actually have to match the column > you're > inserting into. Sweet GOD, I hope no one ever has to read the stuff I'm working on! This is just a personal curiousity project, not anything for work or anything released publicly. I'd be far too embarrassed to ever release this, as I'm just fumbling along, learning. :) > Note that this is going to have some concurrency issues. I think it will > be > possible for a second query to execute before the first commits. In that > case > it won't see the record the first query inserted and try to insert again. > You'll just get a primary key violation though which I guess you can just > ignore. Concurrency shouldn't be an issue - this is a perl script running from cron. The only concurrency that will ever happen is if I'm a bonehead and I run the script manually right as cron kicks off another copy. > Which raises a question. Why not forgoe this complicated SQL and try to do > the > insert. If you get a primary key violation, well there's your answer... If > you > don't care about the failure just ignore it and move on. I would suggest > checking specifically for a primary key violation and still stopping > execution > on unexpected errors though. Well, I was hoping to minimize the amount of perl needed to get this one task done, but I think that's going to be the best way to do it. > Incidentally, if you're putting your parameters directly into your queries > using $column1 then you've got a potential security problem. Unless you're > quoting every variable everywhere religiously using postgres's quoting > functions an attacker can sneak extra SQL into your queries. Potentially > including whole new statements such as "DELETE FROM table"... Yes indeed, good catch. I'll clean that up immediately. Thanks, Greg! Benny -- "Even if a man chops off your hand with a sword, you still have two nice, sharp bones to stick in his eyes." -- .sig on Slashdot