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 1tUNV6-00FrEl-T0 for pgsql-general@arkaria.postgresql.org; Sun, 05 Jan 2025 10:04:53 +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 1tUNV4-00HN3N-M0 for pgsql-general@arkaria.postgresql.org; Sun, 05 Jan 2025 10:04:50 +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 1tUNV4-00HN3E-54 for pgsql-general@lists.postgresql.org; Sun, 05 Jan 2025 10:04:49 +0000 Received: from [209.85.218.45] (helo=mail-ej1-f45.google.com) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tUKTW-002AKI-1W for pgsql-general@lists.postgresql.org; Sun, 05 Jan 2025 06:53:12 +0000 Received: by mail-ej1-f45.google.com with SMTP id a640c23a62f3a-aa684b6d9c7so2228096166b.2 for ; Sat, 04 Jan 2025 22:50:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1736059737; x=1736664537; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=jcknIGR1XTsLfeXuw6tkXaa9Z+EzrdO0N8U3XHI+cZY=; b=F9fiyr389E+Bv0AHgbcbNi2i5Wv+84okfVfHhP1OXGZDj2yDPkP2UmsIWc/WOhKLWX DinJsNv27z1dgfDA5pHjLzYgImdXZ1YkNxs1fIyWO8DDs2g8QkR55Czd3t2XH99Pgn7I YP8Z/xeeGXzW57knYyxepTVayUkNZtXJcIBzDvhpvLu2OFa2YleY2pYLHE4nKmQic1HI GcQex8mP0zec8dIEXBbuURNRQ8omVjQNwoDMJDoJnV68XmXAaOJZ3TqWzKV2lDNtMaOB 8gae1b6VzSxX5o9ZgpOzA9xWu7W+6DdxQXe+pIzbnveep3BvgV9zpBEjz+txx0HS/cN5 9g9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736059737; x=1736664537; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=jcknIGR1XTsLfeXuw6tkXaa9Z+EzrdO0N8U3XHI+cZY=; b=hYJqWNYszH9lO2N0C5Rp9Q9KG3n+PcueHlmiTKOgejlu6Nh/Te9cS5dCdpd0NyJ8d5 GESQAuuej/5ioo+aHDRUlP/Gi2KBRfTJGmNPiQVDgwOy6lIT86+kUTv/Zg6bWrlTyCn8 M/T80+/o2ZKgYxycNgRKt4732cov388ISnFLtMs8hfhY9mrv4tsGobS0IUw5IHxacnlD R4N1llU5TPTUVEMhx9k340WidBbB0vRqVbGRiuBt6MTAMgFS0UzZ6wqm2ZxzYKY1oDrS GMhuBGGYJSnspRaw89TbLkmnriwQbsHk5juqqKo4y5kqAT4rBTo1YGC1ziEVi31Otuf3 EX8g== X-Forwarded-Encrypted: i=1; AJvYcCWFNlc+EqfYVkQ5uAcoIklMRZFWCijMga8+k7l+s92xR/L2rvvYmpPYtYyAqN4jv1ZFZ0Pa7BbOLCgX6oz0@lists.postgresql.org X-Gm-Message-State: AOJu0YxVpUgdHSEjUkkAw/h73nfVwnAKoQyhleZzr4lvBYJM473muHRS eihCda7/Z2mMUZKRwUfKWQetNmUKKjKPGsLQZnZ6ZUbKBjvuwRtKEQqxJVsDfzXhbO4gJMhNfQI S X-Gm-Gg: ASbGncsrFHUAEj5d1cgiRVX4G7fGKNSiyYNo0q3V7OWGYw0B6jNcez9IiUx7Ts3h59t /YXSD58vXV+P+p5jWdMcSIVpoAs2e/F3btlZKouIXFAosMUdrlp6ZLDowp6uDrZSZToyNKolDVW 0E3g2HR/G2jy9Cp+jJaxC192dvUWzmntFZr9h7THJndVEU3b1LGlUwWn7BwmSqo2iY+5S0RbuyB sB/YgUXpKDlsOpAWscBfh12eErHjfPNiQZ0hMe8ubxNlr/oVaY/EGiA1cpHfVe/l5+fcROO+9cg 4P0= X-Google-Smtp-Source: AGHT+IHrAaIl7JavmUNnUPpHad+DtNwU4A8YX9mZ4KfSvm5OIiIlClXngvxebOQTaGu/UD24biL9Ig== X-Received: by 2002:a17:907:7288:b0:aa6:a9fe:46dd with SMTP id a640c23a62f3a-aac346507b2mr3828891866b.38.1736059737293; Sat, 04 Jan 2025 22:48:57 -0800 (PST) Received: from localhost.localdomain ([2001:871:255:9ae3:244c:7859:2776:53b7]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5d80701bf20sm21469826a12.83.2025.01.04.22.48.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Jan 2025 22:48:56 -0800 (PST) Message-ID: <75510e9d2663573f1d32835eb6bcaf326e6a7997.camel@cybertec.at> Subject: Re: search_path for PL/pgSQL functions partially cached? From: Laurenz Albe To: Jan Behrens , Adrian Klaver , Tom Lane Cc: "David G. Johnston" , "pgsql-general@lists.postgresql.org" Date: Sun, 05 Jan 2025 07:48:56 +0100 In-Reply-To: <20250105001249.cdca9dd84a3061c06f936ca7@magnetkern.de> References: <20241227205025.1d059f72c7c08d23c9648c26@magnetkern.de> <20241228004009.267f21b78394c934f27f9974@magnetkern.de> <20250103135332.7910a008714abaa04d32e5e2@magnetkern.de> <20250103174849.f071d9c6942f76a9b812157c@magnetkern.de> <20250103223312.dde69ae482776e4f1b0c2258@magnetkern.de> <14e035fb-ab35-4a73-a637-ad0180067b07@aklaver.com> <20250104002203.2aed83d48d795ce6fbbd3b61@magnetkern.de> <20250105001249.cdca9dd84a3061c06f936ca7@magnetkern.de> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.54.2 (3.54.2-1.fc41) MIME-Version: 1.0 X-Host-Lookup-Failed: Reverse DNS lookup failed for 209.85.218.45 (deferred) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, 2025-01-05 at 00:12 +0100, Jan Behrens wrote: > I constructed the following new example: >=20 > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > CREATE TABLE "tbl" ("col" NUMERIC(15, 0)); >=20 > CREATE FUNCTION "foo"() RETURNS TEXT LANGUAGE plpgsql AS $$ > BEGIN > RETURN '2.4'; > END; > $$; >=20 > BEGIN; >=20 > CREATE SCHEMA "myschema"; > SET LOCAL search_path TO 'myschema'; >=20 > CREATE TABLE "tbl" ("col" NUMERIC); >=20 > CREATE FUNCTION "foo"() RETURNS TEXT LANGUAGE plpgsql AS $$ > BEGIN > RETURN '5.4'; > END; > $$; >=20 > CREATE FUNCTION "run"() RETURNS TEXT LANGUAGE plpgsql AS $$ > DECLARE > "old_search_path" TEXT; > BEGIN > "old_search_path" :=3D current_setting('search_path'); > SET LOCAL search_path TO "myschema"; > -- At this point, search_path is always set to 'myschema'! > DECLARE > "variable" "tbl"."col"%TYPE; > BEGIN > "variable" :=3D "foo"(); > RETURN "variable"; > END; > PERFORM set_config('search_path', "old_search_path", TRUE); > END; > $$; >=20 > COMMIT; >=20 > Even if >=20 > DECLARE "variable" "tbl"."col"%TYPE; >=20 > follows *after* the schema is set to "myschema" in the example above, I > still get differing results, depending on how the search_path was set > when the function was first called. So what you should do is set the "search_path" *on* the function, not *in* the function: CREATE FUNCTION "run"() RETURNS TEXT LANGUAGE plpgsql SET search_path =3D myschema AS $$ DECLARE "variable" "tbl"."col"%TYPE; BEGIN "variable" :=3D "foo"(); RETURN "variable"; END; $$; Yours, Laurenz Albe