Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 673E764FC27 for ; Tue, 30 Dec 2008 06:55:10 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 76041-03 for ; Tue, 30 Dec 2008 06:55:07 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by mail.postgresql.org (Postfix) with ESMTP id 3BD5C64FC4B for ; Tue, 30 Dec 2008 06:55:06 -0400 (AST) Received: from root by ciao.gmane.org with local (Exim 4.43) id 1LHcFa-00067E-UR for pgsql-performance@postgresql.org; Tue, 30 Dec 2008 10:55:03 +0000 Received: from 89.248.111.230 ([89.248.111.230]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 30 Dec 2008 10:55:02 +0000 Received: from poison.box by 89.248.111.230 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 30 Dec 2008 10:55:02 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-performance@postgresql.org From: Anton Bogdanovitch Subject: perform 1 check vs exception when unique_violation Date: Tue, 30 Dec 2008 13:41:42 +0300 Lines: 30 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1251; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 89.248.111.230 User-Agent: Thunderbird 2.0.0.18 (Windows/20081105) X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.599 tagged_above=0 required=5 tests=RCVD_IN_DNSWL_LOW=-1, RCVD_NUMERIC_HELO=2.599 X-Spam-Level: * X-Archive-Number: 200812/226 X-Sequence-Number: 32262 I have to insert rows to table with 95% primary key unique_violation. I've tested 2 examples below: 1) BEGIN INSERT INTO main (name, created) VALUES (i_name, CURRENT_TIMESTAMP AT TIME ZONE 'GMT'); EXCEPTION WHEN UNIQUE_VIOLATION THEN RETURN 'error: already exists'; END; RETURN 'ok: store'; 2) PERFORM 1 FROM main WHERE name = i_name; IF NOT FOUND THEN INSERT INTO main (name, created) VALUES (i_name, CURRENT_TIMESTAMP AT TIME ZONE 'GMT'); RETURN 'ok: stored'; ELSE RETURN 'error: already exists'; END IF; The first one performs about 20% slower, have 5 times more disk i/o write operations. The second one uses 20% more cpu. Is it because of raid1 and slow writes? What is the better solution to fit best performance? Pg version 8.3, table size will probably grow to 100M rows