pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Search path & functions in temporary schemas
2+ messages / 2 participants
[nested] [flat]

* Search path & functions in temporary schemas
@ 2018-12-11 16:04 jose luis pillado <josel.pillado@gmail.com>
  2018-12-11 16:27 ` Re: Search path & functions in temporary schemas Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: jose luis pillado @ 2018-12-11 16:04 UTC (permalink / raw)
  To: pgsql-general

Hi all,

I was trying to mock a function. So I followed the instructions in this
<https://www.postgresql.org/message-id/0E379B0D-EEFA-45FB-AC60-23F760B8D338%40justatheory.com;
 thread.

I created a function with the same name as the existing one in different
schema, and I updated the search path adding that schema at the beginning.

This solution worked with a real schema, but it did not with a temporary
one.

Code working with a real schema:

> SHOW SEARCH_PATH; -- public



CREATE OR REPLACE FUNCTION public.get_random_string()
> RETURNS TEXT LANGUAGE SQL AS $$
> SELECT 'real'::text;
> $$;

SELECT get_random_string(); -- real



CREATE SCHEMA mock;
> CREATE OR REPLACE FUNCTION mock.get_random_string()
> RETURNS TEXT LANGUAGE SQL AS $$
> SELECT 'mock'::text;
> $$;

SELECT get_random_string(); -- real



SET SEARCH_PATH = mock, public;
> SELECT get_random_string(); -- mock


Code not working with a temporary schema:

> SHOW SEARCH_PATH; -- public



CREATE OR REPLACE FUNCTION public.get_random_string()
> RETURNS TEXT LANGUAGE SQL AS $$
> SELECT 'real'::text;
> $$;
> SELECT get_random_string(); -- real



SELECT nspname FROM pg_namespace WHERE oid = pg_my_temp_schema(); --
> pg_temp_12



CREATE OR REPLACE FUNCTION pg_temp_12.get_random_string()
> RETURNS TEXT LANGUAGE SQL AS $$
> SELECT 'mock'::text;
> $$;
> SELECT get_random_string(); -- real



SET SEARCH_PATH = pg_temp_12, public;
> SELECT get_random_string(); -- real


Is there any way to make this work?

Thanks,
Jose

^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: Search path & functions in temporary schemas
  2018-12-11 16:04 Search path & functions in temporary schemas jose luis pillado <josel.pillado@gmail.com>
@ 2018-12-11 16:27 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2018-12-11 16:27 UTC (permalink / raw)
  To: jose luis pillado <josel.pillado@gmail.com>; +Cc: pgsql-general

jose luis pillado <josel.pillado@gmail.com> writes:
> This solution worked with a real schema, but it did not with a temporary
> one. ...
> Is there any way to make this work?

The temp schema is intentionally excluded from the search path for
functions and operators, because otherwise it's just too easy to
trojan-horse things.  If you really want to create and call a
temp function, you have to schema-qualify its name when you call it.

To make that a bit less messy, you can use "pg_temp" as an alias
for your session's temp schema, rather than having to find out which
numbered temp schema you're really using.

			regards, tom lane




^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2018-12-11 16:27 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-12-11 16:04 Search path & functions in temporary schemas jose luis pillado <josel.pillado@gmail.com>
2018-12-11 16:27 ` Tom Lane <tgl@sss.pgh.pa.us>

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