pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
pg_get_viewdef() drops casts, causing broken definitions
4+ messages / 3 participants
[nested] [flat]

* pg_get_viewdef() drops casts, causing broken definitions
@ 2016-05-14 02:44 Christophe Pettus <xof@thebuild.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Christophe Pettus @ 2016-05-14 02:44 UTC (permalink / raw)
  To: pgsql-bugs

I ran into this while doing a pgupgrade; it refused to re-import a view that was operating correctly on 9.2.

Summary: The view text returned by pg_get_viewdef() strips some casts, which can result in an error in trying to recreate the view.  This means you can't successfully recreate those views from a pg_dump.

Recreated:

xof=# drop table b;
DROP TABLE
xof=# CREATE TABLE a (i integer);
CREATE TABLE
xof=# INSERT INTO a SELECT generate_series(1,100);
INSERT 0 100
xof=# CREATE TABLE b (i integer);
CREATE TABLE
xof=# INSERT INTO b values(100);
INSERT 0 1
xof=# CREATE VIEW vb AS SELECT i FROM b WHERE i = ANY((SELECT array_agg(i) FROM a)::integer[]);
CREATE VIEW
xof=# SELECT * FROM vb;
  i  
-----
 100
(1 row)

xof=# select pg_get_viewdef('vb');
                      pg_get_viewdef                      
----------------------------------------------------------
  SELECT b.i                                             +
    FROM b                                               +
   WHERE (b.i = ANY (( SELECT array_agg(a.i) AS array_agg+
            FROM a)));
(1 row)

Note the missing cast.

It's missing from pg_dump as well, of course, since it uses that same function:

--
-- Name: vb; Type: VIEW; Schema: public; Owner: xof
--

CREATE VIEW vb AS
 SELECT b.i
   FROM b
  WHERE (b.i = ANY (( SELECT array_agg(a.i) AS array_agg
           FROM a)));


ALTER TABLE vb OWNER TO xof;

But recreating it from that definition results in an error:

xof=# drop view vb;
DROP VIEW
xof=# CREATE VIEW vb AS
xof-#  SELECT b.i
xof-#    FROM b
xof-#   WHERE (b.i = ANY (( SELECT array_agg(a.i) AS array_agg
xof(#            FROM a)));
ERROR:  operator does not exist: integer = integer[]
LINE 4:   WHERE (b.i = ANY (( SELECT array_agg(a.i) AS array_agg
                     ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

--
-- Christophe Pettus
   xof@thebuild.com



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* pg_get_viewdef() drops casts, causing broken definitions
@ 2016-05-14 02:50 Christophe Pettus <xof@thebuild.com>
  2016-05-14 03:01 ` Re: pg_get_viewdef() drops casts, causing broken definitions David G. Johnston <david.g.johnston@gmail.com>
  2016-05-14 03:01 ` Re: pg_get_viewdef() drops casts, causing broken definitions Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 2 replies; 4+ messages in thread

From: Christophe Pettus @ 2016-05-14 02:50 UTC (permalink / raw)
  To: pgsql-bugs

PostgreSQL 9.5.2.

I ran into this while doing a pgupgrade; it refused to re-import a view that was operating correctly on 9.2.

Summary: The view text returned by pg_get_viewdef() strips some casts, which can result in an error in trying to recreate the view.  This means you can't successfully recreate those views from a pg_dump.

Recreated:

xof=# drop table b;
DROP TABLE
xof=# CREATE TABLE a (i integer);
CREATE TABLE
xof=# INSERT INTO a SELECT generate_series(1,100);
INSERT 0 100
xof=# CREATE TABLE b (i integer);
CREATE TABLE
xof=# INSERT INTO b values(100);
INSERT 0 1
xof=# CREATE VIEW vb AS SELECT i FROM b WHERE i = ANY((SELECT array_agg(i) FROM a)::integer[]);
CREATE VIEW
xof=# SELECT * FROM vb;
 i  
-----
100
(1 row)

xof=# select pg_get_viewdef('vb');
                     pg_get_viewdef                      
----------------------------------------------------------
 SELECT b.i                                             +
   FROM b                                               +
  WHERE (b.i = ANY (( SELECT array_agg(a.i) AS array_agg+
           FROM a)));
(1 row)

Note the missing cast.

It's missing from pg_dump as well, of course, since it uses that same function:

--
-- Name: vb; Type: VIEW; Schema: public; Owner: xof
--

CREATE VIEW vb AS
SELECT b.i
  FROM b
 WHERE (b.i = ANY (( SELECT array_agg(a.i) AS array_agg
          FROM a)));


ALTER TABLE vb OWNER TO xof;

But recreating it from that definition results in an error:

xof=# drop view vb;
DROP VIEW
xof=# CREATE VIEW vb AS
xof-#  SELECT b.i
xof-#    FROM b
xof-#   WHERE (b.i = ANY (( SELECT array_agg(a.i) AS array_agg
xof(#            FROM a)));
ERROR:  operator does not exist: integer = integer[]
LINE 4:   WHERE (b.i = ANY (( SELECT array_agg(a.i) AS array_agg
                    ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

--
-- Christophe Pettus
  xof@thebuild.com



-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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

* Re: pg_get_viewdef() drops casts, causing broken definitions
  2016-05-14 02:50 pg_get_viewdef() drops casts, causing broken definitions Christophe Pettus <xof@thebuild.com>
@ 2016-05-14 03:01 ` David G. Johnston <david.g.johnston@gmail.com>
  1 sibling, 0 replies; 4+ messages in thread

From: David G. Johnston @ 2016-05-14 03:01 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: pgsql-bugs

On Fri, May 13, 2016 at 10:50 PM, Christophe Pettus <xof@thebuild.com>
wrote:

> PostgreSQL 9.5.2.
>
> I ran into this while doing a pgupgrade; it refused to re-import a view
> that was operating correctly on 9.2.
>
> Summary: The view text returned by pg_get_viewdef() strips some casts,
> which can result in an error in trying to recreate the view.  This means
> you can't successfully recreate those views from a pg_dump.
>

​Your example should work in 9.5.3; please upgrade and post back if it does
not.

David J.
​

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

* Re: pg_get_viewdef() drops casts, causing broken definitions
  2016-05-14 02:50 pg_get_viewdef() drops casts, causing broken definitions Christophe Pettus <xof@thebuild.com>
@ 2016-05-14 03:01 ` Tom Lane <tgl@sss.pgh.pa.us>
  1 sibling, 0 replies; 4+ messages in thread

From: Tom Lane @ 2016-05-14 03:01 UTC (permalink / raw)
  To: Christophe Pettus <xof@thebuild.com>; +Cc: pgsql-bugs

Christophe Pettus <xof@thebuild.com> writes:
> PostgreSQL 9.5.2.

> Summary: The view text returned by pg_get_viewdef() strips some casts, which can result in an error in trying to recreate the view.  This means you can't successfully recreate those views from a pg_dump.

Fixed as of 9.5.3.

http://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=1f7c85b820814810f985a270e92cde4c12...

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



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


end of thread, other threads:[~2016-05-14 03:01 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-05-14 02:44 pg_get_viewdef() drops casts, causing broken definitions Christophe Pettus <xof@thebuild.com>
2016-05-14 02:50 pg_get_viewdef() drops casts, causing broken definitions Christophe Pettus <xof@thebuild.com>
2016-05-14 03:01 ` David G. Johnston <david.g.johnston@gmail.com>
2016-05-14 03:01 ` 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