From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Rama Krishnan <raghuldrag@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Batch process
Date: Wed, 20 Jul 2022 10:39:01 -0700
Message-ID: <192eea11-1cb2-89d8-e5f0-cdc5dcf2e995@aklaver.com> (raw)
In-Reply-To: <CAJWX+EOGj+8gWm9cwjfVxB=pv2haVopDTJ4TzPmQvvX515nO3A@mail.gmail.com>
References: <CAJWX+ENJm8tsZ2y9z075w-4Vbtv4GQaLOQpVb8b-VYiOea1oPw@mail.gmail.com>
<375897a0-fbbb-8712-8164-8fbd3cab074a@aklaver.com>
<CAJWX+ENuYcKuBo8X122wAKUoS1BCiYC8fprjxYgK54fEo09Hmg@mail.gmail.com>
<CAJWX+EOGj+8gWm9cwjfVxB=pv2haVopDTJ4TzPmQvvX515nO3A@mail.gmail.com>
On 7/20/22 9:38 AM, Rama Krishnan wrote:
Reply to list also
Ccing list.
> Hi ALL,
>
> I have created the batch wise query but the variable is not working in
> the delete.
>
>
> create or replace function sports_sale() returns void as $$
> declare
> counter integer := 0;
> row_count integer :=0;
> start integer :=1;
>
> begin
>
> SELECT sale_start_count INTO start FROM sale_delete_counter ORDER BY
> ID DESC LIMIT 1;
> SELECT sale_loop_counter INTO counter FROM sale_delete_counter ORDER
> BY ID DESC LIMIT 1;
> SELECT count(*) INTO row_count FROM sports_sale_archive;
> SELECT ceil(row_count/10000) INTO row_count;
> while counter < row_count loop
> raise notice 'Counter %', counter;
> delete from sports_sale where id in (select id from
> sports_sale_archive WHERE id between start and start+9999);
> counter := counter + 1;
> start :=start+10000;
> INSERT INTO
> sale_delete_counter(sale_start_count,sale_loop_counter)
> VALUES(start,counter);
> exit when counter>5;
> end loop;
> end;$$ language plpgsql;
1) I thought this was a date based deletion?
2) How do you know that the ids in "id between start and start+9999"
actually exist?
3) Could this not be simplified to something like?:
create or replace function sports_sale() returns void as $$
declare
counter integer;
begin
select count(*) into counter from sports_sale_archive where
<date/id> between <start> and <end>;
while counter > 0 loop
raise notice 'Counter %', counter;
delete from sports_sale where id in (select id from
sports_sale_archive WHERE id between <start> and <end> order by
<date/id> limit 10000);
counter := counter - 10000;
end loop;
end;$$ language plpgsql;
Not tested and should be taken as starting point as it is not entirely
clear to me what you are trying to achieve.
>
>
> Here I have created the archive table based on created_date with more
> one year data. i want to execute this delete query using batch wise(each
> iteration 10K totally 50K records deletion per execution ). The issue
> was that the variable was not working in deletion subquery.
> Regards
>
> A.Rama Krishnan
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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: adrian.klaver@aklaver.com, raghuldrag@gmail.com
Subject: Re: Batch process
In-Reply-To: <192eea11-1cb2-89d8-e5f0-cdc5dcf2e995@aklaver.com>
References: <CAJWX+ENJm8tsZ2y9z075w-4Vbtv4GQaLOQpVb8b-VYiOea1oPw@mail.gmail.com> <375897a0-fbbb-8712-8164-8fbd3cab074a@aklaver.com> <CAJWX+ENuYcKuBo8X122wAKUoS1BCiYC8fprjxYgK54fEo09Hmg@mail.gmail.com> <CAJWX+EOGj+8gWm9cwjfVxB=pv2haVopDTJ4TzPmQvvX515nO3A@mail.gmail.com> <192eea11-1cb2-89d8-e5f0-cdc5dcf2e995@aklaver.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