Re: Sometimes pg_dump generates dump which is not restorable

Lists: pgsql-hackers
From: "Dmitry Koterov" <dmitry(at)koterov(dot)ru>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Sometimes pg_dump generates dump which is not restorable
Date: 2008-11-13 16:23:33
Message-ID: d7df81620811130823o2d2160d0x7a912ad332487387@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Hello.

Why pg_dump dumps CONSTRAINT ... CHECK together with CREATE TABLE queries,
but NOT at the end of dump file (as FOREIGN KEY)?
Sometimes it causes the generation of invalid dumps which cannot be
restored. Details follow.

1. I use database-dedicated search_path:

ALTER DATABASE d SET search_path TO nsp, public, pg_catalog;

2. I have a CHECK on table1 which calls a stored function:

CREATE TABLE table1 (
i integer,
CONSTRAINT table1_chk CHECK ((a(i) = true))
);

3. The function a() calls any OTHER function b() from OTHER namespace (or
uses operators from other namespaces), but does not specify the schema name,
because it is in database search_path:

CREATE FUNCTION a(i integer) RETURNS boolean AS $$
BEGIN
PERFORM b(); -- b() is is from "nsp" schema
RETURN true;
END;$$ LANGUAGE plpgsql IMMUTABLE;

4. If I dump such schema using pg_dump, later this dump cannot be restored.
Look the following piece of generated dump:

SET search_path = public, pg_catalog;

COPY table1 (i) FROM stdin;
1
\.

You see, when COPY is executed, data is inserted, and CHECK is called. So,
function a() is called with "public, pg_catalog" search_path.
It is errorous!

Possible solutions:

1. When generating CREATE TABLE dump query, DO NOT include CONSTRAINT ...
CHECK clauses in it. Instead, use ALTER TABLE to add all checks AT THE END
of dump, the same as it is done for foreign keys. I have already offered
this above. Additionally, seems to me it will speed up the dump restoration.

2. Replace "SET search_path = public, pg_catalog" to "SET search_path =
public, pg_catalog, <all other database-dedicated search_pathes>". It's a
worse way, kind a hack.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: dmitry(at)koterov(dot)ru
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Sometimes pg_dump generates dump which is not restorable
Date: 2008-11-13 18:07:44
Message-ID: 19458.1226599664@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Dmitry Koterov" <dmitry(at)koterov(dot)ru> writes:
> 3. The function a() calls any OTHER function b() from OTHER namespace (or
> uses operators from other namespaces), but does not specify the schema name,
> because it is in database search_path:

> CREATE FUNCTION a(i integer) RETURNS boolean AS $$
> BEGIN
> PERFORM b(); -- b() is is from "nsp" schema
> RETURN true;
> END;$$ LANGUAGE plpgsql IMMUTABLE;

I think your function is broken. You might want to fix it by attaching
a local search_path setting to it.

regards, tom lane


From: "Dmitry Koterov" <dmitry(at)koterov(dot)ru>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Sometimes pg_dump generates dump which is not restorable
Date: 2008-11-14 14:34:02
Message-ID: d7df81620811140634k7695e17ag6bb52171ad5e4844@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Thank you for a possible solution.

But what about the database which exists and works correctly (and conforms
all the standards from the documentation), but dump+restore sequence is
failed for it? Does it mean that pg_dump should be improved to pass
dump+restore sequence?

Besides that, for pg_dump has corresponding behaviour CONSTRAINT = FOREIGN
KEY.
For CONSTRAINT = CHECK - it hasn't.

On Thu, Nov 13, 2008 at 9:07 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> "Dmitry Koterov" <dmitry(at)koterov(dot)ru> writes:
> > 3. The function a() calls any OTHER function b() from OTHER namespace (or
> > uses operators from other namespaces), but does not specify the schema
> name,
> > because it is in database search_path:
>
> > CREATE FUNCTION a(i integer) RETURNS boolean AS $$
> > BEGIN
> > PERFORM b(); -- b() is is from "nsp" schema
> > RETURN true;
> > END;$$ LANGUAGE plpgsql IMMUTABLE;
>
> I think your function is broken. You might want to fix it by attaching
> a local search_path setting to it.
>
> regards, tom lane
>
> --
> Sent via pgsql-hackers mailing list (pgsql-hackers(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-hackers
>


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: dmitry(at)koterov(dot)ru
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Sometimes pg_dump generates dump which is not restorable
Date: 2008-11-14 16:25:39
Message-ID: 27805.1226679939@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

"Dmitry Koterov" <dmitry(at)koterov(dot)ru> writes:
> Thank you for a possible solution.
> But what about the database which exists and works correctly (and conforms
> all the standards from the documentation), but dump+restore sequence is
> failed for it? Does it mean that pg_dump should be improved to pass
> dump+restore sequence?

No matter what pg_dump does, it can never guarantee that a non-immutable
check constraint will still pass at restore time ... and that's
basically what you've got, if the check function is
search-path-sensitive.

regards, tom lane


From: "Dmitry Koterov" <dmitry(at)koterov(dot)ru>
To: "Tom Lane" <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Sometimes pg_dump generates dump which is not restorable
Date: 2008-11-15 10:04:28
Message-ID: d7df81620811150204l23485fc3gc86269ee93092bf7@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-hackers

Oh, I understood you. Clearly, to surely avoid any side-effect in pg_dump,
all IMMUTABLE functions must implicitly assign search_path in develop time.
It's not obvious, so I propose to include this in CONSTRAINT ... CHECK and
CREATE INDEX documentation. :-) Or - raise NOTICE if an IMMUTABLE function
is used in CHECK or INDEX, but does not define search_path ints arguments.

Thanks!

On Fri, Nov 14, 2008 at 7:25 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

> "Dmitry Koterov" <dmitry(at)koterov(dot)ru> writes:
> > Thank you for a possible solution.
> > But what about the database which exists and works correctly (and
> conforms
> > all the standards from the documentation), but dump+restore sequence is
> > failed for it? Does it mean that pg_dump should be improved to pass
> > dump+restore sequence?
>
> No matter what pg_dump does, it can never guarantee that a non-immutable
> check constraint will still pass at restore time ... and that's
> basically what you've got, if the check function is
> search-path-sensitive.
>
> regards, tom lane
>