From: Laurenz Albe <laurenz.albe@cybertec.at>
To: rekgrpth@gmail.com
To: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18621: postgres_fdw cannot work with tables that use schemaless functions in triggers
Date: Tue, 17 Sep 2024 11:19:45 +0200
Message-ID: <ecc1d31ae512cc5795bfa470bf548afe4145639c.camel@cybertec.at> (raw)
In-Reply-To: <18621-34e3fc574aa9fbc1@postgresql.org>
References: <18621-34e3fc574aa9fbc1@postgresql.org>
On Tue, 2024-09-17 at 05:28 +0000, PG Bug reporting form wrote:
> Let's create a trigger on a remote server that uses different functions for
> different users. postgres_fdw cannot perform an insert in this case. And the
> remote server may be a production server, where there is no access to change
> the trigger code so that it uses an explicit schema specification!
>
> [...]
> create function multiply(i int) returns int language plpgsql as $body$
> begin
> return i * 1;
> end;$body$;
> create schema alice;
> create function alice.multiply(i int) returns int language plpgsql as $body$
> begin
> return i * 2;
> end;$body$;
> create schema bob;
> create function bob.multiply(i int) returns int language plpgsql as $body$
> begin
> return i * 3;
> end;$body$;
> create function remote_trigger() returns trigger language plpgsql as $body$
> begin
> if tg_when = 'BEFORE' and tg_op in ('INSERT', 'UPDATE') then
> new.i = multiply(new.i);
> new.t = current_user;
> end if;
> return case when tg_op = 'DELETE' then old else new end;
> end;$body$;
> create trigger remote_before_trigger before insert or update or delete on
> remote for each row execute procedure remote_trigger();
>
> [...]
>
> create extension postgres_fdw schema fdw;
> create server remote foreign data wrapper postgres_fdw options (dbname
> 'remote');
> create user mapping for current_user server remote options (user
> 'remote');
> create user mapping for alice server remote options (user 'remote');
> create user mapping for bob server remote options (user 'remote');
> create foreign table remote (i int, t text) server remote options
> (schema_name 'public', table_name 'remote');
> select * from remote;
>
> \connect "user=bob dbname=local"
> insert into remote select i from generate_series(1, 10) i;
>
> ERROR: function multiply(integer) does not exist
> HINT: No function matches the given name and argument types. You might need
> to add explicit type casts.
> CONTEXT: PL/pgSQL function remote.remote_trigger() line 3 at assignment
> remote SQL command: INSERT INTO public.remote(i, t) VALUES ($1, $2)
That is documented
(https://www.postgresql.org/docs/current/postgres-fdw.html#POSTGRES-FDW-REMOTE-QUERY-EXECUTION-ENVIRO...)
"In the remote sessions opened by postgres_fdw, the search_path parameter
is set to just pg_catalog, so that only built-in objects are visible without
schema qualification. This is not an issue for queries generated by
postgres_fdw itself, because it always supplies such qualification.
However, this can pose a hazard for functions that are executed on the
remote server via triggers or rules on remote tables. For example, if a
remote table is actually a view, any functions used in that view will be
executed with the restricted search path. It is recommended to schema-qualify
all names in such functions, or else attach SET search_path options (see
CREATE FUNCTION) to such functions to establish their expected search path
environment."
So I don't see a bug here.
Yours,
Laurenz Albe
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 using the --to and --cc options:
reply via email
To: pgsql-bugs@postgresql.org
Cc: laurenz.albe@cybertec.at, rekgrpth@gmail.com, pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #18621: postgres_fdw cannot work with tables that use schemaless functions in triggers
In-Reply-To: <ecc1d31ae512cc5795bfa470bf548afe4145639c.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