From: Laurenz Albe <laurenz.albe@cybertec.at>
To: Wiltsch,Sigrid <Sigrid.Wiltsch@oclc.org>
To: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Cc: Matthias Apitz <guru@unixarea.de>
Subject: Re: Problem close curser after rollback
Date: Wed, 30 Sep 2020 17:26:39 +0200
Message-ID: <f2af04c4fc6af1791c52b8bd2937522832b4df8c.camel@cybertec.at> (raw)
In-Reply-To: <DS7PR06MB68850E9EB5C735140A5CC5D3FF330@DS7PR06MB6885.namprd06.prod.outlook.com>
References: <DS7PR06MB6885FE41D39C4A2779CF13A7FF330@DS7PR06MB6885.namprd06.prod.outlook.com>
<DM6PR06MB6169D90391183C6AA56376C4EA330@DM6PR06MB6169.namprd06.prod.outlook.com>
<DS7PR06MB68850E9EB5C735140A5CC5D3FF330@DS7PR06MB6885.namprd06.prod.outlook.com>
On Wed, 2020-09-30 at 13:32 +0000, Wiltsch,Sigrid wrote:
> we use the following statements in our applications, as described on the site:
>
> https://www.postgresql.org/docs/11/ecpg-commands.html#ECPG-TRANSACTIONS
>
> EXEC SQL PREPARE stmt1 FROM "SELECT oid,datname FROM pg_database WHERE oid > ?";
> EXEC SQL DECLARE foo_bar CURSOR FOR stmt1;
>
> /* when end of result set reached, break out of while loop */
> EXEC SQL WHENEVER NOT FOUND DO BREAK;
>
> EXEC SQL OPEN foo_bar USING 100;
> ...
> while (1)
> {
> EXEC SQL FETCH NEXT FROM foo_bar INTO :dboid, :dbname;
> ...
> }
>
> EXEC SQL CLOSE foo_bar;
>
> After every fetch we open a transaction which is terminated with a rollback in the event of an error.
>
> The problem we now have with this is, that the cursor is obviously closed with the rollback,
> so the next fetch ends with the error that the cursor no longer exists (sqlcode -400
>
> What can I do so that the cursor is retained despite rollback?
>
> The procedure described is very often found in our applications, which have been ported from sybase to postgreSQL.
You cannot start a transaction while you are reading a cursor; you probably
get a warning "there is already a transaction in progress".
You could try using a CURSOR WITH HOLD.
Such cursors outlast a database transaction. They get materialized on the server
during commit.
So you could create the WITH HOLD cursor, commit and then start your individual transactions.
Don't forget to close the cursor when you are done, else it will use server resources
until you close the database connection.
Another option is to use savepoints, but you may run into performance problems
if you use more than 64 of them and have concurrent database activity.
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.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: laurenz.albe@cybertec.at, Sigrid.Wiltsch@oclc.org, guru@unixarea.de
Subject: Re: Problem close curser after rollback
In-Reply-To: <f2af04c4fc6af1791c52b8bd2937522832b4df8c.camel@cybertec.at>
References: <DS7PR06MB6885FE41D39C4A2779CF13A7FF330@DS7PR06MB6885.namprd06.prod.outlook.com> <DM6PR06MB6169D90391183C6AA56376C4EA330@DM6PR06MB6169.namprd06.prod.outlook.com> <DS7PR06MB68850E9EB5C735140A5CC5D3FF330@DS7PR06MB6885.namprd06.prod.outlook.com> <f2af04c4fc6af1791c52b8bd2937522832b4df8c.camel@cybertec.at>
* 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