Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwe7N-0005PL-7q for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2019 03:02:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hwe7L-0001YD-WD for pgsql-general@arkaria.postgresql.org; Sun, 11 Aug 2019 03:02:00 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwe7L-0001OZ-HQ for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2019 03:01:59 +0000 Received: from mail-ot1-x343.google.com ([2607:f8b0:4864:20::343]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hwe7G-0000aH-Ir for pgsql-general@lists.postgresql.org; Sun, 11 Aug 2019 03:01:57 +0000 Received: by mail-ot1-x343.google.com with SMTP id k18so15235083otr.3 for ; Sat, 10 Aug 2019 20:01:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=JFbXw39mDKRz57QDdHqZj72ID8NIDD1DXPgQ/PNnCDM=; b=MMJHCflDkTUOV/Woo8yojrCn9RZdyglRjCZJ6gCJLV2Ti14PNJqZRGMbV8Dw/mFlNu 0tNkIiEnRQx/9/tDNs615GGClaz8XM0MJ3zEOPPVuKxhgcEBIpdiAFmvf4mIxXB8+nmn 2J3TiAY4iFP7lo9+vaJoXTifno+yiU+iuLW77ZCUf5fYh8rknK1he81ISMPY23TYZ0Iq pvqCTUD5MXZrWVPMkl6cnG0VdY/bwztppzMq4QdefhmI3fNkyZaXKqV07x2LQgE7u5RA rT2mJJy2I96fD//TuHi4kJ4ARYAFFbneT6diMAMsRDNQGkjnKTRv8M4Ea8YOgIKlsrmW VhgQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=JFbXw39mDKRz57QDdHqZj72ID8NIDD1DXPgQ/PNnCDM=; b=WXGxAcgWNUhJR75rtEwSl533+IrgURVEWVi6E4GR6OnlPoVotY5CUC7rks7fN05K25 vi2hv4nrrmf2MmQiN458rZ4b2DqGPOvRDmRRlhEqJBmtzIi+qO+ayJJrazeDuMZJml/g b5Jug1txd2xbS/+/V+Dm7dUmrTV90l+DrASlAd+kXkrPkGKJ6S7LMH2fpzP99PKtJizr +g42+cTGXN9+91CAJyMomPXDVuDCuD+sYWH7XqJK7Cd3BAowFB/tTUa021RZdWzKz7jg xfokprnncNhRiQ1tSgmruxvarDOYepagkrHG6P2ojEP/NT9ysqGpgMD5tVF3LupUnrrA vOuw== X-Gm-Message-State: APjAAAWl0EgKzzoL6YAKMQUTudifkyyvl9tmv2OlDNlIcFMt7EGDeXNv WxMbHzHDaN+r3SN/mCOQx0I= X-Google-Smtp-Source: APXvYqyptRHmg/BSKx5oFb8Xrn9d7+snQbOE6rV2PQyx9hiN2YB/uC8cld8oV02Gvi0iJ0JC6DHF/g== X-Received: by 2002:a6b:1ca:: with SMTP id 193mr29279110iob.264.1565492512044; Sat, 10 Aug 2019 20:01:52 -0700 (PDT) Received: from [192.168.1.108] (c-73-63-59-118.hsd1.ut.comcast.net. [73.63.59.118]) by smtp.gmail.com with ESMTPSA id h19sm68138323iol.65.2019.08.10.20.01.51 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 10 Aug 2019 20:01:51 -0700 (PDT) From: Rob Sargent Message-Id: <9EB0475E-FB96-4F66-8FF8-7576F52383FE@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_025567C3-25DF-4CA7-BBC3-E106977022EC" Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.11\)) Subject: Re: Bulk Inserts Date: Sat, 10 Aug 2019 21:01:50 -0600 In-Reply-To: Cc: Adrian Klaver , "pgsql-generallists.postgresql.org" To: Souvik Bhattacherjee References: X-Mailer: Apple Mail (2.3445.104.11) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail=_025567C3-25DF-4CA7-BBC3-E106977022EC Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > On Aug 10, 2019, at 8:47 PM, Souvik Bhattacherjee = wrote: >=20 > Hi Adrian, >=20 > Thanks for the response. >=20 > > Yes, but you will some code via client or function that batches the=20= > > inserts for you. >=20 > Could you please elaborate a bit on how EXP 1 could be performed such = that it uses bulk inserts? >=20 > Best, > -SB >=20 > On Fri, Aug 9, 2019 at 7:26 PM Adrian Klaver = > wrote: > On 8/9/19 3:06 PM, Souvik Bhattacherjee wrote: > > Hi, > >=20 > > I'm trying to measure the performance of the following: Multiple = txns=20 > > inserting tuples into a table concurrently vs single txn doing the = whole=20 > > insertion. > >=20 > > *new table created as:* > > create table tab2 ( > > id serial, > > attr1 integer not null, > > attr2 integer not null, > > primary key(id) > > ); > >=20 > > *EXP 1: inserts with multiple txn:* > > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where=20= > > attr2 =3D 10); > > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 where=20= > > attr2 =3D 20); > >=20 > > note: attr2 has only two values 10 and 20 > >=20 > > *EXP 2: inserts with a single txn:* > > insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1); > >=20 > > I also performed another experiment as follows: > > *EXP 3:* select attr1, attr2 into tab2 from tab1; > >=20 > > The observation here is EXP 3 is much faster than EXP 2 probably = due to=20 > > bulk inserts used by Postgres. However I could not find a way to = insert=20 > > id values in tab2 using EXP 3. Also select .. into .. from .. throws = an=20 > > error if we create a table first and then populate the tuples using = the=20 > > command. >=20 > Yes as SELECT INTO is functionally the same as CREATE TABLE AS: >=20 > https://www.postgresql.org/docs/11/sql-selectinto.html = >=20 > >=20 > > I have the following questions: > > 1. Is it possible to have an id column in tab2 and perform a bulk = insert=20 > > using select .. into .. from .. or using some other means? >=20 > 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. >=20 > > 2. If a table is already created, is it possible to do bulk inserts = via=20 > > multiple txns inserting into the same table (EXP 3)? >=20 > Yes, but you will some code via client or function that batches the=20 > inserts for you. >=20 > >=20 > > Best, > > -SB >=20 >=20 > --=20 > Adrian Klaver > 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 = ) --Apple-Mail=_025567C3-25DF-4CA7-BBC3-E106977022EC Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii

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> 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 =3D 10);
> insert into tab2 (attr1, attr2) (select attr1, attr2 from tab1 = where
> attr2 =3D 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

>
> 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
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)

= --Apple-Mail=_025567C3-25DF-4CA7-BBC3-E106977022EC--