pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Markhof, Ingolf <ingolf.markhof@de.verizon.com>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: raf <raf@raf.org>
Cc: pgsql-generallists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: How to keep format of views source code as entered?
Date: Fri, 08 Jan 2021 10:41:13 -0500
Message-ID: <284500.1610120473@sss.pgh.pa.us> (raw)
In-Reply-To: <eb20f1477a0f442cb58678530b0e31fa@MS-FRA-E13EX02.intl.ad.vzwcorp.com>
References: <eb20f1477a0f442cb58678530b0e31fa@MS-FRA-E13EX02.intl.ad.vzwcorp.com>

"Markhof, Ingolf" <ingolf.markhof@de.verizon.com> writes:
> I am really surprised that PostgreSQL is unable to keep the source text
> of a view. Honestly, for me the looks like an implementation gap.

Perhaps, but the "gap" is wider than you seem to think.  Consider

CREATE TABLE t1 (f1 int, f2 text);
CREATE VIEW v1 AS SELECT f2 FROM t1;
ALTER TABLE t1 RENAME COLUMN f2 TO zed;
\d+ v1
                            View "public.v1"
 Column | Type | Collation | Nullable | Default | Storage  | Description 
--------+------+-----------+----------+---------+----------+-------------
 f2     | text |           |          |         | extended | 
View definition:
 SELECT t1.zed AS f2
   FROM t1;

At this point the original text of the view is useless; with
another rename or two it could become downright misleading.

Another issue revolves around the fact that a textual SQL statement
is seldom totally unambiguous.  In the above example, the fact that
"t1" refers to public.t1 and not some other t1 depends on the
search_path as it stood at CREATE VIEW time.  If you change your
search_path you might need an explicit schema qualification.
The reverse-parsed view display accounts for that:

# set search_path = pg_catalog;
# \d+ public.v1
                            View "public.v1"
 Column | Type | Collation | Nullable | Default | Storage  | Description 
--------+------+-----------+----------+---------+----------+-------------
 f2     | text |           |          |         | extended | 
View definition:
 SELECT t1.zed AS f2
   FROM public.t1;

but a static source text could not.  In PG this hazard applies to
functions and operators not only tables.  If pg_dump regurgitated
the original view text, there would be trivially-exploitable
security holes that allow some other user to take control of your
view after a dump/reload.

We actually used to store both text and parsed versions of some
sub-constructs, such as CHECK constraints and column default values.
We got rid of the text versions because there was no reasonable way
to keep them up-to-date.  (And, AFAIR, there hasn't been a lot of
push-back about those catalog columns disappearing.)  So I don't
think we'd accept a patch to store the text form of a view, unless
some solution to these issues were provided.

			regards, tom lane





view thread (31+ messages)  latest in thread

Message-ID: <284500.1610120473@sss.pgh.pa.us>
Permalink:  ../284500.1610120473@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/284500.1610120473@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, ingolf.markhof@de.verizon.com, david.g.johnston@gmail.com, raf@raf.org
  Subject: Re: How to keep format of views source code as entered?
  In-Reply-To: <284500.1610120473@sss.pgh.pa.us>
  References: <eb20f1477a0f442cb58678530b0e31fa@MS-FRA-E13EX02.intl.ad.vzwcorp.com> <284500.1610120473@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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