Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyG7Q-0001SM-Cm for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Mar 2018 12:11:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eyG7O-0002wv-G3 for pgsql-bugs@arkaria.postgresql.org; Tue, 20 Mar 2018 12:11:54 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyG7O-0002wm-6l for pgsql-bugs@lists.postgresql.org; Tue, 20 Mar 2018 12:11:54 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eyG7K-00053x-To for pgsql-bugs@lists.postgresql.org; Tue, 20 Mar 2018 12:11:53 +0000 Received: from [192.168.127.1] (port=14814 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1eyG7H-0001TJ-Lc; Tue, 20 Mar 2018 12:11:47 +0000 Received: from [127.0.0.1] (port=59218 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1eyG7G-000ACK-QJ; Tue, 20 Mar 2018 12:11:46 +0000 From: Andrew Gierth To: Asier =?utf-8?Q?Lostal=C3=A9?= Cc: pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15122: can't import data if table has a constraint with a function calling another function In-Reply-To: ("Asier \=\?utf-8\?Q\?Lostal\=C3\=A9\=22's\?\= message of "Tue, 20 Mar 2018 11:24:05 +0100") Message-ID: <87muz3f0xt.fsf@news-spur.riddles.org.uk> References: <152153826367.11956.8092048336300020216@wrigleys.postgresql.org> <87r2off4fc.fsf@news-spur.riddles.org.uk> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Tue, 20 Mar 2018 12:11:45 +0000 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Asier" =3D=3D Asier Lostal=C3=A9 write= s: Asier> Thanks Andrew for your quick response and clear explanation. Asier> Can I understand from your explanation this is not considered as Asier> a bug? I would call it a misfeature rather than a bug. Asier> Although the adding a qualified reference workarounds the Asier> problem, it forces to write pl code that is aware of the schema Asier> it is going to be imported in. How could I write this code to be Asier> schema agnostic, so I can import it in any schema without Asier> modifying it? For plpgsql (and other pl/* languages, but not LANGUAGE SQL) the best way is probably to do this: SET search_path =3D public; -- or whatever schema CREATE OR REPLACE FUNCTION is_even_positive(integer) RETURNS boolean LANGUAGE plpgsql IMMUTABLE SET SEARCH_PATH FROM CURRENT -- ** this is the important bit AS $$ begin return is_even($1) and $1 >=3D 0; end; $$; Some caveats: 1) The default search_path is "$user",public. Using SET SEARCH_PATH FROM CURRENT doesn't interact well with this (arguably this part _is_ a bug), so either ensure that the search_path is set to something that doesn't exclude $user, or (if you need something that works in a script) you can canonicalize it first using this query: SELECT set_config('search_path', string_agg(quote_ident(s),','), false) -- change to true for equivalent of SET LOCAL FROM unnest(current_schemas(false)) s; 2) This doesn't work well for LANGUAGE SQL functions since it would block inlining, which is usually the primary reason for using LANGUAGE SQL in the first place. I don't know of any good workaround for those except to explicitly use the schema in the function body (possibly via text substitution). --=20 Andrew (irc:RhodiumToad)