Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 991BEB5DBC5 for ; Mon, 12 Sep 2011 18:26:37 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 24350-03 for ; Mon, 12 Sep 2011 21:26:31 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id E027DB5DBD1 for ; Mon, 12 Sep 2011 18:26:30 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1R3E1N-0000aD-JX for pgsql-performance@postgresql.org; Mon, 12 Sep 2011 23:26:29 +0200 Received: from p5dc4ef5c.dip.t-dialin.net ([93.196.239.92]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 12 Sep 2011 23:26:29 +0200 Received: from shortcutter by p5dc4ef5c.dip.t-dialin.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 12 Sep 2011 23:26:29 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-performance@postgresql.org From: Robert Klemme Subject: Re: Postgres for a "data warehouse", 5-10 TB Date: Mon, 12 Sep 2011 23:26:10 +0200 Lines: 72 Message-ID: References: <4E6CC458.4010206@squeakycode.net> <4E6E3FE8.5070609@squeakycode.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: p5dc4ef5c.dip.t-dialin.net User-Agent: Mozilla/5.0 (Windows NT 6.0; WOW64; rv:6.0.2) Gecko/20110902 Thunderbird/6.0.2 In-Reply-To: <4E6E3FE8.5070609@squeakycode.net> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.393 tagged_above=-10 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RP_MATCHES_RCVD=-0.504, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201109/165 X-Sequence-Number: 44941 On 12.09.2011 19:22, Andy Colson wrote: > On 9/12/2011 12:15 PM, Robert Klemme wrote: >> On 11.09.2011 19:02, Marti Raudsepp wrote: >>> On Sun, Sep 11, 2011 at 17:23, Andy Colson wrote: >>>> On 09/11/2011 08:59 AM, Igor Chudov wrote: >>>>> By the way, does that INSERT UPDATE functionality or something like >>>>> this exist in Postgres? >>>> You have two options: >>>> 1) write a function like: >>>> create function doinsert(_id integer, _value text) returns void as >>>> 2) use two sql statements: >>> >>> Unfortunately both of these options have caveats. Depending on your >>> I/O speed, you might need to use multiple loader threads to saturate >>> the write bandwidth. >>> >>> However, neither option is safe from race conditions. If you need to >>> load data from multiple threads at the same time, they won't see each >>> other's inserts (until commit) and thus cause unique violations. If >>> you could somehow partition their operation by some key, so threads >>> are guaranteed not to conflict each other, then that would be perfect. >>> The 2nd option given by Andy is probably faster. >>> >>> You *could* code a race-condition-safe function, but that would be a >>> no-go on a data warehouse, since each call needs a separate >>> subtransaction which involves allocating a transaction ID. >> >> Wouldn't it be sufficient to reverse order for race condition safety? >> Pseudo code: >> >> begin >> insert ... >> catch >> update ... >> if not found error >> end >> >> Speed is another matter though... > No, I dont think so, if you had two loaders, both would start a > transaction, then neither could see what the other was doing. It depends. But the point is that not both INSERTS can succeed. The one which fails will attempt the UPDATE and - depending on isolation level and DB implementation - will be blocked or fail. In the case of PG this particular example will work: 1. TX inserts new PK row 2. TX tries to insert same PK row => blocks 1. TX commits 2. TX fails with PK violation 2. TX does the update (if the error is caught) > There are > transaction isolation levels, but they are like playing with fire. (in > my opinion). You make them sound like witchcraft. But they are clearly defined - even standardized. Granted, different RDBMS might implement them in different ways - here's PG's view of TX isolation: http://www.postgresql.org/docs/8.4/interactive/transaction-iso.html In my opinion anybody working with RDBMS should make himself familiar with this concept - at least know about it - because it is one of the fundamental features of RDBMS and certainly needs consideration in applications with highly concurrent DB activity. Kind regards robert