pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Asier Lostalé <asier.lostale@openbravo.com>
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
Date: Tue, 20 Mar 2018 12:11:45 +0000
Message-ID: <87muz3f0xt.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CABtr+CJ5xKZbeoL5gv_KRUUXZU6tqKNxw7-J9TLARCwLk+WDjw@mail.gmail.com>
References: <152153826367.11956.8092048336300020216@wrigleys.postgresql.org>
	<87r2off4fc.fsf@news-spur.riddles.org.uk>
	<CABtr+CJ5xKZbeoL5gv_KRUUXZU6tqKNxw7-J9TLARCwLk+WDjw@mail.gmail.com>

>>>>> "Asier" == Asier Lostalé <asier.lostale@openbravo.com> writes:

 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 = 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 >= 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).

-- 
Andrew (irc:RhodiumToad)




view thread (5+ messages)  latest in thread

Message-ID: <87muz3f0xt.fsf@news-spur.riddles.org.uk>
Permalink:  ../87muz3f0xt.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87muz3f0xt.fsf@news-spur.riddles.org.uk

 · 

reply

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 from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: andrew@tao11.riddles.org.uk, asier.lostale@openbravo.com
  Subject: Re: BUG #15122: can't import data if table has a constraint with a function calling another function
  In-Reply-To: <87muz3f0xt.fsf@news-spur.riddles.org.uk>
  References: <152153826367.11956.8092048336300020216@wrigleys.postgresql.org> <87r2off4fc.fsf@news-spur.riddles.org.uk> <CABtr+CJ5xKZbeoL5gv_KRUUXZU6tqKNxw7-J9TLARCwLk+WDjw@mail.gmail.com> <87muz3f0xt.fsf@news-spur.riddles.org.uk>

* 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