Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kxttE-0000nv-Av for pgsql-general@arkaria.postgresql.org; Fri, 08 Jan 2021 15:41:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kxttC-0007b3-9K for pgsql-general@arkaria.postgresql.org; Fri, 08 Jan 2021 15:41:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kxttC-0007ZU-1H for pgsql-general@lists.postgresql.org; Fri, 08 Jan 2021 15:41:22 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kxtt8-0005C3-DR for pgsql-general@lists.postgresql.org; Fri, 08 Jan 2021 15:41:21 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 108FfDwg284501; Fri, 8 Jan 2021 10:41:13 -0500 From: Tom Lane To: "Markhof, Ingolf" cc: "David G. Johnston" , raf , "pgsql-generallists.postgresql.org" Subject: Re: How to keep format of views source code as entered? In-reply-to: References: Comments: In-reply-to "Markhof, Ingolf" message dated "Fri, 08 Jan 2021 08:38:29 +0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <284499.1610120473.1@sss.pgh.pa.us> Date: Fri, 08 Jan 2021 10:41:13 -0500 Message-ID: <284500.1610120473@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk "Markhof, Ingolf" 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