@@ -370,26 +370,29 @@ static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
370
370
static void make_viewdef (StringInfo buf , HeapTuple ruletup , TupleDesc rulettc ,
371
371
int prettyFlags , int wrapColumn );
372
372
static void get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
373
- TupleDesc resultDesc ,
373
+ TupleDesc resultDesc , bool colNamesVisible ,
374
374
int prettyFlags , int wrapColumn , int startIndent );
375
375
static void get_values_def (List * values_lists , deparse_context * context );
376
376
static void get_with_clause (Query * query , deparse_context * context );
377
377
static void get_select_query_def (Query * query , deparse_context * context ,
378
- TupleDesc resultDesc );
379
- static void get_insert_query_def (Query * query , deparse_context * context );
380
- static void get_update_query_def (Query * query , deparse_context * context );
378
+ TupleDesc resultDesc , bool colNamesVisible );
379
+ static void get_insert_query_def (Query * query , deparse_context * context ,
380
+ bool colNamesVisible );
381
+ static void get_update_query_def (Query * query , deparse_context * context ,
382
+ bool colNamesVisible );
381
383
static void get_update_query_targetlist_def (Query * query , List * targetList ,
382
384
deparse_context * context ,
383
385
RangeTblEntry * rte );
384
- static void get_delete_query_def (Query * query , deparse_context * context );
386
+ static void get_delete_query_def (Query * query , deparse_context * context ,
387
+ bool colNamesVisible );
385
388
static void get_utility_query_def (Query * query , deparse_context * context );
386
389
static void get_basic_select_query (Query * query , deparse_context * context ,
387
- TupleDesc resultDesc );
390
+ TupleDesc resultDesc , bool colNamesVisible );
388
391
static void get_target_list (List * targetList , deparse_context * context ,
389
- TupleDesc resultDesc );
392
+ TupleDesc resultDesc , bool colNamesVisible );
390
393
static void get_setop_query (Node * setOp , Query * query ,
391
394
deparse_context * context ,
392
- TupleDesc resultDesc );
395
+ TupleDesc resultDesc , bool colNamesVisible );
393
396
static Node * get_rule_sortgroupclause (Index ref , List * tlist ,
394
397
bool force_colno ,
395
398
deparse_context * context );
@@ -4788,7 +4791,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4788
4791
foreach (action , actions )
4789
4792
{
4790
4793
query = (Query * ) lfirst (action );
4791
- get_query_def (query , buf , NIL , viewResultDesc ,
4794
+ get_query_def (query , buf , NIL , viewResultDesc , true,
4792
4795
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
4793
4796
if (prettyFlags )
4794
4797
appendStringInfoString (buf , ";\n" );
@@ -4806,7 +4809,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4806
4809
Query * query ;
4807
4810
4808
4811
query = (Query * ) linitial (actions );
4809
- get_query_def (query , buf , NIL , viewResultDesc ,
4812
+ get_query_def (query , buf , NIL , viewResultDesc , true,
4810
4813
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
4811
4814
appendStringInfoChar (buf , ';' );
4812
4815
}
@@ -4880,7 +4883,7 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4880
4883
4881
4884
ev_relation = heap_open (ev_class , AccessShareLock );
4882
4885
4883
- get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ),
4886
+ get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ), true,
4884
4887
prettyFlags , wrapColumn , 0 );
4885
4888
appendStringInfoChar (buf , ';' );
4886
4889
@@ -4891,13 +4894,23 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4891
4894
/* ----------
4892
4895
* get_query_def - Parse back one query parsetree
4893
4896
*
4894
- * If resultDesc is not NULL, then it is the output tuple descriptor for
4895
- * the view represented by a SELECT query.
4897
+ * query: parsetree to be displayed
4898
+ * buf: output text is appended to buf
4899
+ * parentnamespace: list (initially empty) of outer-level deparse_namespace's
4900
+ * resultDesc: if not NULL, the output tuple descriptor for the view
4901
+ * represented by a SELECT query. We use the column names from it
4902
+ * to label SELECT output columns, in preference to names in the query
4903
+ * colNamesVisible: true if the surrounding context cares about the output
4904
+ * column names at all (as, for example, an EXISTS() context does not);
4905
+ * when false, we can suppress dummy column labels such as "?column?"
4906
+ * prettyFlags: bitmask of PRETTYFLAG_XXX options
4907
+ * wrapColumn: maximum line length, or -1 to disable wrapping
4908
+ * startIndent: initial indentation amount
4896
4909
* ----------
4897
4910
*/
4898
4911
static void
4899
4912
get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
4900
- TupleDesc resultDesc ,
4913
+ TupleDesc resultDesc , bool colNamesVisible ,
4901
4914
int prettyFlags , int wrapColumn , int startIndent )
4902
4915
{
4903
4916
deparse_context context ;
@@ -4934,19 +4947,19 @@ get_query_def(Query *query, StringInfo buf, List *parentnamespace,
4934
4947
switch (query -> commandType )
4935
4948
{
4936
4949
case CMD_SELECT :
4937
- get_select_query_def (query , & context , resultDesc );
4950
+ get_select_query_def (query , & context , resultDesc , colNamesVisible );
4938
4951
break ;
4939
4952
4940
4953
case CMD_UPDATE :
4941
- get_update_query_def (query , & context );
4954
+ get_update_query_def (query , & context , colNamesVisible );
4942
4955
break ;
4943
4956
4944
4957
case CMD_INSERT :
4945
- get_insert_query_def (query , & context );
4958
+ get_insert_query_def (query , & context , colNamesVisible );
4946
4959
break ;
4947
4960
4948
4961
case CMD_DELETE :
4949
- get_delete_query_def (query , & context );
4962
+ get_delete_query_def (query , & context , colNamesVisible );
4950
4963
break ;
4951
4964
4952
4965
case CMD_NOTHING :
@@ -5058,6 +5071,7 @@ get_with_clause(Query *query, deparse_context *context)
5058
5071
if (PRETTY_INDENT (context ))
5059
5072
appendContextKeyword (context , "" , 0 , 0 , 0 );
5060
5073
get_query_def ((Query * ) cte -> ctequery , buf , context -> namespaces , NULL ,
5074
+ true,
5061
5075
context -> prettyFlags , context -> wrapColumn ,
5062
5076
context -> indentLevel );
5063
5077
if (PRETTY_INDENT (context ))
@@ -5081,7 +5095,7 @@ get_with_clause(Query *query, deparse_context *context)
5081
5095
*/
5082
5096
static void
5083
5097
get_select_query_def (Query * query , deparse_context * context ,
5084
- TupleDesc resultDesc )
5098
+ TupleDesc resultDesc , bool colNamesVisible )
5085
5099
{
5086
5100
StringInfo buf = context -> buf ;
5087
5101
List * save_windowclause ;
@@ -5105,13 +5119,14 @@ get_select_query_def(Query *query, deparse_context *context,
5105
5119
*/
5106
5120
if (query -> setOperations )
5107
5121
{
5108
- get_setop_query (query -> setOperations , query , context , resultDesc );
5122
+ get_setop_query (query -> setOperations , query , context , resultDesc ,
5123
+ colNamesVisible );
5109
5124
/* ORDER BY clauses must be simple in this case */
5110
5125
force_colno = true;
5111
5126
}
5112
5127
else
5113
5128
{
5114
- get_basic_select_query (query , context , resultDesc );
5129
+ get_basic_select_query (query , context , resultDesc , colNamesVisible );
5115
5130
force_colno = false;
5116
5131
}
5117
5132
@@ -5268,7 +5283,7 @@ get_simple_values_rte(Query *query, TupleDesc resultDesc)
5268
5283
5269
5284
static void
5270
5285
get_basic_select_query (Query * query , deparse_context * context ,
5271
- TupleDesc resultDesc )
5286
+ TupleDesc resultDesc , bool colNamesVisible )
5272
5287
{
5273
5288
StringInfo buf = context -> buf ;
5274
5289
RangeTblEntry * values_rte ;
@@ -5321,7 +5336,7 @@ get_basic_select_query(Query *query, deparse_context *context,
5321
5336
}
5322
5337
5323
5338
/* Then we tell what to select (the targetlist) */
5324
- get_target_list (query -> targetList , context , resultDesc );
5339
+ get_target_list (query -> targetList , context , resultDesc , colNamesVisible );
5325
5340
5326
5341
/* Add the FROM clause if needed */
5327
5342
get_from_clause (query , " FROM " , context );
@@ -5391,11 +5406,13 @@ get_basic_select_query(Query *query, deparse_context *context,
5391
5406
* get_target_list - Parse back a SELECT target list
5392
5407
*
5393
5408
* This is also used for RETURNING lists in INSERT/UPDATE/DELETE.
5409
+ *
5410
+ * resultDesc and colNamesVisible are as for get_query_def()
5394
5411
* ----------
5395
5412
*/
5396
5413
static void
5397
5414
get_target_list (List * targetList , deparse_context * context ,
5398
- TupleDesc resultDesc )
5415
+ TupleDesc resultDesc , bool colNamesVisible )
5399
5416
{
5400
5417
StringInfo buf = context -> buf ;
5401
5418
StringInfoData targetbuf ;
@@ -5446,8 +5463,13 @@ get_target_list(List *targetList, deparse_context *context,
5446
5463
else
5447
5464
{
5448
5465
get_rule_expr ((Node * ) tle -> expr , context , true);
5449
- /* We'll show the AS name unless it's this: */
5450
- attname = "?column?" ;
5466
+
5467
+ /*
5468
+ * When colNamesVisible is true, we should always show the
5469
+ * assigned column name explicitly. Otherwise, show it only if
5470
+ * it's not FigureColname's fallback.
5471
+ */
5472
+ attname = colNamesVisible ? NULL : "?column?" ;
5451
5473
}
5452
5474
5453
5475
/*
@@ -5526,7 +5548,7 @@ get_target_list(List *targetList, deparse_context *context,
5526
5548
5527
5549
static void
5528
5550
get_setop_query (Node * setOp , Query * query , deparse_context * context ,
5529
- TupleDesc resultDesc )
5551
+ TupleDesc resultDesc , bool colNamesVisible )
5530
5552
{
5531
5553
StringInfo buf = context -> buf ;
5532
5554
bool need_paren ;
@@ -5552,6 +5574,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5552
5574
if (need_paren )
5553
5575
appendStringInfoChar (buf , '(' );
5554
5576
get_query_def (subquery , buf , context -> namespaces , resultDesc ,
5577
+ colNamesVisible ,
5555
5578
context -> prettyFlags , context -> wrapColumn ,
5556
5579
context -> indentLevel );
5557
5580
if (need_paren )
@@ -5594,7 +5617,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5594
5617
else
5595
5618
subindent = 0 ;
5596
5619
5597
- get_setop_query (op -> larg , query , context , resultDesc );
5620
+ get_setop_query (op -> larg , query , context , resultDesc , colNamesVisible );
5598
5621
5599
5622
if (need_paren )
5600
5623
appendContextKeyword (context , ") " , - subindent , 0 , 0 );
@@ -5638,7 +5661,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5638
5661
subindent = 0 ;
5639
5662
appendContextKeyword (context , "" , subindent , 0 , 0 );
5640
5663
5641
- get_setop_query (op -> rarg , query , context , resultDesc );
5664
+ get_setop_query (op -> rarg , query , context , resultDesc , false );
5642
5665
5643
5666
if (PRETTY_INDENT (context ))
5644
5667
context -> indentLevel -= subindent ;
@@ -5965,7 +5988,8 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
5965
5988
* ----------
5966
5989
*/
5967
5990
static void
5968
- get_insert_query_def (Query * query , deparse_context * context )
5991
+ get_insert_query_def (Query * query , deparse_context * context ,
5992
+ bool colNamesVisible )
5969
5993
{
5970
5994
StringInfo buf = context -> buf ;
5971
5995
RangeTblEntry * select_rte = NULL ;
@@ -6074,6 +6098,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6074
6098
{
6075
6099
/* Add the SELECT */
6076
6100
get_query_def (select_rte -> subquery , buf , NIL , NULL ,
6101
+ false,
6077
6102
context -> prettyFlags , context -> wrapColumn ,
6078
6103
context -> indentLevel );
6079
6104
}
@@ -6167,7 +6192,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6167
6192
{
6168
6193
appendContextKeyword (context , " RETURNING" ,
6169
6194
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6170
- get_target_list (query -> returningList , context , NULL );
6195
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6171
6196
}
6172
6197
}
6173
6198
@@ -6177,7 +6202,8 @@ get_insert_query_def(Query *query, deparse_context *context)
6177
6202
* ----------
6178
6203
*/
6179
6204
static void
6180
- get_update_query_def (Query * query , deparse_context * context )
6205
+ get_update_query_def (Query * query , deparse_context * context ,
6206
+ bool colNamesVisible )
6181
6207
{
6182
6208
StringInfo buf = context -> buf ;
6183
6209
RangeTblEntry * rte ;
@@ -6222,7 +6248,7 @@ get_update_query_def(Query *query, deparse_context *context)
6222
6248
{
6223
6249
appendContextKeyword (context , " RETURNING" ,
6224
6250
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6225
- get_target_list (query -> returningList , context , NULL );
6251
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6226
6252
}
6227
6253
}
6228
6254
@@ -6383,7 +6409,8 @@ get_update_query_targetlist_def(Query *query, List *targetList,
6383
6409
* ----------
6384
6410
*/
6385
6411
static void
6386
- get_delete_query_def (Query * query , deparse_context * context )
6412
+ get_delete_query_def (Query * query , deparse_context * context ,
6413
+ bool colNamesVisible )
6387
6414
{
6388
6415
StringInfo buf = context -> buf ;
6389
6416
RangeTblEntry * rte ;
@@ -6424,7 +6451,7 @@ get_delete_query_def(Query *query, deparse_context *context)
6424
6451
{
6425
6452
appendContextKeyword (context , " RETURNING" ,
6426
6453
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6427
- get_target_list (query -> returningList , context , NULL );
6454
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6428
6455
}
6429
6456
}
6430
6457
@@ -9624,7 +9651,7 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
9624
9651
if (need_paren )
9625
9652
appendStringInfoChar (buf , '(' );
9626
9653
9627
- get_query_def (query , buf , context -> namespaces , NULL ,
9654
+ get_query_def (query , buf , context -> namespaces , NULL , false,
9628
9655
context -> prettyFlags , context -> wrapColumn ,
9629
9656
context -> indentLevel );
9630
9657
@@ -9883,6 +9910,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
9883
9910
/* Subquery RTE */
9884
9911
appendStringInfoChar (buf , '(' );
9885
9912
get_query_def (rte -> subquery , buf , context -> namespaces , NULL ,
9913
+ true,
9886
9914
context -> prettyFlags , context -> wrapColumn ,
9887
9915
context -> indentLevel );
9888
9916
appendStringInfoChar (buf , ')' );
0 commit comments