@@ -384,26 +384,29 @@ static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
384
384
static void make_viewdef (StringInfo buf , HeapTuple ruletup , TupleDesc rulettc ,
385
385
int prettyFlags , int wrapColumn );
386
386
static void get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
387
- TupleDesc resultDesc ,
387
+ TupleDesc resultDesc , bool colNamesVisible ,
388
388
int prettyFlags , int wrapColumn , int startIndent );
389
389
static void get_values_def (List * values_lists , deparse_context * context );
390
390
static void get_with_clause (Query * query , deparse_context * context );
391
391
static void get_select_query_def (Query * query , deparse_context * context ,
392
- TupleDesc resultDesc );
393
- static void get_insert_query_def (Query * query , deparse_context * context );
394
- static void get_update_query_def (Query * query , deparse_context * context );
392
+ TupleDesc resultDesc , bool colNamesVisible );
393
+ static void get_insert_query_def (Query * query , deparse_context * context ,
394
+ bool colNamesVisible );
395
+ static void get_update_query_def (Query * query , deparse_context * context ,
396
+ bool colNamesVisible );
395
397
static void get_update_query_targetlist_def (Query * query , List * targetList ,
396
398
deparse_context * context ,
397
399
RangeTblEntry * rte );
398
- static void get_delete_query_def (Query * query , deparse_context * context );
400
+ static void get_delete_query_def (Query * query , deparse_context * context ,
401
+ bool colNamesVisible );
399
402
static void get_utility_query_def (Query * query , deparse_context * context );
400
403
static void get_basic_select_query (Query * query , deparse_context * context ,
401
- TupleDesc resultDesc );
404
+ TupleDesc resultDesc , bool colNamesVisible );
402
405
static void get_target_list (List * targetList , deparse_context * context ,
403
- TupleDesc resultDesc );
406
+ TupleDesc resultDesc , bool colNamesVisible );
404
407
static void get_setop_query (Node * setOp , Query * query ,
405
408
deparse_context * context ,
406
- TupleDesc resultDesc );
409
+ TupleDesc resultDesc , bool colNamesVisible );
407
410
static Node * get_rule_sortgroupclause (Index ref , List * tlist ,
408
411
bool force_colno ,
409
412
deparse_context * context );
@@ -4897,7 +4900,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4897
4900
foreach (action , actions )
4898
4901
{
4899
4902
query = (Query * ) lfirst (action );
4900
- get_query_def (query , buf , NIL , viewResultDesc ,
4903
+ get_query_def (query , buf , NIL , viewResultDesc , true,
4901
4904
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
4902
4905
if (prettyFlags )
4903
4906
appendStringInfoString (buf , ";\n" );
@@ -4915,7 +4918,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4915
4918
Query * query ;
4916
4919
4917
4920
query = (Query * ) linitial (actions );
4918
- get_query_def (query , buf , NIL , viewResultDesc ,
4921
+ get_query_def (query , buf , NIL , viewResultDesc , true,
4919
4922
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
4920
4923
appendStringInfoChar (buf , ';' );
4921
4924
}
@@ -4989,7 +4992,7 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
4989
4992
4990
4993
ev_relation = table_open (ev_class , AccessShareLock );
4991
4994
4992
- get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ),
4995
+ get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ), true,
4993
4996
prettyFlags , wrapColumn , 0 );
4994
4997
appendStringInfoChar (buf , ';' );
4995
4998
@@ -5000,13 +5003,23 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5000
5003
/* ----------
5001
5004
* get_query_def - Parse back one query parsetree
5002
5005
*
5003
- * If resultDesc is not NULL, then it is the output tuple descriptor for
5004
- * the view represented by a SELECT query.
5006
+ * query: parsetree to be displayed
5007
+ * buf: output text is appended to buf
5008
+ * parentnamespace: list (initially empty) of outer-level deparse_namespace's
5009
+ * resultDesc: if not NULL, the output tuple descriptor for the view
5010
+ * represented by a SELECT query. We use the column names from it
5011
+ * to label SELECT output columns, in preference to names in the query
5012
+ * colNamesVisible: true if the surrounding context cares about the output
5013
+ * column names at all (as, for example, an EXISTS() context does not);
5014
+ * when false, we can suppress dummy column labels such as "?column?"
5015
+ * prettyFlags: bitmask of PRETTYFLAG_XXX options
5016
+ * wrapColumn: maximum line length, or -1 to disable wrapping
5017
+ * startIndent: initial indentation amount
5005
5018
* ----------
5006
5019
*/
5007
5020
static void
5008
5021
get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
5009
- TupleDesc resultDesc ,
5022
+ TupleDesc resultDesc , bool colNamesVisible ,
5010
5023
int prettyFlags , int wrapColumn , int startIndent )
5011
5024
{
5012
5025
deparse_context context ;
@@ -5044,19 +5057,19 @@ get_query_def(Query *query, StringInfo buf, List *parentnamespace,
5044
5057
switch (query -> commandType )
5045
5058
{
5046
5059
case CMD_SELECT :
5047
- get_select_query_def (query , & context , resultDesc );
5060
+ get_select_query_def (query , & context , resultDesc , colNamesVisible );
5048
5061
break ;
5049
5062
5050
5063
case CMD_UPDATE :
5051
- get_update_query_def (query , & context );
5064
+ get_update_query_def (query , & context , colNamesVisible );
5052
5065
break ;
5053
5066
5054
5067
case CMD_INSERT :
5055
- get_insert_query_def (query , & context );
5068
+ get_insert_query_def (query , & context , colNamesVisible );
5056
5069
break ;
5057
5070
5058
5071
case CMD_DELETE :
5059
- get_delete_query_def (query , & context );
5072
+ get_delete_query_def (query , & context , colNamesVisible );
5060
5073
break ;
5061
5074
5062
5075
case CMD_NOTHING :
@@ -5180,6 +5193,7 @@ get_with_clause(Query *query, deparse_context *context)
5180
5193
if (PRETTY_INDENT (context ))
5181
5194
appendContextKeyword (context , "" , 0 , 0 , 0 );
5182
5195
get_query_def ((Query * ) cte -> ctequery , buf , context -> namespaces , NULL ,
5196
+ true,
5183
5197
context -> prettyFlags , context -> wrapColumn ,
5184
5198
context -> indentLevel );
5185
5199
if (PRETTY_INDENT (context ))
@@ -5203,7 +5217,7 @@ get_with_clause(Query *query, deparse_context *context)
5203
5217
*/
5204
5218
static void
5205
5219
get_select_query_def (Query * query , deparse_context * context ,
5206
- TupleDesc resultDesc )
5220
+ TupleDesc resultDesc , bool colNamesVisible )
5207
5221
{
5208
5222
StringInfo buf = context -> buf ;
5209
5223
List * save_windowclause ;
@@ -5227,13 +5241,14 @@ get_select_query_def(Query *query, deparse_context *context,
5227
5241
*/
5228
5242
if (query -> setOperations )
5229
5243
{
5230
- get_setop_query (query -> setOperations , query , context , resultDesc );
5244
+ get_setop_query (query -> setOperations , query , context , resultDesc ,
5245
+ colNamesVisible );
5231
5246
/* ORDER BY clauses must be simple in this case */
5232
5247
force_colno = true;
5233
5248
}
5234
5249
else
5235
5250
{
5236
- get_basic_select_query (query , context , resultDesc );
5251
+ get_basic_select_query (query , context , resultDesc , colNamesVisible );
5237
5252
force_colno = false;
5238
5253
}
5239
5254
@@ -5403,7 +5418,7 @@ get_simple_values_rte(Query *query, TupleDesc resultDesc)
5403
5418
5404
5419
static void
5405
5420
get_basic_select_query (Query * query , deparse_context * context ,
5406
- TupleDesc resultDesc )
5421
+ TupleDesc resultDesc , bool colNamesVisible )
5407
5422
{
5408
5423
StringInfo buf = context -> buf ;
5409
5424
RangeTblEntry * values_rte ;
@@ -5456,7 +5471,7 @@ get_basic_select_query(Query *query, deparse_context *context,
5456
5471
}
5457
5472
5458
5473
/* Then we tell what to select (the targetlist) */
5459
- get_target_list (query -> targetList , context , resultDesc );
5474
+ get_target_list (query -> targetList , context , resultDesc , colNamesVisible );
5460
5475
5461
5476
/* Add the FROM clause if needed */
5462
5477
get_from_clause (query , " FROM " , context );
@@ -5526,11 +5541,13 @@ get_basic_select_query(Query *query, deparse_context *context,
5526
5541
* get_target_list - Parse back a SELECT target list
5527
5542
*
5528
5543
* This is also used for RETURNING lists in INSERT/UPDATE/DELETE.
5544
+ *
5545
+ * resultDesc and colNamesVisible are as for get_query_def()
5529
5546
* ----------
5530
5547
*/
5531
5548
static void
5532
5549
get_target_list (List * targetList , deparse_context * context ,
5533
- TupleDesc resultDesc )
5550
+ TupleDesc resultDesc , bool colNamesVisible )
5534
5551
{
5535
5552
StringInfo buf = context -> buf ;
5536
5553
StringInfoData targetbuf ;
@@ -5581,8 +5598,13 @@ get_target_list(List *targetList, deparse_context *context,
5581
5598
else
5582
5599
{
5583
5600
get_rule_expr ((Node * ) tle -> expr , context , true);
5584
- /* We'll show the AS name unless it's this: */
5585
- attname = "?column?" ;
5601
+
5602
+ /*
5603
+ * When colNamesVisible is true, we should always show the
5604
+ * assigned column name explicitly. Otherwise, show it only if
5605
+ * it's not FigureColname's fallback.
5606
+ */
5607
+ attname = colNamesVisible ? NULL : "?column?" ;
5586
5608
}
5587
5609
5588
5610
/*
@@ -5661,7 +5683,7 @@ get_target_list(List *targetList, deparse_context *context,
5661
5683
5662
5684
static void
5663
5685
get_setop_query (Node * setOp , Query * query , deparse_context * context ,
5664
- TupleDesc resultDesc )
5686
+ TupleDesc resultDesc , bool colNamesVisible )
5665
5687
{
5666
5688
StringInfo buf = context -> buf ;
5667
5689
bool need_paren ;
@@ -5687,6 +5709,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5687
5709
if (need_paren )
5688
5710
appendStringInfoChar (buf , '(' );
5689
5711
get_query_def (subquery , buf , context -> namespaces , resultDesc ,
5712
+ colNamesVisible ,
5690
5713
context -> prettyFlags , context -> wrapColumn ,
5691
5714
context -> indentLevel );
5692
5715
if (need_paren )
@@ -5729,7 +5752,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5729
5752
else
5730
5753
subindent = 0 ;
5731
5754
5732
- get_setop_query (op -> larg , query , context , resultDesc );
5755
+ get_setop_query (op -> larg , query , context , resultDesc , colNamesVisible );
5733
5756
5734
5757
if (need_paren )
5735
5758
appendContextKeyword (context , ") " , - subindent , 0 , 0 );
@@ -5773,7 +5796,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
5773
5796
subindent = 0 ;
5774
5797
appendContextKeyword (context , "" , subindent , 0 , 0 );
5775
5798
5776
- get_setop_query (op -> rarg , query , context , resultDesc );
5799
+ get_setop_query (op -> rarg , query , context , resultDesc , false );
5777
5800
5778
5801
if (PRETTY_INDENT (context ))
5779
5802
context -> indentLevel -= subindent ;
@@ -6108,7 +6131,8 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
6108
6131
* ----------
6109
6132
*/
6110
6133
static void
6111
- get_insert_query_def (Query * query , deparse_context * context )
6134
+ get_insert_query_def (Query * query , deparse_context * context ,
6135
+ bool colNamesVisible )
6112
6136
{
6113
6137
StringInfo buf = context -> buf ;
6114
6138
RangeTblEntry * select_rte = NULL ;
@@ -6218,6 +6242,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6218
6242
{
6219
6243
/* Add the SELECT */
6220
6244
get_query_def (select_rte -> subquery , buf , NIL , NULL ,
6245
+ false,
6221
6246
context -> prettyFlags , context -> wrapColumn ,
6222
6247
context -> indentLevel );
6223
6248
}
@@ -6311,7 +6336,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6311
6336
{
6312
6337
appendContextKeyword (context , " RETURNING" ,
6313
6338
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6314
- get_target_list (query -> returningList , context , NULL );
6339
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6315
6340
}
6316
6341
}
6317
6342
@@ -6321,7 +6346,8 @@ get_insert_query_def(Query *query, deparse_context *context)
6321
6346
* ----------
6322
6347
*/
6323
6348
static void
6324
- get_update_query_def (Query * query , deparse_context * context )
6349
+ get_update_query_def (Query * query , deparse_context * context ,
6350
+ bool colNamesVisible )
6325
6351
{
6326
6352
StringInfo buf = context -> buf ;
6327
6353
RangeTblEntry * rte ;
@@ -6366,7 +6392,7 @@ get_update_query_def(Query *query, deparse_context *context)
6366
6392
{
6367
6393
appendContextKeyword (context , " RETURNING" ,
6368
6394
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6369
- get_target_list (query -> returningList , context , NULL );
6395
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6370
6396
}
6371
6397
}
6372
6398
@@ -6528,7 +6554,8 @@ get_update_query_targetlist_def(Query *query, List *targetList,
6528
6554
* ----------
6529
6555
*/
6530
6556
static void
6531
- get_delete_query_def (Query * query , deparse_context * context )
6557
+ get_delete_query_def (Query * query , deparse_context * context ,
6558
+ bool colNamesVisible )
6532
6559
{
6533
6560
StringInfo buf = context -> buf ;
6534
6561
RangeTblEntry * rte ;
@@ -6569,7 +6596,7 @@ get_delete_query_def(Query *query, deparse_context *context)
6569
6596
{
6570
6597
appendContextKeyword (context , " RETURNING" ,
6571
6598
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6572
- get_target_list (query -> returningList , context , NULL );
6599
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6573
6600
}
6574
6601
}
6575
6602
@@ -9896,7 +9923,7 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
9896
9923
if (need_paren )
9897
9924
appendStringInfoChar (buf , '(' );
9898
9925
9899
- get_query_def (query , buf , context -> namespaces , NULL ,
9926
+ get_query_def (query , buf , context -> namespaces , NULL , false,
9900
9927
context -> prettyFlags , context -> wrapColumn ,
9901
9928
context -> indentLevel );
9902
9929
@@ -10142,6 +10169,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
10142
10169
/* Subquery RTE */
10143
10170
appendStringInfoChar (buf , '(' );
10144
10171
get_query_def (rte -> subquery , buf , context -> namespaces , NULL ,
10172
+ true,
10145
10173
context -> prettyFlags , context -> wrapColumn ,
10146
10174
context -> indentLevel );
10147
10175
appendStringInfoChar (buf , ')' );
0 commit comments