From 41de93c53aa941167d445ecb7d91d58829adcc92 Mon Sep 17 00:00:00 2001
From: Tom Lane <tgl@sss.pgh.pa.us>
Date: Wed, 30 Apr 2014 13:26:26 -0400
Subject: [PATCH] Reduce indentation/parenthesization of set operations in
 rule/view dumps.

A query such as "SELECT x UNION SELECT y UNION SELECT z UNION ..."
produces a left-deep nested parse tree, which we formerly showed in its
full nested glory and with all the possible parentheses.  This does little
for readability, though, and long UNION lists resulting in excessive
indentation are common.  Instead, let's omit parentheses and indent all
the subqueries at the same level in such cases.

This patch skips indentation/parenthesization whenever the lefthand input
of a SetOperationStmt is another SetOperationStmt of the same kind and
ALL/DISTINCT property.  We could teach the code the exact syntactic
precedence of set operations and thereby avoid parenthesization in some
more cases, but it's not clear that that'd be a readability win: it seems
better to parenthesize if the set operation changes.  (As an example,
if there's one UNION in a long list of UNION ALL, it now stands out like
a sore thumb, which seems like a good thing.)

Back-patch to 9.3.  This completes our response to a complaint from Greg
Stark that since commit 62e666400d there's a performance problem in pg_dump
for views containing long UNION sequences (or other types of deeply nested
constructs).  The previous commit 0601cb54dac14d979d726ab2ebeda251ae36e857
handles the general problem, but this one makes the specific case of UNION
lists look a lot nicer.
---
 src/backend/utils/adt/ruleutils.c         |  81 ++++--
 src/test/regress/expected/create_view.out | 274 ++++++++++----------
 src/test/regress/expected/matview.out     |  24 +-
 src/test/regress/expected/rules.out       | 290 +++++++++++-----------
 src/test/regress/expected/with.out        |  84 +++----
 5 files changed, 390 insertions(+), 363 deletions(-)

diff --git a/src/backend/utils/adt/ruleutils.c b/src/backend/utils/adt/ruleutils.c
index 5ed762cddb6..957f0fc7d9f 100644
--- a/src/backend/utils/adt/ruleutils.c
+++ b/src/backend/utils/adt/ruleutils.c
@@ -4714,42 +4714,59 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
 	else if (IsA(setOp, SetOperationStmt))
 	{
 		SetOperationStmt *op = (SetOperationStmt *) setOp;
-
-		if (PRETTY_INDENT(context))
-		{
-			context->indentLevel += PRETTYINDENT_STD;
-			appendStringInfoSpaces(buf, PRETTYINDENT_STD);
-		}
+		int			subindent;
 
 		/*
-		 * We force parens whenever nesting two SetOperationStmts. There are
-		 * some cases in which parens are needed around a leaf query too, but
-		 * those are more easily handled at the next level down (see code
-		 * above).
+		 * We force parens when nesting two SetOperationStmts, except when the
+		 * lefthand input is another setop of the same kind.  Syntactically,
+		 * we could omit parens in rather more cases, but it seems best to use
+		 * parens to flag cases where the setop operator changes.  If we use
+		 * parens, we also increase the indentation level for the child query.
+		 *
+		 * There are some cases in which parens are needed around a leaf query
+		 * too, but those are more easily handled at the next level down (see
+		 * code above).
 		 */
-		need_paren = !IsA(op->larg, RangeTblRef);
+		if (IsA(op->larg, SetOperationStmt))
+		{
+			SetOperationStmt *lop = (SetOperationStmt *) op->larg;
+
+			if (op->op == lop->op && op->all == lop->all)
+				need_paren = false;
+			else
+				need_paren = true;
+		}
+		else
+			need_paren = false;
 
 		if (need_paren)
+		{
 			appendStringInfoChar(buf, '(');
+			subindent = PRETTYINDENT_STD;
+			appendContextKeyword(context, "", subindent, 0, 0);
+		}
+		else
+			subindent = 0;
+
 		get_setop_query(op->larg, query, context, resultDesc);
-		if (need_paren)
-			appendStringInfoChar(buf, ')');
 
-		if (!PRETTY_INDENT(context))
+		if (need_paren)
+			appendContextKeyword(context, ") ", -subindent, 0, 0);
+		else if (PRETTY_INDENT(context))
+			appendContextKeyword(context, "", -subindent, 0, 0);
+		else
 			appendStringInfoChar(buf, ' ');
+
 		switch (op->op)
 		{
 			case SETOP_UNION:
-				appendContextKeyword(context, "UNION ",
-									 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
+				appendStringInfoString(buf, "UNION ");
 				break;
 			case SETOP_INTERSECT:
-				appendContextKeyword(context, "INTERSECT ",
-									 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
+				appendStringInfoString(buf, "INTERSECT ");
 				break;
 			case SETOP_EXCEPT:
-				appendContextKeyword(context, "EXCEPT ",
-									 -PRETTYINDENT_STD, PRETTYINDENT_STD, 0);
+				appendStringInfoString(buf, "EXCEPT ");
 				break;
 			default:
 				elog(ERROR, "unrecognized set op: %d",
@@ -4758,19 +4775,29 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
 		if (op->all)
 			appendStringInfoString(buf, "ALL ");
 
-		if (PRETTY_INDENT(context))
-			appendContextKeyword(context, "", 0, 0, 0);
-
-		need_paren = !IsA(op->rarg, RangeTblRef);
+		/* Always parenthesize if RHS is another setop */
+		need_paren = IsA(op->rarg, SetOperationStmt);
 
+		/*
+		 * The indentation code here is deliberately a bit different from that
+		 * for the lefthand input, because we want the line breaks in
+		 * different places.
+		 */
 		if (need_paren)
+		{
 			appendStringInfoChar(buf, '(');
+			subindent = PRETTYINDENT_STD;
+		}
+		else
+			subindent = 0;
+		appendContextKeyword(context, "", subindent, 0, 0);
+
 		get_setop_query(op->rarg, query, context, resultDesc);
-		if (need_paren)
-			appendStringInfoChar(buf, ')');
 
 		if (PRETTY_INDENT(context))
-			context->indentLevel -= PRETTYINDENT_STD;
+			context->indentLevel -= subindent;
+		if (need_paren)
+			appendContextKeyword(context, ")", 0, 0, 0);
 	}
 	else
 	{
diff --git a/src/test/regress/expected/create_view.out b/src/test/regress/expected/create_view.out
index d8a67d34b2f..1b95e90e020 100644
--- a/src/test/regress/expected/create_view.out
+++ b/src/test/regress/expected/create_view.out
@@ -1090,23 +1090,23 @@ select * from (values(1,2,3,4,5)) v(a,b,c,d,e)
 union all
 select * from tt7 full join tt8 using (x), tt8 tt8x;
 select pg_get_viewdef('vv2', true);
-                     pg_get_viewdef                     
---------------------------------------------------------
-          SELECT v.a,                                  +
-             v.b,                                      +
-             v.c,                                      +
-             v.d,                                      +
-             v.e                                       +
-            FROM ( VALUES (1,2,3,4,5)) v(a, b, c, d, e)+
- UNION ALL                                             +
-          SELECT x AS a,                               +
-             tt7.y AS b,                               +
-             tt8.z AS c,                               +
-             tt8x.x_1 AS d,                            +
-             tt8x.z AS e                               +
-            FROM tt7                                   +
-              FULL JOIN tt8 USING (x),                 +
-             tt8 tt8x(x_1, z);
+                 pg_get_viewdef                 
+------------------------------------------------
+  SELECT v.a,                                  +
+     v.b,                                      +
+     v.c,                                      +
+     v.d,                                      +
+     v.e                                       +
+    FROM ( VALUES (1,2,3,4,5)) v(a, b, c, d, e)+
+ UNION ALL                                     +
+  SELECT x AS a,                               +
+     tt7.y AS b,                               +
+     tt8.z AS c,                               +
+     tt8x.x_1 AS d,                            +
+     tt8x.z AS e                               +
+    FROM tt7                                   +
+      FULL JOIN tt8 USING (x),                 +
+     tt8 tt8x(x_1, z);
 (1 row)
 
 create view vv3 as
@@ -1116,26 +1116,26 @@ select * from
   tt7 full join tt8 using (x),
   tt7 tt7x full join tt8 tt8x using (x);
 select pg_get_viewdef('vv3', true);
-                       pg_get_viewdef                        
--------------------------------------------------------------
-          SELECT v.a,                                       +
-             v.b,                                           +
-             v.c,                                           +
-             v.x,                                           +
-             v.e,                                           +
-             v.f                                            +
-            FROM ( VALUES (1,2,3,4,5,6)) v(a, b, c, x, e, f)+
- UNION ALL                                                  +
-          SELECT x AS a,                                    +
-             tt7.y AS b,                                    +
-             tt8.z AS c,                                    +
-             x_1 AS x,                                      +
-             tt7x.y AS e,                                   +
-             tt8x.z AS f                                    +
-            FROM tt7                                        +
-              FULL JOIN tt8 USING (x),                      +
-             tt7 tt7x(x_1, y)                               +
-              FULL JOIN tt8 tt8x(x_1, z) USING (x_1);
+                   pg_get_viewdef                    
+-----------------------------------------------------
+  SELECT v.a,                                       +
+     v.b,                                           +
+     v.c,                                           +
+     v.x,                                           +
+     v.e,                                           +
+     v.f                                            +
+    FROM ( VALUES (1,2,3,4,5,6)) v(a, b, c, x, e, f)+
+ UNION ALL                                          +
+  SELECT x AS a,                                    +
+     tt7.y AS b,                                    +
+     tt8.z AS c,                                    +
+     x_1 AS x,                                      +
+     tt7x.y AS e,                                   +
+     tt8x.z AS f                                    +
+    FROM tt7                                        +
+      FULL JOIN tt8 USING (x),                      +
+     tt7 tt7x(x_1, y)                               +
+      FULL JOIN tt8 tt8x(x_1, z) USING (x_1);
 (1 row)
 
 create view vv4 as
@@ -1145,29 +1145,29 @@ select * from
   tt7 full join tt8 using (x),
   tt7 tt7x full join tt8 tt8x using (x) full join tt8 tt8y using (x);
 select pg_get_viewdef('vv4', true);
-                          pg_get_viewdef                          
-------------------------------------------------------------------
-          SELECT v.a,                                            +
-             v.b,                                                +
-             v.c,                                                +
-             v.x,                                                +
-             v.e,                                                +
-             v.f,                                                +
-             v.g                                                 +
-            FROM ( VALUES (1,2,3,4,5,6,7)) v(a, b, c, x, e, f, g)+
- UNION ALL                                                       +
-          SELECT x AS a,                                         +
-             tt7.y AS b,                                         +
-             tt8.z AS c,                                         +
-             x_1 AS x,                                           +
-             tt7x.y AS e,                                        +
-             tt8x.z AS f,                                        +
-             tt8y.z AS g                                         +
-            FROM tt7                                             +
-              FULL JOIN tt8 USING (x),                           +
-             tt7 tt7x(x_1, y)                                    +
-              FULL JOIN tt8 tt8x(x_1, z) USING (x_1)             +
-              FULL JOIN tt8 tt8y(x_1, z) USING (x_1);
+                      pg_get_viewdef                      
+----------------------------------------------------------
+  SELECT v.a,                                            +
+     v.b,                                                +
+     v.c,                                                +
+     v.x,                                                +
+     v.e,                                                +
+     v.f,                                                +
+     v.g                                                 +
+    FROM ( VALUES (1,2,3,4,5,6,7)) v(a, b, c, x, e, f, g)+
+ UNION ALL                                               +
+  SELECT x AS a,                                         +
+     tt7.y AS b,                                         +
+     tt8.z AS c,                                         +
+     x_1 AS x,                                           +
+     tt7x.y AS e,                                        +
+     tt8x.z AS f,                                        +
+     tt8y.z AS g                                         +
+    FROM tt7                                             +
+      FULL JOIN tt8 USING (x),                           +
+     tt7 tt7x(x_1, y)                                    +
+      FULL JOIN tt8 tt8x(x_1, z) USING (x_1)             +
+      FULL JOIN tt8 tt8y(x_1, z) USING (x_1);
 (1 row)
 
 alter table tt7 add column zz int;
@@ -1175,72 +1175,72 @@ alter table tt7 add column z int;
 alter table tt7 drop column zz;
 alter table tt8 add column z2 int;
 select pg_get_viewdef('vv2', true);
-                     pg_get_viewdef                     
---------------------------------------------------------
-          SELECT v.a,                                  +
-             v.b,                                      +
-             v.c,                                      +
-             v.d,                                      +
-             v.e                                       +
-            FROM ( VALUES (1,2,3,4,5)) v(a, b, c, d, e)+
- UNION ALL                                             +
-          SELECT x AS a,                               +
-             tt7.y AS b,                               +
-             tt8.z AS c,                               +
-             tt8x.x_1 AS d,                            +
-             tt8x.z AS e                               +
-            FROM tt7                                   +
-              FULL JOIN tt8 USING (x),                 +
-             tt8 tt8x(x_1, z, z2);
+                 pg_get_viewdef                 
+------------------------------------------------
+  SELECT v.a,                                  +
+     v.b,                                      +
+     v.c,                                      +
+     v.d,                                      +
+     v.e                                       +
+    FROM ( VALUES (1,2,3,4,5)) v(a, b, c, d, e)+
+ UNION ALL                                     +
+  SELECT x AS a,                               +
+     tt7.y AS b,                               +
+     tt8.z AS c,                               +
+     tt8x.x_1 AS d,                            +
+     tt8x.z AS e                               +
+    FROM tt7                                   +
+      FULL JOIN tt8 USING (x),                 +
+     tt8 tt8x(x_1, z, z2);
 (1 row)
 
 select pg_get_viewdef('vv3', true);
-                       pg_get_viewdef                        
--------------------------------------------------------------
-          SELECT v.a,                                       +
-             v.b,                                           +
-             v.c,                                           +
-             v.x,                                           +
-             v.e,                                           +
-             v.f                                            +
-            FROM ( VALUES (1,2,3,4,5,6)) v(a, b, c, x, e, f)+
- UNION ALL                                                  +
-          SELECT x AS a,                                    +
-             tt7.y AS b,                                    +
-             tt8.z AS c,                                    +
-             x_1 AS x,                                      +
-             tt7x.y AS e,                                   +
-             tt8x.z AS f                                    +
-            FROM tt7                                        +
-              FULL JOIN tt8 USING (x),                      +
-             tt7 tt7x(x_1, y, z)                            +
-              FULL JOIN tt8 tt8x(x_1, z, z2) USING (x_1);
+                   pg_get_viewdef                    
+-----------------------------------------------------
+  SELECT v.a,                                       +
+     v.b,                                           +
+     v.c,                                           +
+     v.x,                                           +
+     v.e,                                           +
+     v.f                                            +
+    FROM ( VALUES (1,2,3,4,5,6)) v(a, b, c, x, e, f)+
+ UNION ALL                                          +
+  SELECT x AS a,                                    +
+     tt7.y AS b,                                    +
+     tt8.z AS c,                                    +
+     x_1 AS x,                                      +
+     tt7x.y AS e,                                   +
+     tt8x.z AS f                                    +
+    FROM tt7                                        +
+      FULL JOIN tt8 USING (x),                      +
+     tt7 tt7x(x_1, y, z)                            +
+      FULL JOIN tt8 tt8x(x_1, z, z2) USING (x_1);
 (1 row)
 
 select pg_get_viewdef('vv4', true);
-                          pg_get_viewdef                          
-------------------------------------------------------------------
-          SELECT v.a,                                            +
-             v.b,                                                +
-             v.c,                                                +
-             v.x,                                                +
-             v.e,                                                +
-             v.f,                                                +
-             v.g                                                 +
-            FROM ( VALUES (1,2,3,4,5,6,7)) v(a, b, c, x, e, f, g)+
- UNION ALL                                                       +
-          SELECT x AS a,                                         +
-             tt7.y AS b,                                         +
-             tt8.z AS c,                                         +
-             x_1 AS x,                                           +
-             tt7x.y AS e,                                        +
-             tt8x.z AS f,                                        +
-             tt8y.z AS g                                         +
-            FROM tt7                                             +
-              FULL JOIN tt8 USING (x),                           +
-             tt7 tt7x(x_1, y, z)                                 +
-              FULL JOIN tt8 tt8x(x_1, z, z2) USING (x_1)         +
-              FULL JOIN tt8 tt8y(x_1, z, z2) USING (x_1);
+                      pg_get_viewdef                      
+----------------------------------------------------------
+  SELECT v.a,                                            +
+     v.b,                                                +
+     v.c,                                                +
+     v.x,                                                +
+     v.e,                                                +
+     v.f,                                                +
+     v.g                                                 +
+    FROM ( VALUES (1,2,3,4,5,6,7)) v(a, b, c, x, e, f, g)+
+ UNION ALL                                               +
+  SELECT x AS a,                                         +
+     tt7.y AS b,                                         +
+     tt8.z AS c,                                         +
+     x_1 AS x,                                           +
+     tt7x.y AS e,                                        +
+     tt8x.z AS f,                                        +
+     tt8y.z AS g                                         +
+    FROM tt7                                             +
+      FULL JOIN tt8 USING (x),                           +
+     tt7 tt7x(x_1, y, z)                                 +
+      FULL JOIN tt8 tt8x(x_1, z, z2) USING (x_1)         +
+      FULL JOIN tt8 tt8y(x_1, z, z2) USING (x_1);
 (1 row)
 
 -- Implicit coercions in a JOIN USING create issues similar to FULL JOIN
@@ -1252,23 +1252,23 @@ select * from (values(now(),2,3,now(),5)) v(a,b,c,d,e)
 union all
 select * from tt7a left join tt8a using (x), tt8a tt8ax;
 select pg_get_viewdef('vv2a', true);
-                         pg_get_viewdef                         
-----------------------------------------------------------------
-          SELECT v.a,                                          +
-             v.b,                                              +
-             v.c,                                              +
-             v.d,                                              +
-             v.e                                               +
-            FROM ( VALUES (now(),2,3,now(),5)) v(a, b, c, d, e)+
- UNION ALL                                                     +
-          SELECT x AS a,                                       +
-             tt7a.y AS b,                                      +
-             tt8a.z AS c,                                      +
-             tt8ax.x_1 AS d,                                   +
-             tt8ax.z AS e                                      +
-            FROM tt7a                                          +
-              LEFT JOIN tt8a USING (x),                        +
-             tt8a tt8ax(x_1, z);
+                     pg_get_viewdef                     
+--------------------------------------------------------
+  SELECT v.a,                                          +
+     v.b,                                              +
+     v.c,                                              +
+     v.d,                                              +
+     v.e                                               +
+    FROM ( VALUES (now(),2,3,now(),5)) v(a, b, c, d, e)+
+ UNION ALL                                             +
+  SELECT x AS a,                                       +
+     tt7a.y AS b,                                      +
+     tt8a.z AS c,                                      +
+     tt8ax.x_1 AS d,                                   +
+     tt8ax.z AS e                                      +
+    FROM tt7a                                          +
+      LEFT JOIN tt8a USING (x),                        +
+     tt8a tt8ax(x_1, z);
 (1 row)
 
 --
diff --git a/src/test/regress/expected/matview.out b/src/test/regress/expected/matview.out
index daf3b9e91d5..ddac97bea66 100644
--- a/src/test/regress/expected/matview.out
+++ b/src/test/regress/expected/matview.out
@@ -346,13 +346,13 @@ CREATE VIEW v_test2 AS SELECT moo, 2*moo FROM v_test1 UNION ALL SELECT moo, 3*mo
  moo      | integer |           | plain   | 
  ?column? | integer |           | plain   | 
 View definition:
-         SELECT v_test1.moo,
-            2 * v_test1.moo
-           FROM v_test1
+ SELECT v_test1.moo,
+    2 * v_test1.moo
+   FROM v_test1
 UNION ALL
-         SELECT v_test1.moo,
-            3 * v_test1.moo
-           FROM v_test1;
+ SELECT v_test1.moo,
+    3 * v_test1.moo
+   FROM v_test1;
 
 CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SELECT moo, 3*moo FROM v_test2;
 \d+ mv_test2
@@ -362,13 +362,13 @@ CREATE MATERIALIZED VIEW mv_test2 AS SELECT moo, 2*moo FROM v_test2 UNION ALL SE
  moo      | integer |           | plain   |              | 
  ?column? | integer |           | plain   |              | 
 View definition:
-         SELECT v_test2.moo,
-            2 * v_test2.moo
-           FROM v_test2
+ SELECT v_test2.moo,
+    2 * v_test2.moo
+   FROM v_test2
 UNION ALL
-         SELECT v_test2.moo,
-            3 * v_test2.moo
-           FROM v_test2;
+ SELECT v_test2.moo,
+    3 * v_test2.moo
+   FROM v_test2;
 
 CREATE MATERIALIZED VIEW mv_test3 AS SELECT * FROM mv_test2 WHERE moo = 12345;
 SELECT relispopulated FROM pg_class WHERE oid = 'mv_test3'::regclass;
diff --git a/src/test/regress/expected/rules.out b/src/test/regress/expected/rules.out
index b0750f97352..87870cf1af6 100644
--- a/src/test/regress/expected/rules.out
+++ b/src/test/regress/expected/rules.out
@@ -1401,111 +1401,111 @@ pg_rules| SELECT n.nspname AS schemaname,
      JOIN pg_class c ON ((c.oid = r.ev_class)))
      LEFT JOIN pg_namespace n ON ((n.oid = c.relnamespace)))
   WHERE (r.rulename <> '_RETURN'::name);
-pg_seclabels|        (        (        (        (        (        (        (        (        (         SELECT l.objoid,
-              l.classoid,
-              l.objsubid,
-            CASE
-             WHEN (rel.relkind = 'r'::"char") THEN 'table'::text
-             WHEN (rel.relkind = 'v'::"char") THEN 'view'::text
-             WHEN (rel.relkind = 'm'::"char") THEN 'materialized view'::text
-             WHEN (rel.relkind = 'S'::"char") THEN 'sequence'::text
-             WHEN (rel.relkind = 'f'::"char") THEN 'foreign table'::text
-             ELSE NULL::text
-            END AS objtype,
-              rel.relnamespace AS objnamespace,
-            CASE
-             WHEN pg_table_is_visible(rel.oid) THEN quote_ident((rel.relname)::text)
-             ELSE ((quote_ident((nsp.nspname)::text) || '.'::text) || quote_ident((rel.relname)::text))
-            END AS objname,
-              l.provider,
-              l.label
-             FROM ((pg_seclabel l
-               JOIN pg_class rel ON (((l.classoid = rel.tableoid) AND (l.objoid = rel.oid))))
-               JOIN pg_namespace nsp ON ((rel.relnamespace = nsp.oid)))
-            WHERE (l.objsubid = 0)
-        UNION ALL
-           SELECT l.objoid,
-              l.classoid,
-              l.objsubid,
-              'column'::text AS objtype,
-              rel.relnamespace AS objnamespace,
-              ((
-            CASE
-             WHEN pg_table_is_visible(rel.oid) THEN quote_ident((rel.relname)::text)
-             ELSE ((quote_ident((nsp.nspname)::text) || '.'::text) || quote_ident((rel.relname)::text))
-            END || '.'::text) || (att.attname)::text) AS objname,
-              l.provider,
-              l.label
-             FROM (((pg_seclabel l
-               JOIN pg_class rel ON (((l.classoid = rel.tableoid) AND (l.objoid = rel.oid))))
-               JOIN pg_attribute att ON (((rel.oid = att.attrelid) AND (l.objsubid = att.attnum))))
-               JOIN pg_namespace nsp ON ((rel.relnamespace = nsp.oid)))
-            WHERE (l.objsubid <> 0))
-      UNION ALL
-         SELECT l.objoid,
-            l.classoid,
-            l.objsubid,
-          CASE
-           WHEN (pro.proisagg = true) THEN 'aggregate'::text
-           WHEN (pro.proisagg = false) THEN 'function'::text
-           ELSE NULL::text
-          END AS objtype,
-            pro.pronamespace AS objnamespace,
-            (((
-          CASE
-           WHEN pg_function_is_visible(pro.oid) THEN quote_ident((pro.proname)::text)
-           ELSE ((quote_ident((nsp.nspname)::text) || '.'::text) || quote_ident((pro.proname)::text))
-          END || '('::text) || pg_get_function_arguments(pro.oid)) || ')'::text) AS objname,
-            l.provider,
-            l.label
-           FROM ((pg_seclabel l
-             JOIN pg_proc pro ON (((l.classoid = pro.tableoid) AND (l.objoid = pro.oid))))
-             JOIN pg_namespace nsp ON ((pro.pronamespace = nsp.oid)))
-          WHERE (l.objsubid = 0))
-    UNION ALL
-       SELECT l.objoid,
-          l.classoid,
-          l.objsubid,
+pg_seclabels| SELECT l.objoid,
+    l.classoid,
+    l.objsubid,
         CASE
-         WHEN (typ.typtype = 'd'::"char") THEN 'domain'::text
-         ELSE 'type'::text
+            WHEN (rel.relkind = 'r'::"char") THEN 'table'::text
+            WHEN (rel.relkind = 'v'::"char") THEN 'view'::text
+            WHEN (rel.relkind = 'm'::"char") THEN 'materialized view'::text
+            WHEN (rel.relkind = 'S'::"char") THEN 'sequence'::text
+            WHEN (rel.relkind = 'f'::"char") THEN 'foreign table'::text
+            ELSE NULL::text
         END AS objtype,
-          typ.typnamespace AS objnamespace,
+    rel.relnamespace AS objnamespace,
         CASE
-         WHEN pg_type_is_visible(typ.oid) THEN quote_ident((typ.typname)::text)
-         ELSE ((quote_ident((nsp.nspname)::text) || '.'::text) || quote_ident((typ.typname)::text))
+            WHEN pg_table_is_visible(rel.oid) THEN quote_ident((rel.relname)::text)
+            ELSE ((quote_ident((nsp.nspname)::text) || '.'::text) || quote_ident((rel.relname)::text))
         END AS objname,
-          l.provider,
-          l.label
-         FROM ((pg_seclabel l
-           JOIN pg_type typ ON (((l.classoid = typ.tableoid) AND (l.objoid = typ.oid))))
-           JOIN pg_namespace nsp ON ((typ.typnamespace = nsp.oid)))
-        WHERE (l.objsubid = 0))
-  UNION ALL
-     SELECT l.objoid,
-        l.classoid,
-        l.objsubid,
-        'large object'::text AS objtype,
-        NULL::oid AS objnamespace,
-        (l.objoid)::text AS objname,
-        l.provider,
-        l.label
-       FROM (pg_seclabel l
-         JOIN pg_largeobject_metadata lom ON ((l.objoid = lom.oid)))
-      WHERE ((l.classoid = ('pg_largeobject'::regclass)::oid) AND (l.objsubid = 0)))
+    l.provider,
+    l.label
+   FROM ((pg_seclabel l
+     JOIN pg_class rel ON (((l.classoid = rel.tableoid) AND (l.objoid = rel.oid))))
+     JOIN pg_namespace nsp ON ((rel.relnamespace = nsp.oid)))
+  WHERE (l.objsubid = 0)
+UNION ALL
+ SELECT l.objoid,
+    l.classoid,
+    l.objsubid,
+    'column'::text AS objtype,
+    rel.relnamespace AS objnamespace,
+    ((
+        CASE
+            WHEN pg_table_is_visible(rel.oid) THEN quote_ident((rel.relname)::text)
+            ELSE ((quote_ident((nsp.nspname)::text) || '.'::text) || quote_ident((rel.relname)::text))
+        END || '.'::text) || (att.attname)::text) AS objname,
+    l.provider,
+    l.label
+   FROM (((pg_seclabel l
+     JOIN pg_class rel ON (((l.classoid = rel.tableoid) AND (l.objoid = rel.oid))))
+     JOIN pg_attribute att ON (((rel.oid = att.attrelid) AND (l.objsubid = att.attnum))))
+     JOIN pg_namespace nsp ON ((rel.relnamespace = nsp.oid)))
+  WHERE (l.objsubid <> 0)
+UNION ALL
+ SELECT l.objoid,
+    l.classoid,
+    l.objsubid,
+        CASE
+            WHEN (pro.proisagg = true) THEN 'aggregate'::text
+            WHEN (pro.proisagg = false) THEN 'function'::text
+            ELSE NULL::text
+        END AS objtype,
+    pro.pronamespace AS objnamespace,
+    (((
+        CASE
+            WHEN pg_function_is_visible(pro.oid) THEN quote_ident((pro.proname)::text)
+            ELSE ((quote_ident((nsp.nspname)::text) || '.'::text) || quote_ident((pro.proname)::text))
+        END || '('::text) || pg_get_function_arguments(pro.oid)) || ')'::text) AS objname,
+    l.provider,
+    l.label
+   FROM ((pg_seclabel l
+     JOIN pg_proc pro ON (((l.classoid = pro.tableoid) AND (l.objoid = pro.oid))))
+     JOIN pg_namespace nsp ON ((pro.pronamespace = nsp.oid)))
+  WHERE (l.objsubid = 0)
+UNION ALL
+ SELECT l.objoid,
+    l.classoid,
+    l.objsubid,
+        CASE
+            WHEN (typ.typtype = 'd'::"char") THEN 'domain'::text
+            ELSE 'type'::text
+        END AS objtype,
+    typ.typnamespace AS objnamespace,
+        CASE
+            WHEN pg_type_is_visible(typ.oid) THEN quote_ident((typ.typname)::text)
+            ELSE ((quote_ident((nsp.nspname)::text) || '.'::text) || quote_ident((typ.typname)::text))
+        END AS objname,
+    l.provider,
+    l.label
+   FROM ((pg_seclabel l
+     JOIN pg_type typ ON (((l.classoid = typ.tableoid) AND (l.objoid = typ.oid))))
+     JOIN pg_namespace nsp ON ((typ.typnamespace = nsp.oid)))
+  WHERE (l.objsubid = 0)
+UNION ALL
+ SELECT l.objoid,
+    l.classoid,
+    l.objsubid,
+    'large object'::text AS objtype,
+    NULL::oid AS objnamespace,
+    (l.objoid)::text AS objname,
+    l.provider,
+    l.label
+   FROM (pg_seclabel l
+     JOIN pg_largeobject_metadata lom ON ((l.objoid = lom.oid)))
+  WHERE ((l.classoid = ('pg_largeobject'::regclass)::oid) AND (l.objsubid = 0))
+UNION ALL
+ SELECT l.objoid,
+    l.classoid,
+    l.objsubid,
+    'language'::text AS objtype,
+    NULL::oid AS objnamespace,
+    quote_ident((lan.lanname)::text) AS objname,
+    l.provider,
+    l.label
+   FROM (pg_seclabel l
+     JOIN pg_language lan ON (((l.classoid = lan.tableoid) AND (l.objoid = lan.oid))))
+  WHERE (l.objsubid = 0)
 UNION ALL
-   SELECT l.objoid,
-      l.classoid,
-      l.objsubid,
-      'language'::text AS objtype,
-      NULL::oid AS objnamespace,
-      quote_ident((lan.lanname)::text) AS objname,
-      l.provider,
-      l.label
-     FROM (pg_seclabel l
-       JOIN pg_language lan ON (((l.classoid = lan.tableoid) AND (l.objoid = lan.oid))))
-    WHERE (l.objsubid = 0))
-                                UNION ALL
  SELECT l.objoid,
     l.classoid,
     l.objsubid,
@@ -1516,52 +1516,52 @@ UNION ALL
     l.label
    FROM (pg_seclabel l
      JOIN pg_namespace nsp ON (((l.classoid = nsp.tableoid) AND (l.objoid = nsp.oid))))
-  WHERE (l.objsubid = 0))
-                        UNION ALL
-                                 SELECT l.objoid,
-                                    l.classoid,
-                                    l.objsubid,
-                                    'event trigger'::text AS objtype,
-                                    NULL::oid AS objnamespace,
-                                    quote_ident((evt.evtname)::text) AS objname,
-                                    l.provider,
-                                    l.label
-                                   FROM (pg_seclabel l
-                                     JOIN pg_event_trigger evt ON (((l.classoid = evt.tableoid) AND (l.objoid = evt.oid))))
-                                  WHERE (l.objsubid = 0))
-                UNION ALL
-                         SELECT l.objoid,
-                            l.classoid,
-                            0 AS objsubid,
-                            'database'::text AS objtype,
-                            NULL::oid AS objnamespace,
-                            quote_ident((dat.datname)::text) AS objname,
-                            l.provider,
-                            l.label
-                           FROM (pg_shseclabel l
-                             JOIN pg_database dat ON (((l.classoid = dat.tableoid) AND (l.objoid = dat.oid)))))
-        UNION ALL
-                 SELECT l.objoid,
-                    l.classoid,
-                    0 AS objsubid,
-                    'tablespace'::text AS objtype,
-                    NULL::oid AS objnamespace,
-                    quote_ident((spc.spcname)::text) AS objname,
-                    l.provider,
-                    l.label
-                   FROM (pg_shseclabel l
-                     JOIN pg_tablespace spc ON (((l.classoid = spc.tableoid) AND (l.objoid = spc.oid)))))
+  WHERE (l.objsubid = 0)
+UNION ALL
+ SELECT l.objoid,
+    l.classoid,
+    l.objsubid,
+    'event trigger'::text AS objtype,
+    NULL::oid AS objnamespace,
+    quote_ident((evt.evtname)::text) AS objname,
+    l.provider,
+    l.label
+   FROM (pg_seclabel l
+     JOIN pg_event_trigger evt ON (((l.classoid = evt.tableoid) AND (l.objoid = evt.oid))))
+  WHERE (l.objsubid = 0)
+UNION ALL
+ SELECT l.objoid,
+    l.classoid,
+    0 AS objsubid,
+    'database'::text AS objtype,
+    NULL::oid AS objnamespace,
+    quote_ident((dat.datname)::text) AS objname,
+    l.provider,
+    l.label
+   FROM (pg_shseclabel l
+     JOIN pg_database dat ON (((l.classoid = dat.tableoid) AND (l.objoid = dat.oid))))
+UNION ALL
+ SELECT l.objoid,
+    l.classoid,
+    0 AS objsubid,
+    'tablespace'::text AS objtype,
+    NULL::oid AS objnamespace,
+    quote_ident((spc.spcname)::text) AS objname,
+    l.provider,
+    l.label
+   FROM (pg_shseclabel l
+     JOIN pg_tablespace spc ON (((l.classoid = spc.tableoid) AND (l.objoid = spc.oid))))
 UNION ALL
-         SELECT l.objoid,
-            l.classoid,
-            0 AS objsubid,
-            'role'::text AS objtype,
-            NULL::oid AS objnamespace,
-            quote_ident((rol.rolname)::text) AS objname,
-            l.provider,
-            l.label
-           FROM (pg_shseclabel l
-             JOIN pg_authid rol ON (((l.classoid = rol.tableoid) AND (l.objoid = rol.oid))));
+ SELECT l.objoid,
+    l.classoid,
+    0 AS objsubid,
+    'role'::text AS objtype,
+    NULL::oid AS objnamespace,
+    quote_ident((rol.rolname)::text) AS objname,
+    l.provider,
+    l.label
+   FROM (pg_shseclabel l
+     JOIN pg_authid rol ON (((l.classoid = rol.tableoid) AND (l.objoid = rol.oid))));
 pg_settings| SELECT a.name,
     a.setting,
     a.unit,
diff --git a/src/test/regress/expected/with.out b/src/test/regress/expected/with.out
index d76ef4e03f6..06b372bf519 100644
--- a/src/test/regress/expected/with.out
+++ b/src/test/regress/expected/with.out
@@ -300,48 +300,48 @@ SELECT * FROM vsubdepartment ORDER BY name;
 
 -- Check reverse listing
 SELECT pg_get_viewdef('vsubdepartment'::regclass);
-                    pg_get_viewdef                     
--------------------------------------------------------
-  WITH RECURSIVE subdepartment AS (                   +
-                  SELECT department.id,               +
-                     department.parent_department,    +
-                     department.name                  +
-                    FROM department                   +
-                   WHERE (department.name = 'A'::text)+
-         UNION ALL                                    +
-                  SELECT d.id,                        +
-                     d.parent_department,             +
-                     d.name                           +
-                    FROM department d,                +
-                     subdepartment sd                 +
-                   WHERE (d.parent_department = sd.id)+
-         )                                            +
-  SELECT subdepartment.id,                            +
-     subdepartment.parent_department,                 +
-     subdepartment.name                               +
+                pg_get_viewdef                 
+-----------------------------------------------
+  WITH RECURSIVE subdepartment AS (           +
+          SELECT department.id,               +
+             department.parent_department,    +
+             department.name                  +
+            FROM department                   +
+           WHERE (department.name = 'A'::text)+
+         UNION ALL                            +
+          SELECT d.id,                        +
+             d.parent_department,             +
+             d.name                           +
+            FROM department d,                +
+             subdepartment sd                 +
+           WHERE (d.parent_department = sd.id)+
+         )                                    +
+  SELECT subdepartment.id,                    +
+     subdepartment.parent_department,         +
+     subdepartment.name                       +
     FROM subdepartment;
 (1 row)
 
 SELECT pg_get_viewdef('vsubdepartment'::regclass, true);
-                   pg_get_viewdef                    
------------------------------------------------------
-  WITH RECURSIVE subdepartment AS (                 +
-                  SELECT department.id,             +
-                     department.parent_department,  +
-                     department.name                +
-                    FROM department                 +
-                   WHERE department.name = 'A'::text+
-         UNION ALL                                  +
-                  SELECT d.id,                      +
-                     d.parent_department,           +
-                     d.name                         +
-                    FROM department d,              +
-                     subdepartment sd               +
-                   WHERE d.parent_department = sd.id+
-         )                                          +
-  SELECT subdepartment.id,                          +
-     subdepartment.parent_department,               +
-     subdepartment.name                             +
+               pg_get_viewdef                
+---------------------------------------------
+  WITH RECURSIVE subdepartment AS (         +
+          SELECT department.id,             +
+             department.parent_department,  +
+             department.name                +
+            FROM department                 +
+           WHERE department.name = 'A'::text+
+         UNION ALL                          +
+          SELECT d.id,                      +
+             d.parent_department,           +
+             d.name                         +
+            FROM department d,              +
+             subdepartment sd               +
+           WHERE d.parent_department = sd.id+
+         )                                  +
+  SELECT subdepartment.id,                  +
+     subdepartment.parent_department,       +
+     subdepartment.name                     +
     FROM subdepartment;
 (1 row)
 
@@ -360,11 +360,11 @@ SELECT sum(n) FROM t;
  sum    | bigint |           | plain   | 
 View definition:
  WITH RECURSIVE t(n) AS (
-                 VALUES (1)
+         VALUES (1)
         UNION ALL
-                 SELECT t_1.n + 1
-                   FROM t t_1
-                  WHERE t_1.n < 100
+         SELECT t_1.n + 1
+           FROM t t_1
+          WHERE t_1.n < 100
         )
  SELECT sum(t.n) AS sum
    FROM t;
-- 
GitLab