Received: from localhost (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 01F4564FD7D for ; Tue, 30 Dec 2008 10:51:05 -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 17518-04 for ; Tue, 30 Dec 2008 10:51:02 -0400 (AST) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-bw0-f19.google.com (mail-bw0-f19.google.com [209.85.218.19]) by mail.postgresql.org (Postfix) with ESMTP id D23F064FC57 for ; Tue, 30 Dec 2008 10:51:01 -0400 (AST) Received: by bwz12 with SMTP id 12so14168768bwz.19 for ; Tue, 30 Dec 2008 06:51:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:to :subject:cc:in-reply-to:mime-version:content-type :content-transfer-encoding:content-disposition:references; bh=kBDjY88DEU2Sq58GRKHjntxy8eV9Zue85cee19S6Nks=; b=xvtk8tQsRB7flHoyd8P0qsH/Ag8fKjEAE8EXRpHYEyGkFmnVX/WrF5gMXF84YM7AKZ mp9UduIUmKqr8UfJq3EeCl4Ja+r0kR328D67ucR9VQdRVmBn01bPDWF63SqNsL6Iu8bx 0sKMs9utM/b7a0beLAyVZBo9hv2lp+6sRXRLU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:to:subject:cc:in-reply-to:mime-version :content-type:content-transfer-encoding:content-disposition :references; b=xbLOmqQJ+dI7x7CeB/eDMtG40mS7XaJxK3PzilTCSUstsEGIdnvDorZStMscplZBCS GoVj9lUwRdlLQIlHDXYE17a69TFfccHpAhp1AO+u9PkfWrnm5nJD1BtLjydIrHv9dE0y 4FFFQMCBD46u7rwDjqE2Zf1/cWAywZ34Ble+M= Received: by 10.223.116.77 with SMTP id l13mr10735660faq.106.1230648656147; Tue, 30 Dec 2008 06:50:56 -0800 (PST) Received: by 10.223.107.135 with HTTP; Tue, 30 Dec 2008 06:50:55 -0800 (PST) Message-ID: <603c8f070812300650i226f032co8db1f25eb352e4b8@mail.gmail.com> Date: Tue, 30 Dec 2008 09:50:55 -0500 From: "Robert Haas" To: "Anton Bogdanovitch" Subject: Re: perform 1 check vs exception when unique_violation Cc: pgsql-performance@postgresql.org In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline References: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200812/228 X-Sequence-Number: 32264 On Tue, Dec 30, 2008 at 5:41 AM, Anton Bogdanovitch wrote: > I have to insert rows to table with 95% primary key unique_violation. If you're inserting a lot of rows at once, I think you're probably better off loading all of the data into a side table that does not have a primary key, and then writing one statement to remove the duplicates and do all the inserts at once. INSERT INTO main (name, created) SELECT s.name, CURRENT_TIMESTAMP FROM (SELECT DISTINCT ON (name) FROM sidetable) s -- nuke duplicate names within sidetable LEFT JOIN main m ON s.name = m.name WHERE m.name IS NULL; -- nuke names in sidetable that are already in main I've usually found that any kind of loop in PL/pgsql is mind-numbingly slow compared to doing the same thing as a single query. ...Robert