Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eKSon-0004iz-OC for pgsql-general@arkaria.postgresql.org; Thu, 30 Nov 2017 17:40:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eKSon-0001Jc-Av for pgsql-general@arkaria.postgresql.org; Thu, 30 Nov 2017 17:40:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eKSon-0001JK-3L for pgsql-general@lists.postgresql.org; Thu, 30 Nov 2017 17:40:13 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eKSoi-0005UJ-43 for pgsql-general@lists.postgresql.org; Thu, 30 Nov 2017 17:40:11 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 4C2E21555; Thu, 30 Nov 2017 18:40:05 +0100 (CET) Date: Thu, 30 Nov 2017 18:40:05 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: large numbers of inserts out of memory strategy Message-ID: <20171130174005.jc3t5km2f2n4qmk7@hjp.at> References: <87d5d3c9-66e4-c29d-bece-79f2909cccb9@2ndquadrant.com> <8431416a-2ab2-d580-19bd-2c7e3353960d@2ndquadrant.com> <26623.1511927991@sss.pgh.pa.us> <20171130102209.6q6djwimzzj5opge@hjp.at> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="xoiqnxkjfoi6l33n" Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --xoiqnxkjfoi6l33n Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2017-11-30 08:43:32 -0600, Ted Toth wrote: > Date: Thu, 30 Nov 2017 08:43:32 -0600 > From: Ted Toth > To: "Peter J. Holzer" > Cc: pgsql-general@lists.postgresql.org > Subject: Re: large numbers of inserts out of memory strategy >=20 > On Thu, Nov 30, 2017 at 4:22 AM, Peter J. Holzer wrote: > > On 2017-11-29 08:32:02 -0600, Ted Toth wrote: > >> Yes I did generate 1 large DO block: > >> > >> DO $$ > >> DECLARE thingid bigint; thingrec bigint; thingdataid bigint; > >> BEGIN > >> INSERT INTO thing > >> (ltn,classification,machine,source,thgrec,flags,serial,type) VALUES > >> ('T007336','THING',0,1025,7336,7,'XXX869977564',1) RETURNING id,thgrec > >> INTO thingid,thingrec; > >> INSERT INTO recnum_thing (recnum,thing_id) VALUES (thingrec,thingid); > >> INSERT INTO thingstatus > >> (thing_id,nrpts,rmks_cs,force_type_id,ftn_cs,force_code,arr_cs,mask,to= i_state,plot_id,signa_cs,lastchange,des_cs,rig_cs,ownship,correlation,maxrp= ts,rtn_cs,ctc_cs,group_mask,dep_cs) > >> VALUES (thingid,121,'{0,0,0,0}',440,0,23,0,0,0,'{23,-1,3803,3805,-1,-1= ,0,6}',0,1509459164,0,0,0,0,1000,0,0,0,0); [...] > >> > >> > >> > >> END $$; > >> > >> Should I limit the number of 'thing' inserts within a DO block or > >> wrapping each 'thing' insert in it's own DO block? > > >=20 > Thanks for the specific suggestions. >=20 > > I would suggest getting rid of the do block entirely if that is > > possible. Just create lots of insert statements. You can get the current > > value of a sequence with currval('sequence_name'). >=20 > What is the downside of using a DO block? As you discovered, the downside is that the whole do block needs to be parsed before it can be executed. If you generate a huge do block, that can be an issue. If you create lots of little do blocks, the overhead is probably not noticable. But I'd like to pose the question the other way around: What are the advantages of using a do block? If there aren't any, I wouldn't use it (keep it simple). One advantage is that you can use variables. But I don't think you need that. Other possible advantages could be speed or transactions: I don't know if these exist. > I'd have to do a nextval on each sequence before I could use currval, > right? That happens automatically if you insert the default value into a serial column. You seem to do that here: > >> INSERT INTO thing (ltn,classification,machine,source,thgrec,flags,seri= al,type) > >> VALUES ('T007336','THING',0,1025,7336,7,'XXX869977564',1) > >> RETURNING id,thgrec INTO thingid,thingrec; The column id is probably defined as serial (or explicitely associated with a sequence). So you can get the value you just inserted with currval('thing_id_seq') (Check the table definition to get the sequence name, but unless you explicitely specified the sequence name that's it).=20 So you could ditch the "RETURNING ..." clause and replace the next statement with: INSERT INTO recnum_thing (recnum, thing_id) VALUES (7336, currval('thin= g_id_seq')); And similar for the other statements where you use thingid and thingdataid (thingrec is already known as far as i can see). Note that this doesn't work if you need two values from the same sequence. So you can't replace=20 insert into thing(..) values(...) returning id as parent_id; insert into thing(..) values(...) returning id as child_id; insert into structure(parent, child) values(parent_id, child_id); in the same way. But you don't seem to need anything like that. > One thing that is unclear to me is when commits occur while using psql > would you know where in the docs I can find information on this > subject? By default psql enables autocommit which causes an implicit commit after every statement. With a do block I'm not sure whether that means after the do block or after each statement within the do block. I'd just turn autocommit off and add explicit commits wherever I wanted them. > > Alternately or in addition, since you are using python, you might want > > to insert directly into the database from python using psycopg2. For > > separate insert statements that should have about the same performance. > > (It is usually much faster to write to a csv file and load that with > > copy than to insert each row, but you don't do that and it might be > > difficult in your case). >=20 > Yes, I thought about generating csv files but didn't see a way to deal > with the foreign keys. Still: Is there a reason why you use a python script to create an sql script instead of directly issuing the sql queries from your python script? hp --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --xoiqnxkjfoi6l33n Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAlogQm4ACgkQ8g5IURL+ KF0XJw//V2FT2vuUcfxz/ahN6ruP7JZInm17X5FGnZ5UJ8IIygjSWz2l0lDEFIX3 V3VYVplr9hI5VViD8BJdxKjPTSbMXhfZ+1nT9IGuqGJAAP/t5Xp3palR2laxKHGF GE7lfLEaGTuzexPlEeCqkqvAZ7cifGtZ7i5+oNMfnw/iiFSsl1BlYUBmIpCTEhbE CymTUj8dEMlPb+tG0QNWHsDI1v+hrK1fxz+uBCLud/UDKSky36mkVJ3eN1C/5Wp4 Q2QP7c0WJLjcFC2fhyGMK2AELEqMTAoOuzGl66hXCN8OxsRMg+M7bStwdKZg7rAn Q60bJ4WL/qXb5N7VJVHoRI+tYmHgKZH5nUjSQkJkEkOoMpQO5Qz7AFK8V2mKiALY JKXVqKXb1e/M4PIfVUmTaprH84LnVk1d+bRT6DUEHfNP9sPK4GPVFFVMBxPI9grx xuirvJOZbSLcgZqW6C+T5ZnydYzlXA4eBJ1ckrQqb9pE5/M8BPJ/EiXhrWQe4QcP BIrxxgiy6G8nO0mKd2N0GyzRhPH9pqWWiXjRNtF9lRaKYLgZ3Vra6fe6N2VA6W1N TjWMCu6JOmh96DNkhdbTgbqFl8Jr7Ql/QrEQW0tMufCGh3VfUbaSqviZoOWeLSBe Ls3HRyHtL+JBIZtcf5bikCLMvy/20AJJHs9UUd4wDlk6GmrztMc= =iw+i -----END PGP SIGNATURE----- --xoiqnxkjfoi6l33n--