From: Ravi Krishna <s_ravikrishna@aol.com>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: COMMIT IN STORED PROCEDURE WHILE IN A LOOP
Date: Tue, 18 Oct 2022 20:14:05 +0000 (UTC)
Message-ID: <353091661.109766.1666124045054@mail.yahoo.com> (raw)
References: <353091661.109766.1666124045054.ref@mail.yahoo.com>
AWS Aurora based on PG 13
I am writing a sproc to copy a schema into another. Here is the relevant portion of the code.
Basically I want to commit after every table is created. In big schemas with hundreds of table I do not want to run entire operation in one transaction.
I am getting error at COMMIT -> cannot commit while a subtransaction is active.
Is commit not possible in a loop
BEGIN
FOR object IN
SELECT table_name::text FROM information_schema.TABLES WHERE table_schema = source_schema
AND table_type = 'BASE TABLE'
LOOP
buffer := dest_schema || '.' || object;
BEGIN
sql_stmt := 'CREATE TABLE ' || buffer || ' (LIKE ' || source_schema || '.' || object;
sql_stmt := sql_stmt || ' INCLUDING COMMENTS INCLUDING IDENTITY INCLUDING CONSTRAINTS INCLUDING DEFAULTS INCLUDING GENERATED INCLUDING INDEXES)' ;
--RAISE NOTICE '%' , sql_stmt ;
execute sql_stmt ;
COMMIT;
sql_stmt := 'INSERT INTO ' || buffer || ' OVERRIDING SYSTEM VALUE SELECT * FROM ' || source_schema || '.' || object ;
--RAISE NOTICE '%' , sql_stmt ;
execute sql_stmt ;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
GET STACKED DIAGNOSTICS text_var1 = MESSAGE_TEXT,
text_var2 = PG_EXCEPTION_DETAIL,
text_var3 = PG_EXCEPTION_HINT;
print_msg := 'ERROR:->' || text_var1 || '|' || text_var2 || '|' || text_var3 ;
RAISE NOTICE '%' , print_msg ;
END ;
END LOOP;
END;
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: s_ravikrishna@aol.com
Subject: Re: COMMIT IN STORED PROCEDURE WHILE IN A LOOP
In-Reply-To: <353091661.109766.1666124045054@mail.yahoo.com>
References: <353091661.109766.1666124045054.ref@mail.yahoo.com> <353091661.109766.1666124045054@mail.yahoo.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