Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id A99A5B5DBE8 for ; Sun, 14 Aug 2011 04:35:21 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 03217-10 for ; Sun, 14 Aug 2011 07:35:14 +0000 (UTC) X-Greylist: delayed 00:28:55.301702 by SQLgrey-1.7.6 Received: from zorro.isa-geek.com (unknown [91.187.225.16]) by mail.postgresql.org (Postfix) with ESMTP id 811D0B5DBE5 for ; Sun, 14 Aug 2011 04:35:14 -0300 (ADT) Received: from porta.local ([192.168.1.65] ident=rafal) by zorro.isa-geek.com with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.72) (envelope-from ) id 1QsUlq-0006X2-L5 for pgsql-general@postgresql.org; Sun, 14 Aug 2011 09:06:15 +0200 From: Rafal Pietrak To: pgsql-general@postgresql.org In-Reply-To: References: <4E46B33B.7090801@bestmx.ru> Content-Type: text/plain; charset="UTF-8" Date: Sun, 14 Aug 2011 09:06:04 +0200 Message-ID: <1313305564.11685.24.camel@localhost.localdomain> Mime-Version: 1.0 X-Mailer: Evolution 2.30.3 Content-Transfer-Encoding: 7bit X-SA-Exim-Connect-IP: 192.168.1.65 X-SA-Exim-Mail-From: rafal@zorro.isa-geek.com Subject: INSERT-colision/MERGE in postgresql X-SA-Exim-Version: 4.2.1 (built Mon, 22 Mar 2010 06:51:10 +0000) X-SA-Exim-Scanned: Yes (on zorro.isa-geek.com) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.104 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RDNS_NONE=0.793, TO_NO_BRKTS_NORDNS=0.003 X-Spam-Level: X-Archive-Number: 201108/413 X-Sequence-Number: 178695 Hi, I've recently looked into the problem of my INSERTs throwing an ROW error, when a new row hits an already present one, by unique constraint. It triggers an expensive rollback, and I'd like to have it sort of "optimised". In my case, duplicates can be discarded on an attempt INSERT, but an UPDATE instead would also do. When I was looking for a solution, I found this: http://wiki.postgresql.org/wiki/SQL_MERGE Which would do nicely, but I understand postgresql does not have it, yet. On the other hand, I think that providing the OLD.* table for RULES and TRIGGERS on INSERT, for an application level programmer (like myself), could provide a simple way to overcome the missing feature, until it's fully implemented as MERGE statement according to SQL:2003. Such OLD.* table on INSERTS should contain a row from current table content, that matches unique constraints of a currently inserted row or nothing, if there is no collision. This way I could make a conditional RULE with "... WHERE exists(OLD.someting)..." instead of doing an explicit SELECT in that WHERE clausure, which I think is more expensive then referring a column already fetched by the engine. Possibly, this may pave the way to MERGE implementation?? As of today, no application level code can possibly expect a valid OLD.* table within ROLE/TRIGGER on INSERT - so no current code will be broken by this. Would it be possible to add this to a whishlist for 9.2 or something? Regards, -R