Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id DD32D1337B6D for ; Tue, 30 Nov 2010 20:00:48 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 26631-04 for ; Wed, 1 Dec 2010 00:00:39 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out.mail.amis.hr (out1.mail.amis.hr [188.129.0.4]) by mail.postgresql.org (Postfix) with ESMTP id B42E01336971 for ; Tue, 30 Nov 2010 20:00:24 -0400 (AST) Received: from out.fsec.sm.srv.amis.hr (out.fsec.sm.srv.amis.hr [192.168.106.22]) by out.mail.amis.hr (Postfix) with ESMTP id A538920427; Wed, 1 Dec 2010 01:00:24 +0100 (CET) Received: from pps.filterd (out [127.0.0.1]) by out.fsec.sm.srv.amis.hr (8.14.3/8.14.3) with SMTP id oB100Fud029501; Wed, 1 Dec 2010 01:00:24 +0100 Received: from smtp.amis.hr (smtp.sm.srv.amis.hr [192.168.106.39]) by out.fsec.sm.srv.amis.hr with ESMTP id sw86f833f-1; Wed, 01 Dec 2010 01:00:24 +0100 A-Original-Client-UID: 188.129.71.39 splivalo Received: from [192.168.10.1] (cpe-188-129-71-39.dynamic.amis.hr [188.129.71.39]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: splivalo) by smtp.amis.hr (Postfix) with ESMTPSA id F3A37273; Wed, 1 Dec 2010 01:00:23 +0100 (CET) Message-ID: <4CF59017.1090705@megafon.hr> Date: Wed, 01 Dec 2010 01:00:23 +0100 From: Mario Splivalo User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.15) Gecko/20101027 Lightning/1.0b1 Thunderbird/3.0.10 MIME-Version: 1.0 To: Mladen Gogala CC: "pgsql-performance@postgresql.org" Subject: Re: SELECT INTO large FKyed table is slow References: <4CF24103.8030209@megafon.hr> <4CF5259C.8000306@vmsinfo.com> In-Reply-To: <4CF5259C.8000306@vmsinfo.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.2.15, 1.0.148, 0.0.0000 definitions=2010-11-30_13:2010-12-01, 2010-11-30, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=6.0.2-1010190000 definitions=main-1011300169 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-10 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201011/422 X-Sequence-Number: 41446 On 11/30/2010 05:26 PM, Mladen Gogala wrote: > At the beginning of the load, you should defer all of the deferrable > constraints, setting constraints deferred and issuing the copy statement > within a transaction block, like this: > > scott=# begin; BEGIN > Time: 0.203 ms > scott=# set constraints all deferred; > SET CONSTRAINTS > Time: 0.201 ms > scott=# copy test1 from '/tmp/test1.csv'; > COPY 100 > Time: 11.939 ms > scott=# commit; > ERROR: insert or update on table "test1" violates foreign key > constraint "fk_tst1_deptno" > DETAIL: Key (col1)=(1) is not present in table "dept". > > > Of course, that will require complete rewrite of your load script, > because the errors will be checked at the commit time and transaction > can either fail as a whole or succeed as a whole. It's all or nothing Well, it is like that now. First I load the data from the CSV into the temporary table (just named temporary, exists on the server). That table is usualy aroun 10k rows. Then I call the function which does the job. > situation. How frequently do you see records with an incorrect drone_id? Seldom. > If that happens only once in a blue moon, you may need no stinkin' > foreign keys in the first place, you may be able > to have a batch job that will flag all the records with an invalid > drone_id instead. I did have that idea, yes, but still, I'd like to know what is slowing postgres down. Because when I look at the disk I/O, it seems very random - i get around 800k of disk reads and ocasionaly 1500k of writes (during insert into history table). > Furthermore, you can make sure that you have enough shared buffers to > cache the entire "drones" table. Also, do "strace" on the postgres > process handling your session and see whether the time is spent writing > to WAL archives. If that is slowing you down, you should consider buying > a SSD or a high end disk drive. I have never had such problem, but you > should also check whether pg_loader can do anything for you. > > As far as speed is concerned, inserting with deferred foreign keys is > almost as fast as inserting without foreign keys: > > scott=# alter table test1 drop constraint fk_tst1_deptno; > ALTER TABLE > Time: 16.219 ms > scott=# copy test1 from '/tmp/test1.csv'; > COPY 100 > Time: 10.418 ms > > If you take a look at the example above, you will see that inserting > with a deferred FK took 11.939 milliseconds while inserting into the > same table without the FK took 10.418 milliseconds, the difference of > 1.5 milliseconds per 100 rows. The timing of 2 seconds per 100 > rows looks suspiciously high. Me thinks that your problem is not just > the foreign key, there must be something else devouring the time. You > should have a test instance, compiled with "-g" option and do profiling. I'll have to. So far I've been doing this only on that dedicated server. I'll try to download the database to my desktop and try the tests there. Concerning the shared_buffers, it's 256M, and the drones table is just 15M. I have tried your recommendation and it yielded no difference. Now I tried removing the constraints from the history table (including the PK) and the inserts were fast. After few 'rounds' of inserts I added constraints back, and several round after that were fast again. But then all the same. Insert of some 11k rows took 4 seconds (with all constraints) and now the last one of only 4k rows took one minute. I did vacuum after each insert. Mario