pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Rob Sargent <robjsargent@gmail.com>
To: Souvik Bhattacherjee <kivuosb@gmail.com>
Cc: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Bulk Inserts
Date: Sat, 10 Aug 2019 21:01:50 -0600
Message-ID: <9EB0475E-FB96-4F66-8FF8-7576F52383FE@gmail.com> (raw)
In-Reply-To: <CAANrPSf0bHm0BwezBcLjP9DfTdqd2Tdh8WdcKQ7n3=aop32fvA@mail.gmail.com>
References: <CAANrPSeVe5UNdLTm5STHdjPad89VbAZ_0UGcR_Cvo8_OqF91fA@mail.gmail.com>
	<bc6d54b9-a5b0-d974-1247-d9aa2272450f@aklaver.com>
	<CAANrPSf0bHm0BwezBcLjP9DfTdqd2Tdh8WdcKQ7n3=aop32fvA@mail.gmail.com>



> On Aug 10, 2019, at 8:47 PM, Souvik Bhattacherjee <kivuosb@gmail.com> wrote:
> 
> Hi Adrian,
> 
> Thanks for the response.
> 
> > Yes, but you will some code via client or function that batches the 
> > inserts for you.
> 
> Could you please elaborate a bit on how EXP 1 could be performed such that it uses bulk inserts?
> 
> Best,
> -SB
> 
> On Fri, Aug 9, 2019 at 7:26 PM Adrian Klaver <adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>> wrote:
> On 8/9/19 3:06 PM, Souvik Bhattacherjee wrote:
> > Hi,
> > 
> > I'm trying to measure the performance of the following: Multiple txns 
> > inserting tuples into a table concurrently vs single txn doing the whole 
> > insertion.
> > 
> > *new table created as:*
> > create table tab2 (
> > id serial,
> > attr1 integer not null,
> > attr2 integer not null,
> > primary key(id)
> > );
> > 
> > *EXP 1: inserts with multiple txn:*
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
> > attr2 = 10);
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where 
> > attr2 = 20);
> > 
> > note: attr2 has only two values 10 and 20
> > 
> > *EXP 2: inserts with a single txn:*
> > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1);
> > 
> > I also performed another experiment as follows:
> > *EXP 3:* select attr1, attr2 into tab2 from tab1;
> > 
> > The observation here is EXP 3  is much faster than EXP 2 probably due to 
> > bulk inserts used by Postgres. However I could not find a way to insert 
> > id values in tab2 using EXP 3. Also select .. into .. from .. throws an 
> > error if we create a table first and then populate the tuples using the 
> > command.
> 
> Yes as SELECT INTO is functionally the same as CREATE TABLE AS:
> 
> https://www.postgresql.org/docs/11/sql-selectinto.html <https://www.postgresql.org/docs/11/sql-selectinto.html;
> 
> > 
> > I have the following questions:
> > 1. Is it possible to have an id column in tab2 and perform a bulk insert 
> > using select .. into .. from .. or using some other means?
> 
> Not using SELECT INTO for reasons given above.
> Though it is possible to SELECT INTO as you show in EXP 3 and then:
>         alter table tab2 add column id serial primary key;
> EXP 2 shows the other means.
> 
> > 2. If a table is already created, is it possible to do bulk inserts via 
> > multiple txns inserting into the same table (EXP 3)?
> 
> Yes, but you will some code via client or function that batches the 
> inserts for you.
> 
> > 
> > Best,
> > -SB
> 
> 
> -- 
> Adrian Klaver
> adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com>
Top-posting (i.e. putting your reply at the top is discouraged here)
Does this appeal to you:
COPY (SELECT * FROM relation) TO ... (https://www.postgresql.org/docs/10/sql-copy.html <https://www.postgresql.org/docs/10/sql-copy.html;)

view thread (9+ messages)  latest in thread

Message-ID: <9EB0475E-FB96-4F66-8FF8-7576F52383FE@gmail.com>
Permalink:  ../9EB0475E-FB96-4F66-8FF8-7576F52383FE@gmail.com/
Also on:    postgresql.org/message-id/9EB0475E-FB96-4F66-8FF8-7576F52383FE@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: robjsargent@gmail.com, kivuosb@gmail.com, adrian.klaver@aklaver.com
  Subject: Re: Bulk Inserts
  In-Reply-To: <9EB0475E-FB96-4F66-8FF8-7576F52383FE@gmail.com>
  References: <CAANrPSeVe5UNdLTm5STHdjPad89VbAZ_0UGcR_Cvo8_OqF91fA@mail.gmail.com> <bc6d54b9-a5b0-d974-1247-d9aa2272450f@aklaver.com> <CAANrPSf0bHm0BwezBcLjP9DfTdqd2Tdh8WdcKQ7n3=aop32fvA@mail.gmail.com> <9EB0475E-FB96-4F66-8FF8-7576F52383FE@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox