Received: from localhost (unknown [200.46.204.183]) by postgresql.org (Postfix) with ESMTP id 753652E0229 for ; Mon, 10 Mar 2008 08:29:19 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 13648-10 for ; Mon, 10 Mar 2008 08:29:16 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.5 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.61.230.242]) by postgresql.org (Postfix) with ESMTP id BCF0E2E01EA for ; Mon, 10 Mar 2008 08:29:15 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id B0276C268 for ; Mon, 10 Mar 2008 20:29:09 +0900 (WST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 9879E1E0435 for ; Mon, 10 Mar 2008 20:29:09 +0900 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id geuaj+Xlbi8V for ; Mon, 10 Mar 2008 20:29:07 +0900 (WST) Received: from [203.161.97.213] (203.161.97.213.static.amnet.net.au [203.161.97.213]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTP id F188D1E0434 for ; Mon, 10 Mar 2008 20:29:06 +0900 (WST) Message-ID: <47D51B7D.4010800@postnewspapers.com.au> Date: Mon, 10 Mar 2008 20:29:01 +0900 From: Craig Ringer User-Agent: Thunderbird 2.0.0.12 (Windows/20080213) MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: Re: Very slow (2 tuples/second) sequential scan after bulk insert; speed returns to ~500 tuples/second after commit References: <47D505AF.4040101@postnewspapers.com.au> <47D5150C.8090802@enterprisedb.com> In-Reply-To: <47D5150C.8090802@enterprisedb.com> X-Enigmail-Version: 0.95.6 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 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200803/194 X-Sequence-Number: 29148 Thanks for the extremely helpful response. I don't think I would've spotted that one in a hurry. > You must be having an exception handler block in that pl/pgsql > function, which implicitly creates a new subtransaction on each > invocation of the exception handler block, so you end up with hundreds > of thousands of committed subtransactions. Aah - yes, there is. I didn't realize it'd have such an impact. I can work around the need for it by explicitly checking the table constraints in the function - in which case an uncaught exception will terminate the transaction, but should only arise when I've missed a constraint check. > For 8.4, it would be nice to improve that. I tested that on my laptop > with a similarly-sized table, inserting each row in a pl/pgsql > function with an exception handler, and I got very similar run times. > According to oprofile, all the time is spent in > TransactionIdIsInProgress. I think it would be pretty straightforward > to store the committed subtransaction ids in a sorted array, instead > of a linked list, and binary search. Or to use a hash table. That > should eliminate this problem, though there is still other places as > well where a large number of subtransactions will hurt performance. That does sound interesting - and it would be nice to be able to use exception handlers this way without too huge a performance hit. In the end though it's something that can be designed around once you're aware of it - and I'm sure that other ways of storing that data have their own different costs and downsides. What might also be nice, and simpler, would be a `notice', `log', or even `debug1' level warning telling the user they've reached an absurd number of subtransactions that'll cripple PostgreSQL's performance - say 100,000. There's precedent for this in the checkpoint frequency warning 8.3 produces if checkpoints are becoming too frequent - and like that warning it could be configurable for big sites. If you think that's sane I might have a go at it - though I mostly work in C++ so the result probably won't be too pretty initially. -- Craig Ringer