Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1ss2yr-005iUy-Dt for pgsql-general@arkaria.postgresql.org; Sat, 21 Sep 2024 16:29:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sry6H-00FhNY-Mz for pgsql-general@arkaria.postgresql.org; Sat, 21 Sep 2024 11:16:31 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sry6H-00FhLl-9D for pgsql-general@lists.postgresql.org; Sat, 21 Sep 2024 11:16:30 +0000 Received: from mail-yw1-x112c.google.com ([2607:f8b0:4864:20::112c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sry6F-000KzA-Dd for pgsql-general@lists.postgresql.org; Sat, 21 Sep 2024 11:16:30 +0000 Received: by mail-yw1-x112c.google.com with SMTP id 00721157ae682-6dfff346a83so9776277b3.2 for ; Sat, 21 Sep 2024 04:16:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1726917261; x=1727522061; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Cxn3XDDtN2ZtJYVK5bJH0Kf6Bm230XsT0dtj2rrLMCQ=; b=iOQrhrUrYWtBYzKJ0u0fSFSgL22s058sgtwl7iEhYi8RAL8Be2uvL/Jdtt/TK9H3Uy n02KGrt7ILoAuTt5jye8bmEFE1J2HVv3dXxP6dFg8y/DPtD+R4ALClcOiuLYSozFfuwd XD/JDfugMK2s3Koihpr1e5QCQ7jZcOMeZp888ZKpvXGjzIdS3WgzEmAUcP3R516xr3IZ hZlb2LJ9NTEYRUdjJJgf3tMj3/7/gSMNpHeENVyJAKhzfS0T4TFJc4ALtv+8Ja7JxcNN mXrlpaNWsDjOUZfUOMgETSOB0H/In0smK4rY/+2BJv/7RbDkcMzvAsjvlwSnCYQrF4+9 YnAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726917261; x=1727522061; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Cxn3XDDtN2ZtJYVK5bJH0Kf6Bm230XsT0dtj2rrLMCQ=; b=gPIfFlx/CYlIqyhsi/gttXgQyKTrBb2mjwLAEkJn3UI9VOFYuSU5DfaYxDgcVnEHKv loHHSs+jLNgin3hp52ZFSJfJACr/Lb+eiPkPl/OLnx9Wft/VwvHRSo1BSAJ5WcrtgT2a jlfyC9Cb+082Eq8qpQG8qtU9R297FRZIOL7kltUiuCXOqKfRdeaRHu1Ypg0cENKQYwkM avURO1oO2Iujx+CG4isvdxocwGnxC/cLTnqGTXQZkzBD6Z7G6Ix5qWHYL6cGZ84YviK+ cmDCQVh/e+MVrCiyrF9Jh1ob3SktDulaC0YHWV6wCfzwA3g0ptQn3O5PB0f4FObt7X4V j9wg== X-Gm-Message-State: AOJu0YzFFysYEq6dxaP67N4yMzrRndYQYS7XmF84cORc5InUEmFh713i flGyVAHPM2ceg63Y6aR9sQXjuD1m/PAsHHyyR/9CSkhZjjaS9TaR4VGSLY/TtjKCsK3vLWDAGb8 ys3fh3JvLPGRV2usA4w0BaSUYVv4= X-Google-Smtp-Source: AGHT+IH+4DjSGmLZZ3DMefvYHSmjMxJoOgjPOxaGzIW8HTXOQD1nQEwRr+kHInX1pHJF16tLZ265IWJ7EaDwV+lTJ/w= X-Received: by 2002:a05:690c:6403:b0:6db:3b2f:a1eb with SMTP id 00721157ae682-6dfeed64662mr57431547b3.26.1726917260673; Sat, 21 Sep 2024 04:14:20 -0700 (PDT) MIME-Version: 1.0 References: <4178E73A-24F5-4E3C-92F6-1532D8102C3E@kleczek.org> In-Reply-To: <4178E73A-24F5-4E3C-92F6-1532D8102C3E@kleczek.org> From: Lok P Date: Sat, 21 Sep 2024 16:44:08 +0530 Message-ID: Subject: Re: How batch processing works To: =?UTF-8?B?TWljaGHFgiBLxYJlY3plaw==?= Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000e10ea606229f41d4" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000e10ea606229f41d4 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sat, Sep 21, 2024 at 9:51=E2=80=AFAM Micha=C5=82 K=C5=82eczek wrote: > Hi, > > > On 19 Sep 2024, at 07:30, Lok P wrote: > > > [snip] > > > > Method-4 > > > > INSERT INTO parent_table VALUES (1, 'a'), (2, 'a'); > > INSERT INTO child_table VALUES (1,1, 'a'), (1,2, 'a'); > > commit; > > I=E2=80=99ve done some batch processing of JSON messages from Kafka in Ja= va. > By far the most performant way was to: > > 1. Use prepared statements > 2. Parse JSON messages in Postgres > 3. Process messages in batches > > All three can be achieved by using arrays to pass batches: > > WITH parsed AS ( > SELECT msg::json FROM unnest(?) > ), > parents AS ( > INSERT INTO parent SELECT =E2=80=A6 FROM parsed RETURNING ... > ) > INSERT INTO child SELECT =E2=80=A6 FROM parsed=E2=80=A6 > > Not the single parameter that you can bind to String[] > > Hope that helps. > > Got your point. But wondering why we don't see any difference in performance between method-2 and method-3 above. So does it mean that,I am testing this in a wrong way or it's the expected behaviour and thus there is no meaning in converting the row by row inserts into a bulk insert, but just changing the commit frequency will do the same job in a row by row insert approach? --000000000000e10ea606229f41d4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Sat, Sep 21, 2024 at 9:51=E2=80=AF= AM Micha=C5=82 K=C5=82eczek <micha= l@kleczek.org> wrote:
Hi,

> On 19 Sep 2024, at 07:30, Lok P <loknath.73@gmail.com> wrote:
>
[snip]
>
> Method-4
>
> INSERT INTO parent_table VALUES=C2=A0 (1, 'a'), (2, 'a'= ;);
> INSERT INTO child_table VALUES=C2=A0 =C2=A0(1,1, 'a'), (1,2, &= #39;a');
> commit;

I=E2=80=99ve done some batch processing of JSON messages from Kafka in Java= .
By far the most performant way was to:

1. Use prepared statements
2. Parse JSON messages in Postgres
3. Process messages in batches

All three can be achieved by using arrays to pass batches:

WITH parsed AS (
=C2=A0 SELECT msg::json FROM unnest(?)
),
parents AS (
=C2=A0 INSERT INTO parent SELECT =E2=80=A6 FROM parsed RETURNING ...
)
INSERT INTO child SELECT =E2=80=A6 FROM parsed=E2=80=A6

Not the single parameter that you can bind to String[]

Hope that helps.


Got your point.
But wondering why we don't see any difference in performance=C2= =A0between method-2 and method-3 above. So does it mean that,I am testing t= his in a wrong way or it's the expected behaviour and thus there is no = meaning in converting the row by row inserts into a bulk insert, but just c= hanging the commit frequency will do the same job in a row by row insert ap= proach?
--000000000000e10ea606229f41d4--