Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 3A5C11337B64 for ; Wed, 27 Oct 2010 12:42:19 -0300 (ADT) 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 49892-01 for ; Wed, 27 Oct 2010 15:42:11 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from outbound.afilias.info (outbound.afilias.info [69.46.124.26]) by mail.postgresql.org (Postfix) with ESMTP id 7E3E11337B5C for ; Wed, 27 Oct 2010 12:42:11 -0300 (ADT) Received: from ms5.yyz2.afilias-ops.info ([10.50.129.111] helo=smtp.afilias.info) by outbound.afilias.info with esmtp (Exim 4.69) (envelope-from ) id 1PB88f-0007td-6B; Wed, 27 Oct 2010 15:42:09 +0000 Received: from tor-gateway.afilias.info ([199.15.87.4] helo=[10.10.46.23]) by smtp.afilias.info with esmtpsa (TLSv1:AES256-SHA:256) (Exim 4.69) (envelope-from ) id 1PB88f-0005Vf-5l; Wed, 27 Oct 2010 15:42:09 +0000 Message-ID: <4CC84851.3070100@ca.afilias.info> Date: Wed, 27 Oct 2010 11:42:09 -0400 From: Steve Singer User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.12) Gecko/20100915 Thunderbird/3.0.8 MIME-Version: 1.0 To: Divakar Singh CC: jd@commandprompt.com, pgsql-performance@postgresql.org Subject: Re: Postgres insert performance and storage requirement compared to Oracle References: <132275.30531.qm@web65411.mail.ac4.yahoo.com> In-Reply-To: <132275.30531.qm@web65411.mail.ac4.yahoo.com> 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=-4.21 tagged_above=-10 required=5 tests=BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201010/474 X-Sequence-Number: 40892 On 10-10-27 10:00 AM, Divakar Singh wrote: > Hi Steve and other friends, > Some information you would be interested in: > I did some further tests using libpq in my code. > I used a stored proc to insert 100 thousand rows in a table, it took 25 > sec (almost same as time taken by Oracle PL/SQL and OCI interface). > Same inserts through libpq take 70 seconds. > I am inserting all records in a single transaction. > So, the problem seems to be optimization of usage of libpq in my code. > I am attaching my code below. > Is any optimization possible in this? > Do prepared statements help in cutting down the insert time to half for > this kind of inserts? One of the major problems with libpq usage is lack > of good documentation and examples. > I could not get any good example of prepared stmt usage anywhere. Yes using prepared statements should make this go faster, but your best bet might be to use the COPY command. I don't have a PQprepare example handy though we probably should add one to the docs. The copy command would be used similar to PQexec(conn,"COPY TO aaaa (a,b,c,d,e,f,g,h,j,k,l,m,n,p) FROM STDIN WITH (DELIMITER ',') "); for(; i < howmany;i++) { sprintf(query,"67,'ABRAKADABRA', 'ABRAKADABRA', 'ABRAKADABRA', '1-Dec 2010', 'ABRAKADABRA', 'ABRAKADABRA', 'ABRAKADABRA', '1-Dec-2010', 99999, 99999, %d, 9999, 'ABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRA'\n", i); res = PQputCopyData(conn,query,strlen(query); } PQputCopyEnd(conn,NULL); I have not actually tried the above code snippet, it is just to give you the general idea. You call PQexec with the COPY command outside the loop then at each loop iteration you call PQputCopyData with some of the data that gets passed to the server. You can combine multiple lines on a single PQputCopyData call if you want. http://www.postgresql.org/docs/9.0/interactive/sql-copy.html http://www.postgresql.org/docs/9.0/interactive/libpq-copy.html > > > char query[1024]={0}; > > for (; i { > > sprintf (query, "INSERT INTO aaaa(a, b, c, d, e, f, g, h, j, k, l, m, n, > p) VALUES (67, 'ABRAKADABRA', 'ABRAKADABRA', 'ABRAKADABRA', > '1-Dec-2010', 'ABRAKADABRA', 'ABRAKADABRA', 'ABRAKADABRA', '1-Dec-2010', > 99999, 99999, %d, 9999, > 'ABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRAABRAKADABRA')", > i); > > res = PQexec(conn, query); > > if (PQresultStatus(res) != PGRES_COMMAND_OK) > { > cout<<"error at iteration "< PQclear(res); > break; > } > //PQclear(res); > > > } > > /* close the portal ... we don't bother to check for errors ... */ > /*res = PQexec(conn, "CLOSE myportal"); > PQclear(res);*/