Thread: Calling oracle function from PostgreSQL
Hi All,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.Regards,Shweta
Hi All,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.Regards,Shweta
To call an Oracle function from a PostgreSQL database, you can use a Foreign Data Wrapper (FDW) to connect PostgreSQL to Oracle. One of the most commonly used FDWs for this purpose is oracle_fdw
. Here’s a step-by-step guide to achieve this:
Step 1: Install oracle_fdw
Install Oracle Client Libraries: Ensure that the Oracle client libraries are installed on your PostgreSQL server. You can download these from the Oracle website.
Install oracle_fdw: You can install
oracle_fdw
using your package manager or by compiling it from source.For example, on Debian-based systems:
sudo apt-get install postgresql-<version>-oracle-fdw
Or, to compile from source:
git clone https://github.com/laurenz/oracle_fdw.gitcd oracle_fdwmakesudo make install
Step 2: Configure oracle_fdw
Create the Extension: In your PostgreSQL database, create the
oracle_fdw
extension.CREATE EXTENSION oracle_fdw;
Create a Foreign Server: Define a foreign server that connects to your Oracle database.
CREATE SERVER oracle_serverFOREIGN DATA WRAPPER oracle_fdwOPTIONS (dbserver '//oracle_host:1521/oracle_service_name');
Create a User Mapping: Map a PostgreSQL user to an Oracle user.
CREATE USER MAPPING FOR postgresSERVER oracle_serverOPTIONS (user 'oracle_user', password 'oracle_password');
Step 3: Create Foreign Table
- Create a Foreign Table: Define a foreign table in PostgreSQL that maps to the Oracle table or view.
CREATE FOREIGN TABLE oracle_table ( column1 datatype, column2 datatype, ...)SERVER oracle_serverOPTIONS (schema 'oracle_schema', table 'oracle_table');
Step 4: Call Oracle Function
- Call the Oracle Function: You can now call the Oracle function using the foreign table. Here’s an example of how you might do this:
SELECT oracle_function(column1, column2)FROM oracle_tableWHERE some_condition;
Documentation Links
Next Steps
- Test the Connection: Ensure that the connection between PostgreSQL and Oracle is working correctly by querying the foreign table.
- Handle Data Types: Pay attention to data type compatibility between PostgreSQL and Oracle.
- Optimize Performance: Consider performance implications and optimize queries as needed.
HiPlease explore oracle_fdw.Regards,Muhammad IkramOn Fri, Aug 30, 2024 at 12:09 PM Shweta Rahate <rahateshweta20@gmail.com> wrote:Hi All,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.Regards,Shweta--Muhammad Ikram
IMPORTANT: This is the output from Elethena, the AI chatbot specializing in PostgreSQL at https://stormatics.tech/.
HiPlease explore oracle_fdw.
Regards,Muhammad IkramOn Fri, Aug 30, 2024 at 12:09 PM Shweta Rahate <rahateshweta20@gmail.com> wrote:Hi All,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.Regards,Shweta--Muhammad Ikram
On 08/30/2024 12:08 AM PDT Shweta Rahate <rahateshweta20@gmail.com> wrote:Hi All,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.Regards,Shweta
On Fri, 2024-08-30 at 11:54 -0400, Greg Sabino Mullane wrote: > On Fri, Aug 30, 2024 at 11:24 AM Umair Shahid <umair.shahid@gmail.com> wrote: > > IMPORTANT: This is the output from Elethena, the AI chatbot specializing in PostgreSQL at https://stormatics.tech/. > > Please don't do this. The mailing lists (and planet postgres entries) should be human-answered. > On a related note, the answer is not quite correct, and the code samples will not work as written > due to missing spaces. Yes, that answer is pretty much utter nonsense. Yours, Laurenz Albe
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 := 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 = 12 RETURNING outp; That's ugly, but perhaps it is good enough as a workaround. Yours, Laurenz Albe
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.
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 := 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 = 12 RETURNING outp;
That's ugly, but perhaps it is good enough as a workaround.
Yours,
Laurenz Albe