Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 6CFC61337BAD for ; Mon, 22 Nov 2010 23:44:43 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 80756-03-4 for ; Tue, 23 Nov 2010 03:44:38 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail.gmx.net (mailout-de.gmx.net [213.165.64.23]) by mail.postgresql.org (Postfix) with SMTP id 7E7321337BC5 for ; Mon, 22 Nov 2010 23:44:34 -0400 (AST) Received: (qmail invoked by alias); 23 Nov 2010 03:44:34 -0000 Received: from mue-88-130-57-230.dsl.tropolys.de (EHLO [192.168.0.113]) [88.130.57.230] by mail.gmx.net (mp039) with SMTP; 23 Nov 2010 04:44:34 +0100 X-Authenticated: #14269776 X-Provags-ID: V01U2FsdGVkX18ErksraSoEbiUUPo4AD2INzsESTU98JUaKxURSC5 0qaquFBhZ4zjvY Message-ID: <4CEB38B0.8050901@gmx.net> Date: Tue, 23 Nov 2010 04:44:48 +0100 From: Andreas User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.12) Gecko/20101027 Thunderbird/3.1.6 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Howto "insert or update" ? Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Y-GMX-Trusted: 0 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_NONE=-0.0001, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201011/46 X-Sequence-Number: 34784 Hi, is there an elegant way to tell PG : a) Hey PG, look here are e.g. 3 values A, B, c for tableX b) please check if there is a row matching A and B as key in tableX c) if such a row exists, execute an UPDATE on column c else INSERT a new row. Currently I have a trigger function that should store a value in tableX whenever a certain column in tableY gets changed. I do it with: a) delete from tableX where key = ( A, B ) ( regardless if there is one ) b) insert into tableX This seems not very efficient though it works. Is there a better way?