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 1sl8ON-005Rgb-Uu for pgsql-novice@arkaria.postgresql.org; Mon, 02 Sep 2024 14:50:56 +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 1sl8OM-00FQZr-2n for pgsql-novice@arkaria.postgresql.org; Mon, 02 Sep 2024 14:50:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sl8OL-00FQZi-M2 for pgsql-novice@lists.postgresql.org; Mon, 02 Sep 2024 14:50:54 +0000 Received: from mail-qk1-x735.google.com ([2607:f8b0:4864:20::735]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sl8OJ-000It9-69 for pgsql-novice@lists.postgresql.org; Mon, 02 Sep 2024 14:50:52 +0000 Received: by mail-qk1-x735.google.com with SMTP id af79cd13be357-7a80f66575bso12356585a.1 for ; Mon, 02 Sep 2024 07:50:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725288650; x=1725893450; darn=lists.postgresql.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=Gmt6M4B+aJOogTvWa6vo6Hmw3UCg/LqXXj/9woe9TsU=; b=Egc4O+QRhEbYl6+Fv/IDa5phGxo50+Ijg5XhvPfA5k7xvubVVK6xQVkY31iEAyK80T scVnx7HpmMfawoDu9HhDFSpQPp536BtFO2WVVUisGO5jJzPZsN93qEhKZol08dLBavEn BfjP07b+fm+so/MPTS0W0Qa/0BfLaIW2TMdh4+uZavPl29wuQIhUDwDyejzSSfIO+sxs mmAvXNQ2DzvThKQRZinYpcWoR78e/c3FsuVI5c8qUu8KhIN22xpd2rrNzNpKkhJoexM/ VyE2lMHppodA9Lac5tiaQF2XWMrE0mgonahv7smvJsm/hRF74ZlpYWgz62tcyDD3joBN Tyow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725288650; x=1725893450; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Gmt6M4B+aJOogTvWa6vo6Hmw3UCg/LqXXj/9woe9TsU=; b=SRpiIN8oEKGtTuqJk46pQ+PfaGihUrSqC3pcJ7Z7cb6AyoacrkS3BuwgDQPfzHEU5Q vofQo5LjMiH/HSedwPSTdj78QkalECHfMCTyRSxweVGYzfoxTAhkpZj1PJKqq87lyRZx WLpxWkQjOgbH6aaVLgPABu83X88M8hqnFMX4c+kHaGHs/fEQw+p8eruLWNFHBK+ZGmH2 WGGTERzrXLW1yiN0GczODI5nCIfuFUT8GDwAQDckHd8SWxgbGUeNC/cpsLMQpCoVZLHE PYKH8NNmj4QT8xY963N4imPIagUtizj+xqYg2hVV450XYfBE+9wA9mVyJk+u1INxVHjA 8O6A== X-Gm-Message-State: AOJu0Yyd6+lt4NesiknjrTWEVcfjQpmOynq+vYTo+E+fW5bEgp6+FfrF j4ADedizvu8KHNCeskeTTmHqYX2FrmBbrTnzoZcZ/NG87zjE4R+77Tr4ejestNeIUFUPyZROgmV /loOw8QDQpwNWl0gMw54NflYwnag+Ki2X1K8= X-Google-Smtp-Source: AGHT+IFqtsDw7DnrTR5KIBukncEGhXBHfloAnz4aj9MX4AFNdVhODcnY9neYKW2nuUVMDKuUt4NP3UwtU1095kqvsKY= X-Received: by 2002:a05:6214:4103:b0:6b4:fda5:88b6 with SMTP id 6a1803df08f44-6c3495f9315mr61904106d6.1.1725288649695; Mon, 02 Sep 2024 07:50:49 -0700 (PDT) MIME-Version: 1.0 References: <667ba77c9d834363c2d23dab61ab2f003f68be8c.camel@cybertec.at> In-Reply-To: <667ba77c9d834363c2d23dab61ab2f003f68be8c.camel@cybertec.at> From: Adam Brusselback Date: Mon, 2 Sep 2024 10:50:33 -0400 Message-ID: Subject: Re: Calling oracle function from PostgreSQL To: pgsql-novice@lists.postgresql.org Content-Type: multipart/alternative; boundary="00000000000019ab19062124115d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000019ab19062124115d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Re: That table hack Oh man is scary as can be (to me). I think I would go with another option (maybe outside of the database) entirely rather than introducing that into my codebase. Onto the general need: I've definitely had the need for foreign function calls between my (both Postgres) databases (e.g. dwh server calling a function to get some info from oltp server), and I had to resort to dblink for that. Would have been very nice if the FDW interface had support for functions / stored procedures as first class citizens as long as the fdw implementation (and other endpoint) support functions / stored procedures. Once could dream. -Adam On Mon, Sep 2, 2024 at 9:34=E2=80=AFAM Laurenz Albe wrote: > On Fri, 2024-08-30 at 12:38 +0530, Shweta Rahate wrote: > > In my application there is a requirement to call the oracle function > from PostgreSQL db. > > > > The oracle function should take the input from Postgres db and returns > the output. > > Please suggest a way to achieve this. > > There is no direct way to do this via oracle_fdw. > > There are, however, a couple of hacks to do that; see the following > example: > > The Oracle function: > > CREATE OR REPLACE FUNCTION double(n NUMBER) RETURN NUMBER AS > BEGIN > RETURN n * 2; > END; > / > > Then I can define an Oracle table with a single row and a trigger on it: > > CREATE TABLE call_double(inp NUMBER, outp NUMBER); > > INSERT INTO call_double VALUES (1, 1); > > COMMIT; > > CREATE TRIGGER double_trig BEFORE UPDATE ON call_double FOR EACH ROW > BEGIN > :NEW.outp :=3D double(:NEW.inp); > END; > / > > Now I can define a foreign table as follows: > > CREATE FOREIGN TABLE call_double( > inp numeric OPTIONS (key 'true'), > outp numeric) > SERVER oracle OPTIONS (table 'CALL_DOUBLE'); > > And then the following UPDATE calls the function and returns the result: > > UPDATE call_double SET inp =3D 12 RETURNING outp; > > That's ugly, but perhaps it is good enough as a workaround. > > Yours, > Laurenz Albe > > > --00000000000019ab19062124115d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Re: That table hack
Oh man i= s scary as can be (to me). I think I would go with another option (maybe outside of the=20 database) entirely rather than introducing that into my codebase.

Onto the general need:
I've definitely ha= d the need for foreign function calls between my (both Postgres) databases = (e.g. dwh server calling a function to get some info from oltp server), and= I had to resort to dblink for that. Would have been very nice if the FDW i= nterface had support for functions / stored procedures as first class citiz= ens as long as the fdw implementation (and other endpoint) support function= s / stored procedures.

Once could dream.

-Adam<= br>
On Mon, Sep 2, 2024 at 9:34=E2=80=AFAM Laurenz Albe <laurenz.albe@cybertec.at> wrote:
On Fri, 2024-08-30 at 1= 2:38 +0530, Shweta Rahate wrote:
> In my application there is a requirement to call the oracle function f= rom PostgreSQL db.
>
> The oracle function should take the input from Postgres db and returns= the output.
> Please suggest a way to achieve=C2=A0this.=C2=A0

There is no direct way to do this via oracle_fdw.

There are, however, a couple of hacks to do that; see the following example= :

The Oracle function:

=C2=A0 CREATE OR REPLACE FUNCTION double(n NUMBER) RETURN NUMBER AS
=C2=A0 BEGIN
=C2=A0 =C2=A0 =C2=A0RETURN n * 2;
=C2=A0 END;
=C2=A0 /

Then I can define an Oracle table with a single row and a trigger on it:
=C2=A0 CREATE TABLE call_double(inp NUMBER, outp NUMBER);

=C2=A0 INSERT INTO call_double VALUES (1, 1);

=C2=A0 COMMIT;

=C2=A0 CREATE TRIGGER double_trig BEFORE UPDATE ON call_double FOR EACH ROW=
=C2=A0 BEGIN
=C2=A0 =C2=A0 =C2=A0:NEW.outp :=3D double(:NEW.inp);
=C2=A0 END;
=C2=A0 /

Now I can define a foreign table as follows:

=C2=A0 CREATE FOREIGN TABLE call_double(
=C2=A0 =C2=A0 =C2=A0inp numeric OPTIONS (key 'true'),
=C2=A0 =C2=A0 =C2=A0outp numeric)
=C2=A0 SERVER oracle OPTIONS (table 'CALL_DOUBLE');

And then the following UPDATE calls the function and returns the result:
=C2=A0 UPDATE call_double SET inp =3D 12 RETURNING outp;

That's ugly, but perhaps it is good enough as a workaround.

Yours,
Laurenz Albe


--00000000000019ab19062124115d--