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 1kNe0F-000547-G7 for pgsql-general@arkaria.postgresql.org; Wed, 30 Sep 2020 15:26:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kNe0E-0000gU-4g for pgsql-general@arkaria.postgresql.org; Wed, 30 Sep 2020 15:26:46 +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 1kNe0D-0000gN-On for pgsql-general@lists.postgresql.org; Wed, 30 Sep 2020 15:26:45 +0000 Received: from mail-wm1-x329.google.com ([2a00:1450:4864:20::329]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kNe0A-000716-Rs for pgsql-general@lists.postgresql.org; Wed, 30 Sep 2020 15:26:45 +0000 Received: by mail-wm1-x329.google.com with SMTP id e11so257256wme.0 for ; Wed, 30 Sep 2020 08:26:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:cc:date:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=XODmhAM8KRlyFDtjjmeAQMFVEuOWBcc8Kpca/ab1Kcg=; b=jY7p+kKYJ8L7h52J19RZ0BBXDrKv/BYA3b+nHuOdv06EHRq9t7N7F5yiGEQjtA4++b MlFbRJ7+VMJSIgU5UGFPCogifx1aSmNElK/92l1/1ALHbx+6/45Fd1r4yn0TrHoLLua/ 9xpItVSi3DomgHtN9/ZRNvXHViRnyQrKIhLdCNeYSEioDMsvUXczPMPT+XinREKmAuTs 0LWjNHaZZhqc+n6r6MviHIVCsPoM73lGStStQZ6rprDCNzwVj/Om21kMXaWmPxTjXcJ2 BSbmoR9sWDKTFhxRHu9wpstm71ugQVUDf0UPiOvj5TqiThRx4X1+t8p6CtNOc0Oq237d z/Ow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=XODmhAM8KRlyFDtjjmeAQMFVEuOWBcc8Kpca/ab1Kcg=; b=b+G2Er+QmxdKyBUozhM2B6xSghL5qY/ITbY42Gu73UIDUvuZpLrk/eA+hs3xxXZfkD r5CW+OT62rGeIkpkzlIFx2niHWggDXzVvebxD+BCDRLz7f5JkF6iTTBczY/hgGIc5QJ/ +3FVroSzbQTgrg9H0etJ25fsgVnB0paWa9zLa0dd+xSqV69WlUjWmpxG57SajD03U3nJ 49eptFK+qrnqUJKIMc13e3Ncu98h/1dJ9Pfae9BhVLUpc0IkZ4nsbXqlvoJMfdsdkuE0 XYseBiUdlrXEGlrjcjgXYB9qGx0P7lnVBOawOmE50oVKbCeBSE8idKTmfZsKxuisv6sH rXnA== X-Gm-Message-State: AOAM532R5bSCfYq0k3G8oTkE6dy44HtlQ6UqKwfo86dNvJUDBuKiJkWT PGCddTnZsBThO4MkyvgNoRE3Qw== X-Google-Smtp-Source: ABdhPJwNN5rciV9GXYHfmaeoTVw6mGYxFswcYetbd21OKup6j5Z95gwPwkdiNzS6U4jUy7KPokKexw== X-Received: by 2002:a1c:63d7:: with SMTP id x206mr3758948wmb.183.1601479601693; Wed, 30 Sep 2020 08:26:41 -0700 (PDT) Received: from localhost.localdomain (217-149-169-101.nat.highway.telekom.at. [217.149.169.101]) by smtp.gmail.com with ESMTPSA id l18sm3560552wrp.84.2020.09.30.08.26.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 30 Sep 2020 08:26:40 -0700 (PDT) Message-ID: Subject: Re: Problem close curser after rollback From: Laurenz Albe To: "Wiltsch,Sigrid" , "pgsql-general@lists.postgresql.org" Cc: Matthias Apitz Date: Wed, 30 Sep 2020 17:26:39 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.5 (3.36.5-1.fc32) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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