Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oEDfM-0006fE-QG for pgsql-general@arkaria.postgresql.org; Wed, 20 Jul 2022 17:39:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oEDfK-0000pu-Sg for pgsql-general@arkaria.postgresql.org; Wed, 20 Jul 2022 17:39:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oEDfJ-0000hc-CK for pgsql-general@lists.postgresql.org; Wed, 20 Jul 2022 17:39:18 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oEDfB-0007um-6x for pgsql-general@postgresql.org; Wed, 20 Jul 2022 17:39:16 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 65677320029B; Wed, 20 Jul 2022 13:39:03 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Wed, 20 Jul 2022 13:39:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1658338742; x= 1658425142; bh=ID/7WFsxuome7h0pcMfEGxlGnaGUHO9KTCWXTb1rhMs=; b=Y ALIAoXC4Foe7kDpG7/eCw9kJU12EvFwSxLQkr2erohYVN+u570+cXY8f4IQm8nBo DgMFjunuwwgB4gRxzp7CYBiQW0m9ws4p8xRode7uayyJPtrbXsGzF5SUwdylGvAi Dpye2M+CZXyU71VBBd7QO/BMXxk9T/a1KwEmnc/BaagCkge7k4jHyTmJj6swhWyT sibFpVnCew8yD6Ws3pP8PvAds0nIlbphydQrt1OJEd0d13denSMmFt5LJXKgJeby 4bJVdVp0pnh0vye/dy8CZOa8i3BkNVp2RvQqg7f1D/UESvRDfXBq+SOH+T+mq+cK Hx4acFReDtho4ld2pgSMQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1658338742; x= 1658425142; bh=ID/7WFsxuome7h0pcMfEGxlGnaGUHO9KTCWXTb1rhMs=; b=U Ew4P3xRlbij3eGbkUjgYoRGBq4sDzdyln1jpQ2E7yLvyuFRSn95YVDahCP7iHai9 HXehCXE1O98PzAusd1crJhFN4PZK/+7czMKKzkGApdtdalsJtqOcbJfs+7JQPHC8 1aNefzdI1+uQ66voQPKAQxcVpUax5ip7vFWvRfaG8aN/pfUZk9MPzIwMWl9Hk7yJ 0TLl0Kvp0CMB9NcOJg+dBnbQpWyx/pZPuCfTU9uD8FV/QLn6tTxV1cyJ30Iukn8Q O1zGwXOleJtMjSILkbsBQtvDPcljin8cw76dxrOFcFNpTT89cPyHe01VPkqKOjQi xB85fZF0yP+E+K31VJ8JQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrudelvddguddujecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhevhffkffgfgggjtgfgse htkeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceorggurhhirghn rdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrfgrthhtvghrnhepgfegje ekveejfefffeekueffhfekteefvdelveegueduudejffeufeeuffeifeeknecuvehluhhs thgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklh grvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 20 Jul 2022 13:39:02 -0400 (EDT) Subject: Re: Batch process To: Rama Krishnan References: <375897a0-fbbb-8712-8164-8fbd3cab074a@aklaver.com> Cc: pgsql-general From: Adrian Klaver Message-ID: <192eea11-1cb2-89d8-e5f0-cdc5dcf2e995@aklaver.com> Date: Wed, 20 Jul 2022 10:39:01 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 between and ; while counter > 0 loop raise notice 'Counter %', counter; delete from sports_sale where id in (select id from sports_sale_archive WHERE id between and order by 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