Fwd: [BUGS] pg_dump search path issue

From: Elijah Zupancic <elijah(at)zupancic(dot)name>
To: pgsql-general(at)postgresql(dot)org
Subject: Fwd: [BUGS] pg_dump search path issue
Date: 2015-02-07 00:16:50
Message-ID: CALy1bpeMtBKC=YvD_vD8BWNvWdyuYQ9g_qoiP-+0rSwfcPwwhg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-general

I posted this to pgbugs a little while ago and I couldn't get much
traction. I'm hoping that someone in the general list may be able to
help me with this. Namely, this question:

For contrib functions - is there even a way for embedded queries in
functions to be auto-coded to the correct schema when you run CREATE
EXTENSION? I know the command (CREATE EXTENSION) takes a schema name,
but how does that get added to embedded queries? Is there a best
practice for this?

---------- Forwarded message ----------
From: Elijah Zupancic <elijah(at)zupancic(dot)name>
Date: Wed, Feb 4, 2015 at 6:01 PM
Subject: Re: [BUGS] pg_dump search path issue
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-bugs(at)postgresql(dot)org

Hi Tom,

Thanks for your reply. The functions in question are user defined
inasmuch as they are from the contrib package.

Here is a sample of one of the errors:

psql:./prod-db-2015-02-04.sql:1688: ERROR: function
cube_distance(public.earth, public.earth) does not exist
LINE 1: SELECT sec_to_gc(cube_distance($1, $2))
^
HINT: No function matches the given name and argument types. You
might need to add explicit type casts.
QUERY: SELECT sec_to_gc(cube_distance($1, $2))
CONTEXT: SQL function "earth_distance" during inlining
SELECT 0

This happens when pg_dump tries to recreate the definition of a
materialized view.

When I look at the dump, I see it sets a search path like: SET
search_path = aggregator, pg_catalog;

Then it goes a ways and creates a bunch of tables. Then it gets to the
materialized view. Upon closer inspection, the materialized view is
then calling the public.earth_distance function properly. However, the
earth distance function is calling an unqualified (missing the schema
specifier) function like so:

SELECT sec_to_gc(cube_distance($1, $2))

So, I'm with you - this is a problem with how the functions were
created. They should specify the schema so that they work correctly.
However, for contrib functions - is there even a way for them to be
auto-coded to the correct schema when you run CREATE EXTENSION? I know
the command takes a schema name, but how does that get added to
embedded queries? If there, is a best practice for this, I can take a
stab at patching earthdistance.

Thanks,
Elijah Zupancic

On Wed, Feb 4, 2015 at 5:32 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> Elijah Zupancic <elijah(at)zupancic(dot)name> writes:
>> In the SQL dump, you will notice that the SET search_path = xxx values
>> will often not include the public schema which holds the functions
>> needed to properly recreate tables that depend on extensions.
>
> All the cases I've seen of this involve user-defined functions that are
> broken, often dangerously so. A function should not assume that it's
> being called with any particular search_path; if it's intended for use in
> a multi-schema database, good practice is to either explicitly qualify
> names or use a SET clause to force the search_path to be what it expects.
>
>> It seems like the code that generates the SET search_path should check
>> to see if any of the objects it is dumping depend on functions that
>> use the public schema.
>
> If that didn't involve solving the halting problem, we might try to do
> it. But for better or worse, functions in Postgres are mostly black boxes
> so far as callers are concerned. It's not possible for pg_dump to know
> that some function has an expectation of being invoked with a particular
> search path.
>
> regards, tom lane

--
-Elijah

--
-Elijah

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message nad2000 2015-02-08 22:43:47 BUG #12746: Backward compatibility is broken 9.2: pg_stat_activity.procpid renamed to pid
Previous Message Tom Lane 2015-02-06 23:12:49 Re: BUG #12739: to_timestamp function conver string to time incorrectly

Browse pgsql-general by date

  From Date Subject
Next Message Guillaume Lelarge 2015-02-07 06:24:43 Re: Cluster seems broken after pg_basebackup
Previous Message Adrian Klaver 2015-02-06 23:44:47 Re: Cluster seems broken after pg_basebackup