Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rN7P2-009Pbv-Bu for pgsql-sql@arkaria.postgresql.org; Tue, 09 Jan 2024 08:24:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rN7P1-00B2ii-Gp for pgsql-sql@arkaria.postgresql.org; Tue, 09 Jan 2024 08:24:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rN7P1-00B2ia-8E for pgsql-sql@lists.postgresql.org; Tue, 09 Jan 2024 08:24:03 +0000 Received: from smtprelay02.ispgateway.de ([80.67.31.25]) by magus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rN7Oz-000cIV-PX for pgsql-sql@lists.postgresql.org; Tue, 09 Jan 2024 08:24:02 +0000 Received: from [80.129.166.138] (helo=smtpclient.apple) by smtprelay02.ispgateway.de with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.1) (envelope-from ) id 1rN7Ox-0000Ne-2H for pgsql-sql@lists.postgresql.org; Tue, 09 Jan 2024 09:23:59 +0100 From: Maximilian Tyrtania Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.200.91.1.1\)) Subject: Problem with refcursor Message-Id: <990A111A-190F-485A-9B96-665D6D3CABBD@inqua-institut.de> Date: Tue, 9 Jan 2024 09:23:49 +0100 To: pgsql-sql@lists.postgresql.org X-Mailer: Apple Mail (2.3774.200.91.1.1) X-Df-Sender: bWF4aW1pbGlhbi50eXJ0YW5pYUBpbnF1YS1pbnN0aXR1dC5kZQ== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi there, I=E2=80=99m running into trouble with ref cursors. I=E2=80=99ve got these 2 functions, this inner one: CREATE or replace FUNCTION f_client_getCoachingsuccessrate(p_clients = refcursor,out successrate numeric, out unclearrate numeric, out failrate = numeric) AS $$ DECLARE curClient record; vNumberOfClients bigint; vSuccessCounter BIGINT=3D0; vUnclearCounter BIGINT=3D0; vFailureCounter BIGINT=3D0; vCurSuccessState boolean; BEGIN FOR curClient IN FETCH ALL FROM p_clients LOOP =E2=80=94some processing END LOOP; = successrate=3Df_bigint_getpercentage(vSuccessCounter,vNumberOfClients); = unclearrate=3Df_bigint_getpercentage(vUnclearCounter,vNumberOfClients); = failrate=3Df_bigint_getpercentage(vFailureCounter,vNumberOfClients); */ END; $$ LANGUAGE plpgsql; =E2=80=A6and this outer one: create or replace function f_client_get3rdFeedbacksuccessrate(out = successrate numeric, out unclearrate numeric, out failrate numeric) as=20= $$ DECLARE invitedClients refcursor; BEGIN open invitedClients FOR SELECT c.* FROM client c join email e on = e.client_id=3Dc.id where e.textblock_id=3D340; --raise notice 'all is fine so far'; Select rates.successrate,rates.unclearrate,rates.failrate from = f_client_getCoachingsuccessrate(invitedClients) rates into = successrate,unclearrate ,failrate; end; $$ LANGUAGE plpgsql; Now, calling the outer one like this: select * from f_client_get3rdFeedbacksuccessrate(); results in: Query 1 ERROR at Line 1: : ERROR: cannot open FETCH query as cursor CONTEXT: PL/pgSQL function f_client_getcoachingsuccessrate(refcursor) = line 10 at FOR over SELECT rows SQL statement "SELECT f_client_getCoachingsuccessrate(invitedClients)" PL/pgSQL function f_client_get3rdfeedbacksuccessrate() line 8 at PERFORM Any pointers? Thanks, Max=