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 1sqUNG-00A7sB-1C for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Sep 2024 09:19:55 +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 1sqUNE-007zJ7-9T for pgsql-bugs@arkaria.postgresql.org; Tue, 17 Sep 2024 09:19:52 +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 1sqUND-007zIz-S5 for pgsql-bugs@lists.postgresql.org; Tue, 17 Sep 2024 09:19:52 +0000 Received: from mail-wm1-x330.google.com ([2a00:1450:4864:20::330]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sqUNA-001cDn-BZ for pgsql-bugs@lists.postgresql.org; Tue, 17 Sep 2024 09:19:50 +0000 Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-42cc43454d5so34211585e9.3 for ; Tue, 17 Sep 2024 02:19:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1726564787; x=1727169587; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=xuDRvAL1aBMLn48rE3uODJkEBPyL/0HcI+pWGj2vtWE=; b=pNBe9ANkoFtZA+q47kkcyvsZSFA99QR2z8TzGGOQht0kp9bTpJh8ovaoTy+zJL7Mk8 KMAE6gWYRivWnyrfvA/C3l4Lxz5hpAkUEovR4HxrXqfC8xLnBtHYAEcqBLhA6pNXdCK9 hdTRpXBmlg601j/UGrV3VeroEO+WGkQd5aqXxkA6Y5wWcQ61UYauEbcpi2sKVuvDNxBY XYx0wvf/tVNjP4O6BL/BOUevSli8ETj9zOIa7olpI5VkkB7lx6393SGyuhp8PCFdjUsS GRK19r38TTBDKIakyvugWZZkifrTAMGwFGvlFFvXBOnM9mt93ieyIw25rN4CPZeCV/t5 r0OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726564787; x=1727169587; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=xuDRvAL1aBMLn48rE3uODJkEBPyL/0HcI+pWGj2vtWE=; b=EoyECFtFFpVRIKHbnNy4o2TCzrlApyNN1UkmbSJtDxV2tPumOdwt8YH2vANkTidI7X y2O4dz9QjAh61/CMPxJLsOB4AR0fJDB09SxZ5F7kLscqM+5225S+eVKizGaA2KCfAR+i 7/6mFidjYhf83xLvClekLkisQ3s7HVsBCSubDtoh00hf9iQt9FzuTkU7YCv+bLPq+574 zg2TCpImXjsYuKzxmskZkHiiOBohBT8quAl++JdZQjffy/Fu1s6q0NGtKCXU7lUvPeID RweX3yUGj7MBXH+558udJMfvfeNHzXOABvaEThNl8MZnc5LZtvsMFKLEQJ/TCJKIY7eO u6Qg== X-Forwarded-Encrypted: i=1; AJvYcCUt0dbd+QO4XoOZw2GZX85PzUz3tCkqIKsM9AyyLFlBJnmnWMRAaWrnK4spPqzDXhG0fZHzd/yPrrPr@lists.postgresql.org X-Gm-Message-State: AOJu0YzgYuOgBS0WqttxlhaWnCHdtQTKtJPMQdLZ3Xi76iPVXCJsLt9t 8Ir0eUkXKvG2Tf9PCx5kV2cj1SC7n5NRdiGRLPhGG8354hQB1hw/n4xnQUOzaZmGNXV+s4zptUl a X-Google-Smtp-Source: AGHT+IGILOttuJur2d8LnhjfsozBAW3xd3a542GQ740aBycE1602Ej1vC1xsMvt8MpoP2jaJg5MIaQ== X-Received: by 2002:a05:600c:3d86:b0:426:616e:db8d with SMTP id 5b1f17b1804b1-42d9082a638mr101392865e9.15.1726564786401; Tue, 17 Sep 2024 02:19:46 -0700 (PDT) Received: from dynamic-pd01.res.v6.highway.a1.net ([2001:871:5e:1ab4:7288:39c1:e66b:33e4]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-378e71f062esm9073633f8f.7.2024.09.17.02.19.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Sep 2024 02:19:46 -0700 (PDT) Message-ID: Subject: Re: BUG #18621: postgres_fdw cannot work with tables that use schemaless functions in triggers From: Laurenz Albe To: rekgrpth@gmail.com, pgsql-bugs@lists.postgresql.org Date: Tue, 17 Sep 2024 11:19:45 +0200 In-Reply-To: <18621-34e3fc574aa9fbc1@postgresql.org> References: <18621-34e3fc574aa9fbc1@postgresql.org> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-1.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 f= or > 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 cha= nge > the trigger code so that it uses an explicit schema specification! >=20 > [...] > 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 $bo= dy$ > 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 $bod= y$ > begin > if tg_when =3D 'BEFORE' and tg_op in ('INSERT', 'UPDATE') then > new.i =3D multiply(new.i); > new.t =3D current_user; > end if; > return case when tg_op =3D '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(); >=20 > [...] > > 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; >=20 > \connect "user=3Dbob dbname=3Dlocal" > insert into remote select i from generate_series(1, 10) i; >=20 > ERROR: function multiply(integer) does not exist > HINT: No function matches the given name and argument types. You might n= eed > 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-REM= OTE-QUERY-EXECUTION-ENVIRONMENT) "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 wit= hout 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-qu= alify all names in such functions, or else attach SET search_path options (see CREATE FUNCTION) to such functions to establish their expected search pa= th environment." So I don't see a bug here. Yours, Laurenz Albe