Re: [Dbdpg-general] Re: 'prepare' is not quite schema-safe

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: David Wheeler <david(at)kineticode(dot)com>
Cc: Neil Conway <neilc(at)samurai(dot)com>, pgsql-general(at)postgresql(dot)org, dbdpg-general(at)gborg(dot)postgresql(dot)org
Subject: Re: [Dbdpg-general] Re: 'prepare' is not quite schema-safe
Date: 2005-05-02 16:34:17
Message-ID: 29416.1115051657@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

David Wheeler <david(at)kineticode(dot)com> writes:
> On May 1, 2005, at 21:30 , Neil Conway wrote:
>> An alternative would be to flush dependent plans when the schema
>> search path is changed. In effect this would mean flushing *all*
>> prepared plans whenever the search path changes: we could perhaps
>> keep plans that only contain explicit namespace references, but
>> that seems fragile.

> Yes, but this would be invisible to DBD::Pg and other clients, no?

Depends what you call "invisible" --- Neil is suggesting automatic
replanning of already-prepared queries. To the extent that that
causes behavioral changes (like following a new search path) it
wouldn't be invisible to applications.

On the whole I think that the correct semantics of PREPARE is that
the objects referred to by the query are determined when the PREPARE
is executed, and don't change later on. Compare the following
example:

PREPARE foobar AS SELECT * FROM foo;

EXECUTE foobar;

ALTER TABLE foo RENAME TO bar;

EXECUTE foobar;

ALTER TABLE baz RENAME TO foo;

EXECUTE foobar;

Should the second EXECUTE fail entirely? Should the third select a
perhaps completely different set of columns from the formerly-named baz?
I don't think so. But this is exactly equivalent to the idea that
already-prepared statements should track later changes in search_path.

Here's an even nastier example:

SET search_path = s1, s2;

CREATE TABLE s2.foo ( ... );

PREPARE foobar AS SELECT * FROM foo;

EXECUTE foobar; -- shows contents of s2.foo

CREATE TABLE s1.foo ( ... );

EXECUTE foobar; -- shows contents of ??

I think you could demonstrate that if the spec is "make it look like the
original query was retyped as source each time", then *every* DDL change
in the database potentially requires invalidating every cached plan.
I don't find that a desirable spec.

regards, tom lane

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joshua D. Drake 2005-05-02 16:46:47 Re: Tuning queries inside a function
Previous Message Marco Colombo 2005-05-02 16:33:49 Re: Persistent Connections in Webserver Environment