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 1mxSaa-0005Ky-LB for pgsql-sql@arkaria.postgresql.org; Wed, 15 Dec 2021 11:36:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mxSaX-0000Zj-Na for pgsql-sql@arkaria.postgresql.org; Wed, 15 Dec 2021 11:36:49 +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 1mxSaX-0000Za-DS for pgsql-sql@lists.postgresql.org; Wed, 15 Dec 2021 11:36:49 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mxSaU-0006WS-Om for pgsql-sql@postgresql.org; Wed, 15 Dec 2021 11:36:48 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=UfKoDvxxV40F/Ox2u0xq6eFacucFINGJJ42EgOkDk6U=; b=u1heeMAUmbdguK+xQuTHZMaGPS 6n3GRv9rsQ2dMer3PrfecXra51LDWIwrXXys7Zx/zej5hUsggq0wC9/adLvW61bQYdPFx2pNdclnX yPUIyNTvvIxtxNoK+QuYzZocbtvzWu583Ygbssj3RnBN3Sz+W3zojHvdvBifixvN3aQE=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1mxSaS-0008H8-BM; Wed, 15 Dec 2021 12:36:44 +0100 Date: Wed, 15 Dec 2021 12:36:44 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: James Kitambara Cc: Sandeep Saxena , "pgsql-sql@postgresql.org" Subject: Re: ERROR ON INSERTING USING A CURSOR IN EDB POSTGRESQL Message-ID: <20211215113644.GA26977@depesz.com> Reply-To: depesz@depesz.com References: <820139578.307641.1639046195013.ref@mail.yahoo.com> <820139578.307641.1639046195013@mail.yahoo.com> <615924257.1194226.1639150841188@mail.yahoo.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <615924257.1194226.1639150841188@mail.yahoo.com> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Dec 10, 2021 at 03:40:41PM +0000, James Kitambara wrote: > There is no COMMIT in the loop for processing cursor data. > Sorry I forget to share the procedure on my first email: > Here is a procedure: > ------------------------------------------------------- > CREATE OR REPLACE PROCEDURE public.temp_insert_in_books2( > ) > LANGUAGE 'edbspl' >     SECURITY DEFINER VOLATILE PARALLEL UNSAFE  >     COST 100 > AS $BODY$ >     --v_id         INTEGER; >     v_title      CHAR(10); > v_amount NUMERIC; >     CURSOR book_cur IS >         SELECT title, amount FROM books2 WHERE id >=8; > BEGIN >     OPEN book_cur; >     LOOP >         FETCH book_cur INTO v_title, v_amount; >         EXIT WHEN book_cur%NOTFOUND; > INSERT INTO books2 (title, amount) VALUES (v_title, v_amount); >     END LOOP; > COMMIT; >     CLOSE book_cur; > END > $BODY$; Hi, others helped you with the reported problem, but I'd like to point out that your procedure is doing a job, and it's doing it poorly (slowly). There is no need for iteration. There is no need for cursor. There is even no need for procedure, but let's keep it there. Your whole procedure can be simplified to: CREATE OR REPLACE PROCEDURE public.temp_insert_in_books2() LANGUAGE 'plpgsql'     SECURITY DEFINER VOLATILE PARALLEL UNSAFE      COST 100 AS $BODY$ DECLARE BEGIN INSERT INTO books2 (title, amount) SELECT title, amount FROM books2 where id >= 8; END $BODY$; And that's it. It will be faster (single insert, instead of one-for-each-row), and definitely easier to read and maintain. Best regards, depesz