Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 9BE50B5DBC8 for ; Sun, 11 Sep 2011 11:23:27 -0300 (ADT) 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 28933-01 for ; Sun, 11 Sep 2011 14:23:21 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from marajade.localdomain (97-64-237-59.client.mchsi.com [97.64.237.59]) by mail.postgresql.org (Postfix) with ESMTP id 80B8BB5DBC5 for ; Sun, 11 Sep 2011 11:23:21 -0300 (ADT) Received: from [192.168.1.116] (173-31-201-111.client.mchsi.com [173.31.201.111]) (Authenticated sender: andy) by marajade.localdomain (Postfix) with ESMTPA id 9D1674690D; Sun, 11 Sep 2011 09:23:21 -0500 (CDT) Message-ID: <4E6CC458.4010206@squeakycode.net> Date: Sun, 11 Sep 2011 09:23:20 -0500 From: Andy Colson User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.21) Gecko/20110903 Lightning/1.0b2 Thunderbird/3.1.13 MIME-Version: 1.0 To: Igor Chudov CC: pgsql-performance@postgresql.org Subject: Re: Postgres for a "data warehouse", 5-10 TB References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-0.918 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RDNS_DYNAMIC=0.982 X-Spam-Level: X-Archive-Number: 201109/114 X-Sequence-Number: 44890 On 09/11/2011 08:59 AM, Igor Chudov wrote: > > > I do not plan to do a lot of random writing. My current design is that my perl scripts write to a temporary table every week, and then I do INSERT..ON DUPLICATE KEY UPDATE. > > By the way, does that INSERT UPDATE functionality or something like this exist in Postgres? > > i You have two options: 1) write a function like: create function doinsert(_id integer, _value text) returns void as $$ begin update thetable set value = _value where id = _id; if not found then insert into thetable(id, value) values (_id, _value); end if end; $$ language plpgsql; 2) use two sql statements: -- update the existing update realTable set value = (select value from tmp where tmp.id = realTable.id) where exists (select value from tmp where tmp.id = realTable.id); -- insert the missing insert into realTable(id, value) select id, value from tmp where not exists(select 1 from realTable where tmp.id = realTable.id); -Andy