Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o9A6f-0006yd-Fr for pgsql-general@arkaria.postgresql.org; Wed, 06 Jul 2022 18:50:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1o9A6c-0000KD-HD for pgsql-general@arkaria.postgresql.org; Wed, 06 Jul 2022 18:50:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1o9A6c-0000K4-2L for pgsql-general@lists.postgresql.org; Wed, 06 Jul 2022 18:50:34 +0000 Received: from mail-pg1-x532.google.com ([2607:f8b0:4864:20::532]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1o9A6Y-0004rY-Vs for pgsql-general@lists.postgresql.org; Wed, 06 Jul 2022 18:50:33 +0000 Received: by mail-pg1-x532.google.com with SMTP id q82so7520592pgq.6 for ; Wed, 06 Jul 2022 11:50:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yugabyte.com; s=google; h=from:message-id:mime-version:subject:date:in-reply-to:cc:to :references; bh=5HZ0qTneKh1CkLn+MZ31Z3ncNhZ5UhBkSPeozD2ZbK4=; b=KiSGQ7wz22yAKPDny+ouPB7dTf/zjbrdTIYCC8RXjqtaD3jIuESbspYsEO08/swt63 uPgebAo/vGeDKEDrXf8hR3d1hFxoxeRJjG4NnYIck77GguLBw2q+8aZ7qOaT0QN0m8ov Qck/77UNQdfc/y1m9QAxB+6FjkG4CBkJ7SveM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:message-id:mime-version:subject:date :in-reply-to:cc:to:references; bh=5HZ0qTneKh1CkLn+MZ31Z3ncNhZ5UhBkSPeozD2ZbK4=; b=HLk0b9sCMTEWEUdXXOpvTttdq/SJzndWzmHu/iYIKWh0XVLRTXIK8TDpNzcu4Bxtzq 0kDWpAX7USHmodZPmlaIRWvv4p5+XDPeT3WZXGqiv2ch2F+8EdX/mgU7Bu4AsRBJQgEN 1hr9lXLOo2c9aL7X/OEIa8CdDOipIkuGWJ7uBa12+TLIuvivfjQID5KjqiytFbBkGaV9 9JnyqxwYNqbnkGUBNq/dWlhWEf1UD5MCno7e0jiaa5sZPvKKl5/AD4GflWHHA58Ph5Af E/NWtIxSLJihEVa8/cxRwmKgCIs+xG7S4nEJDs9z2t5+IuBDMXpyx2GkklrriQiumqkd B7yA== X-Gm-Message-State: AJIora9mZGWY4Ejd92JoxHxem7UQ0sDzBrnQZQUdKSxJOD7PXH4IcwOt kVOgKt9FrIUOl+WJGyMaJfdq4Q== X-Google-Smtp-Source: AGRyM1t/HMBPj0RPw5uWF1DHOKOCHHS7TfhCQlEwbAfQJP0K1DXSwZqITLiZh/1nv6EniSvC2LjepA== X-Received: by 2002:a05:6a00:1895:b0:527:f270:64de with SMTP id x21-20020a056a00189500b00527f27064demr42194775pfh.61.1657133428509; Wed, 06 Jul 2022 11:50:28 -0700 (PDT) Received: from smtpclient.apple ([2601:646:101:7fda:4575:42ac:10ec:1715]) by smtp.gmail.com with ESMTPSA id b6-20020a170902650600b0016be4d310b2sm6906330plk.80.2022.07.06.11.50.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Jul 2022 11:50:27 -0700 (PDT) From: Bryn Llewellyn Message-Id: Content-Type: multipart/alternative; boundary="Apple-Mail=_D9F3BB35-6BC2-4DE4-8459-455A95A517C4" Mime-Version: 1.0 (Mac OS X Mail 14.0 \(3654.120.0.1.13\)) Subject: Re: Seems to be impossible to set a NULL search_path Date: Wed, 6 Jul 2022 11:50:26 -0700 In-Reply-To: Cc: pgsql-general list To: "David G. Johnston" References: <4B5F24E6-2FF9-4964-BF07-C609DC38E377@yugabyte.com> X-Mailer: Apple Mail (2.3654.120.0.1.13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_D9F3BB35-6BC2-4DE4-8459-455A95A517C4 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > david.g.johnston@gmail.com wrote: >=20 > unless you are going to write: operator(pg_catalog.=3D) in your = function the advice to always use schema qualifications is not going to = be taken seriously... the correct search_path to set isn't "empty" but = "pg_catalog", "pg_temp". While this does violate "DRY" principles it is = the solution you are looking for. =20 The advice always to use fully qualified names is orthogonal to the = advice to set an explicit search_path in a subprogram's definition. Am I = right that your point is about what search path to set and has nothing = to do with the possible use of qualified names? I do see that I can achieve my goal by setting the search_path to = pg_catalog, pg_temp in my functions rather than to empty. So I can adopt = that practice. But it's at odds with an example in this section: Writing SECURITY DEFINER Functions Safely = https://www.postgresql.org/docs/current/sql-createfunction.html#id-1.9.3.6= 7.10.2 The example sets a search path the does *not* mention pg_catalog. I hadn't come across this locution: operator(pg_catalog.=3D) But I found an account here: 4.2.5. Operator Invocations = https://www.postgresql.org/docs/current/sql-expressions.html#SQL-EXPRESSIO= NS-OPERATOR-CALLS To be sure, I tried this: select 17 operator(pg_catalog.=3D) 17; It ran without error and produced TRUE. I believe that you're saying that because (as I presume) the "normal" = definition of the =3D operator is done by code in the pg_catalog schema, = pg_catalog must be on the path for ordinary equality tests to work. (I = also found random folks saying this on StackExchange.) So I tried this: create function f() returns boolean set search_path =3D '' security definer language plpgsql as $body$ begin return (2 + 5) =3D 7; end; $body$; select f()::text; It succeeded and said true. I don't know how to explain this outcome. = For good measure, I tried this too: create procedure p() set search_path =3D '' security definer language plpgsql as $body$ begin create temporary table x(k int); end; $body$; call p(); It succeeded. And the \d metacommand showed me that I now have a table = pg_temp_3.x. Using a different database, it ends up in "pg_temp_1. = What's going on here? Is "pg_temp" a kind of generic nickname for ANY = "pg_temp_N"? I hate to realize that I'm failing to understand a fundamental = principle. It rather looks like the name-res to pg_catalog and (some) pg_temp is = hard-coded and doesn't rely on the reigning search_path. Or, to put it = another way, these two schemas are inevitably at the end of the = search_path no matter what you set explicitly, and never mind that "show = search_path" doesn't show them unless you also put them on the path = (again) explicitly. I can't make sense of this wording from "Writing SECURITY DEFINER = Functions Safely": =C2=AB A secure arrangement can be obtained by forcing the temporary = schema to be searched last. To do this, write pg_temp as the last entry = in search_path. =C2=BB If I do this: set search_path =3D 'pg_catalog, pg_temp'; show search_path; Then I see what I set=E2=80=94in that order. But if I set the = search_path to empty (and don't see pg_catalog or pg_temp with "show") = PG behaves as if they're still there. Not only as my f() and p() above = show. But even, say, "select count(*) from pg_class". Moreover, this is = allowed too: set search_path =3D 'pg_temp, pg_catalog, pg_temp'; show search_path; Now I see exactly what I set. It seems strange that this is allowed. How = does the implementation handle this when a to-be-resolved name exists = nowhere? Does it just crank on, repeatedly searching where it already = failed, right up to the bitter end? Here's another test whose outcome surprises me and seems to be at odds = with what you're saying and what the "Writing SECURITY DEFINER Functions = Safely" section says: select count(*) from pg_class; -- 399 create temporary table pg_class(k int); select count(*) from pg_class; -- 0 set search_path =3D 'pg_catalog, pg_temp'; select count(*) from pg_class; -- STILL 0 Why does the final "select" show that the temp table's name has still = captured the one in pg_catalog even though it's ahead in the path. Might I trouble you to explain more carefully=E2=80=94or to give me a = doc reference that will allow me to see why the equality function in my = example f() is found when the path for the function is empty? And why my = example procedure p() manages to create a temporary table while pg_temp = is not on the search_path? > The main problem is that by doing search_path manipulation on the = function you prevent inlining it into the main query. The "Writing SECURITY DEFINER Functions Safely" section explicitly = recommends that a subprogram includes a "set search_path" specification. = But, as I read it, you're saying that this advice is wrong (at least = when a function will be invoked in more than a bare "select" because it = prevents inlining. How should I resolve these two conflicting pieces of advice? --Apple-Mail=_D9F3BB35-6BC2-4DE4-8459-455A95A517C4 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8
david.g.johnston@gmail.com wrote:

unless you = are going to write: operator(pg_catalog.=3D) in your function the advice = to always use schema qualifications is not going to be taken = seriously... the correct search_path to set isn't "empty" but = "pg_catalog", "pg_temp".  While this does violate "DRY" = principles it is the solution you are looking for.
 
The advice always to use = fully qualified names is orthogonal to the advice to set an explicit = search_path in a subprogram's definition. Am I right that your point is = about what search path to set and has nothing to do with the possible = use of qualified names?

I do see that I can achieve my goal by = setting the search_path to pg_catalog, pg_temp in my functions rather = than to empty. So I can adopt that practice. But it's at odds with = an example in this section:

Writing SECURITY DEFINER Functions Safely
https://www.postgresql.org/docs/current/sql-createfunction.html= #id-1.9.3.67.10.2

The example sets a search path the does = *not* mention pg_catalog.

I hadn't come across this locution:

operator(pg_catalog.=3D)

But I found an account here:

4.2.5. Operator Invocations
https://www.postgresql.org/docs/current/sql-expressions.html#SQ= L-EXPRESSIONS-OPERATOR-CALLS

To be sure, I = tried this:

select 17 operator(pg_catalog.=3D) 17;

It ran without error and = produced TRUE.

I believe that you're saying = that because (as I presume) the "normal" definition of the =3D operator = is done by code in = the pg_catalog schema, pg_catalog must be on the = path for ordinary equality tests to work. (I also found random folks = saying this on StackExchange.) So I tried this:

create function f()
  returns boolean
  set search_path =3D ''
  security = definer
  language plpgsql
as $body$
begin
  return (2 + 5) =3D 7;
end;
$body$;
select f()::text;

It succeeded and said true. = I don't know how to explain this outcome. For good measure, I tried = this too:

create procedure p()
  set = search_path =3D ''
  security definer
  language plpgsql
as $body$
begin
  create temporary table x(k = int);
end;
$body$;
call = p();

It succeeded. And = the \d metacommand showed me that I now have a table pg_temp_3.x. Using = a different database, it ends up in "pg_temp_1. What's going on = here? Is "pg_temp" a kind of generic nickname for ANY "pg_temp_N"?

I hate to realize that = I'm failing to understand a fundamental principle.

It rather looks like the = name-res to pg_catalog and (some) pg_temp is hard-coded and doesn't rely = on the reigning search_path. Or, to put it another way, these two = schemas are inevitably at the end of the search_path no matter what you = set explicitly, and never mind that "show search_path" doesn't show them = unless you also put them on the path (again) explicitly.

I can't make sense of = this wording from "Writing SECURITY DEFINER Functions = Safely":

=C2=AB = A secure arrangement can be obtained by forcing the temporary schema to = be searched last. To do this, write pg_temp as the last entry = in search_path. =C2=BB

If I do this:

set search_path =3D 'pg_catalog, = pg_temp';
show search_path;

Then I see what I = set=E2=80=94in that order. But if I set the search_path to empty (and = don't see pg_catalog or pg_temp with "show") PG behaves as if = they're still there. Not only as my f() and p() above show. But even, = say, "select count(*) from pg_class". Moreover, this is allowed = too:

set search_path =3D 'pg_temp, = pg_catalog, pg_temp';
show search_path;

Now I = see exactly what I set. It seems strange that this is allowed. How does = the implementation handle this when a to-be-resolved name exists = nowhere? Does it just crank on, repeatedly searching where it already = failed, right up to the bitter end?

Here's another test whose outcome = surprises me and seems to be at odds with what you're saying and what = the "Writing SECURITY DEFINER Functions Safely" section = says:

select count(*) from pg_class; -- 399
create temporary = table pg_class(k int);
select count(*) from pg_class; -- 0
set search_path =3D= 'pg_catalog, pg_temp';
select count(*) from pg_class; -- STILL 0

Why does the = final "select" show that the temp table's name has still captured the = one in pg_catalog even though it's ahead in the path.

Might I trouble you to = explain more carefully=E2=80=94or to give me a doc reference that = will allow me to see why the equality function in my example f() is = found when the path for the function is empty? And why my example = procedure p() manages to create a temporary table while pg_temp is not = on the search_path?

The = main problem is that by doing search_path manipulation on the function = you prevent inlining it into the main query.

The = "Writing SECURITY DEFINER Functions Safely" section explicitly = recommends that a subprogram includes a "set search_path" specification. = But, as I read it, you're saying that this advice is wrong (at least = when a function will be invoked in more than a bare "select" because it = prevents inlining.

How should I resolve these two conflicting pieces of = advice?

= --Apple-Mail=_D9F3BB35-6BC2-4DE4-8459-455A95A517C4--