pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #7553: Un-executable view definitions in pg_catalog.pg_views in versions 8.3.x-9.2.0
2+ messages / 2 participants
[nested] [flat]

* BUG #7553: Un-executable view definitions in pg_catalog.pg_views in versions 8.3.x-9.2.0
@ 2012-09-18 06:16 lalbin@fhcrc.org
  2012-09-18 16:41 ` Re: BUG #7553: Un-executable view definitions in pg_catalog.pg_views in versions 8.3.x-9.2.0 Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 2+ messages in thread

From: lalbin@fhcrc.org @ 2012-09-18 06:16 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      7553
Logged by:          Lloyd Albin
Email address:      lalbin@fhcrc.org
PostgreSQL version: 9.0.7
Operating system:   SUSE Linux (64-bit)
Description:        

I have run across a situation where we are changing schema and table names
that a view references. The view is still viewable with a select statement
but if you try and execute the view definition contained within the
pg_catalog.pg_views, then it fails. I have tested this on the following
versions:
9.3.9 Redhat 4.1.2 (64-bit)
9.3.20 Windows 7 (32-bit)
8.4.4 SUSE Linux (32-bit)
8.4.13 Windows 7 (32-bit)
9.0.7 SUSE Linux (64-bit)
9.0.9 Windows 7 (64-bit)
9.1.5 Windows 7 (64-bit)
9.2Beta1 Windows 7 (64-bit)
9.2.0 Windows 7 (64-bit)

I have been able to write this script to demo the failure.



-- Start test script
CREATE SCHEMA schema_a;
CREATE SCHEMA schema_b;
CREATE SCHEMA schema_c;

CREATE TABLE schema_a.table_a (
                id            varchar(11),
    field1 varchar(10)
);

CREATE TABLE schema_b.table_a (
                id            varchar(11),
    field1 varchar(10)
);

CREATE TABLE schema_b.table_b (
                id            varchar(11)
);

INSERT INTO schema_a.table_a VALUES ('test1', 'test2');
INSERT INTO schema_b.table_a VALUES ('test1', 'test3');
INSERT INTO schema_b.table_b VALUES ('test1');

CREATE OR REPLACE VIEW public.view_b AS 
SELECT 
                schema_a.table_a.field1,
                schema_b.table_a.field1 AS field2
FROM schema_b.table_b
LEFT JOIN schema_b.table_a
                ON schema_b.table_b.id = schema_b.table_a.id
LEFT JOIN schema_a.table_a
                ON schema_a.table_a.id = schema_b.table_b.id;

SELECT * FROM public.view_b;

CREATE OR REPLACE FUNCTION schema_c.function_a ()
RETURNS void AS $$
DECLARE
  def_row RECORD;
BEGIN
	SELECT definition INTO def_row FROM pg_catalog.pg_views WHERE viewname =
'view_b';
	EXECUTE def_row.definition;
END;
$$ LANGUAGE plpgsql;

SELECT schema_c.function_a();

ALTER TABLE schema_a.table_a RENAME TO table_d;

ALTER TABLE schema_a.table_d SET SCHEMA schema_c;

ALTER TABLE schema_b.table_a RENAME TO table_e;

ALTER TABLE schema_b.table_e SET SCHEMA schema_c;

ALTER TABLE schema_b.table_b RENAME TO table_f;

ALTER TABLE schema_b.table_f SET SCHEMA schema_c;

-- Use with Postgres 8.3
--ALTER TABLE public.view_b SET SCHEMA schema_c;
-- Use with Postgres 8.4+
ALTER VIEW public.view_b SET SCHEMA schema_c;

SELECT * FROM schema_c.view_b;

SELECT schema_c.function_a();
-- End test script



When executed you get the following error:

ERROR:  invalid reference to FROM-clause entry for table "table_a"
LINE 1: ...hema_c.table_e table_a ON (((table_b.id)::text = (schema_c.t...
                                                             ^
HINT:  There is an entry for table "table_a", but it cannot be referenced
from this part of the query.
QUERY:  SELECT schema_c.table_a.field1, schema_c.table_a.field1 AS field2
FROM ((schema_c.table_f table_b LEFT JOIN schema_c.table_e table_a ON
(((table_b.id)::text = (schema_c.table_a.id)::text))) LEFT JOIN
schema_c.table_d table_a ON (((schema_c.table_a.id)::text =
(table_b.id)::text)));
CONTEXT:  PL/pgSQL function schema_c.function_a() line 6 at EXECUTE
statement


Lloyd Albin
Statistical Center for HIV/AIDS Research and Prevention (SCHARP)
Vaccine and Infectious Disease Division (VIDD)
Fred Hutchinson Cancer Research Center (FHCRC)





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

* Re: BUG #7553: Un-executable view definitions in pg_catalog.pg_views in versions 8.3.x-9.2.0
  2012-09-18 06:16 BUG #7553: Un-executable view definitions in pg_catalog.pg_views in versions 8.3.x-9.2.0 lalbin@fhcrc.org
@ 2012-09-18 16:41 ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 2+ messages in thread

From: Tom Lane @ 2012-09-18 16:41 UTC (permalink / raw)
  To: lalbin@fhcrc.org; +Cc: pgsql-bugs

lalbin@fhcrc.org writes:
> I have run across a situation where we are changing schema and table names
> that a view references. The view is still viewable with a select statement
> but if you try and execute the view definition contained within the
> pg_catalog.pg_views, then it fails.

This appears to be a variant of the what-alias-to-use-after-a-rename
problem that we were discussing last winter:
http://archives.postgresql.org/message-id/29791.1327718297@sss.pgh.pa.us

That thread kinda died off after coming to the conclusion that there
isn't a simple, obviously correct solution.  The code's current behavior
is definitely wrong in the face of renames, but I don't want to change
it in a way that just moves the failure cases elsewhere (which is what
would happen for any of the proposals made in the thread :-().

			regards, tom lane




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


end of thread, other threads:[~2012-09-18 16:41 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-09-18 06:16 BUG #7553: Un-executable view definitions in pg_catalog.pg_views in versions 8.3.x-9.2.0 lalbin@fhcrc.org
2012-09-18 16:41 ` 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