@@ -390,26 +390,29 @@ static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
390
390
static void make_viewdef (StringInfo buf , HeapTuple ruletup , TupleDesc rulettc ,
391
391
int prettyFlags , int wrapColumn );
392
392
static void get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
393
- TupleDesc resultDesc ,
393
+ TupleDesc resultDesc , bool colNamesVisible ,
394
394
int prettyFlags , int wrapColumn , int startIndent );
395
395
static void get_values_def (List * values_lists , deparse_context * context );
396
396
static void get_with_clause (Query * query , deparse_context * context );
397
397
static void get_select_query_def (Query * query , deparse_context * context ,
398
- TupleDesc resultDesc );
399
- static void get_insert_query_def (Query * query , deparse_context * context );
400
- static void get_update_query_def (Query * query , deparse_context * context );
398
+ TupleDesc resultDesc , bool colNamesVisible );
399
+ static void get_insert_query_def (Query * query , deparse_context * context ,
400
+ bool colNamesVisible );
401
+ static void get_update_query_def (Query * query , deparse_context * context ,
402
+ bool colNamesVisible );
401
403
static void get_update_query_targetlist_def (Query * query , List * targetList ,
402
404
deparse_context * context ,
403
405
RangeTblEntry * rte );
404
- static void get_delete_query_def (Query * query , deparse_context * context );
406
+ static void get_delete_query_def (Query * query , deparse_context * context ,
407
+ bool colNamesVisible );
405
408
static void get_utility_query_def (Query * query , deparse_context * context );
406
409
static void get_basic_select_query (Query * query , deparse_context * context ,
407
- TupleDesc resultDesc );
410
+ TupleDesc resultDesc , bool colNamesVisible );
408
411
static void get_target_list (List * targetList , deparse_context * context ,
409
- TupleDesc resultDesc );
412
+ TupleDesc resultDesc , bool colNamesVisible );
410
413
static void get_setop_query (Node * setOp , Query * query ,
411
414
deparse_context * context ,
412
- TupleDesc resultDesc );
415
+ TupleDesc resultDesc , bool colNamesVisible );
413
416
static Node * get_rule_sortgroupclause (Index ref , List * tlist ,
414
417
bool force_colno ,
415
418
deparse_context * context );
@@ -3445,7 +3448,7 @@ print_function_sqlbody(StringInfo buf, HeapTuple proctup)
3445
3448
3446
3449
/* It seems advisable to get at least AccessShareLock on rels */
3447
3450
AcquireRewriteLocks (query , false, false);
3448
- get_query_def (query , buf , list_make1 (& dpns ), NULL ,
3451
+ get_query_def (query , buf , list_make1 (& dpns ), NULL , false,
3449
3452
PRETTYFLAG_INDENT , WRAP_COLUMN_DEFAULT , 1 );
3450
3453
appendStringInfoChar (buf , ';' );
3451
3454
appendStringInfoChar (buf , '\n' );
@@ -3459,7 +3462,7 @@ print_function_sqlbody(StringInfo buf, HeapTuple proctup)
3459
3462
3460
3463
/* It seems advisable to get at least AccessShareLock on rels */
3461
3464
AcquireRewriteLocks (query , false, false);
3462
- get_query_def (query , buf , list_make1 (& dpns ), NULL ,
3465
+ get_query_def (query , buf , list_make1 (& dpns ), NULL , false,
3463
3466
0 , WRAP_COLUMN_DEFAULT , 0 );
3464
3467
}
3465
3468
}
@@ -5189,7 +5192,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5189
5192
foreach (action , actions )
5190
5193
{
5191
5194
query = (Query * ) lfirst (action );
5192
- get_query_def (query , buf , NIL , viewResultDesc ,
5195
+ get_query_def (query , buf , NIL , viewResultDesc , true,
5193
5196
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
5194
5197
if (prettyFlags )
5195
5198
appendStringInfoString (buf , ";\n" );
@@ -5203,7 +5206,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5203
5206
Query * query ;
5204
5207
5205
5208
query = (Query * ) linitial (actions );
5206
- get_query_def (query , buf , NIL , viewResultDesc ,
5209
+ get_query_def (query , buf , NIL , viewResultDesc , true,
5207
5210
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
5208
5211
appendStringInfoChar (buf , ';' );
5209
5212
}
@@ -5277,7 +5280,7 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5277
5280
5278
5281
ev_relation = table_open (ev_class , AccessShareLock );
5279
5282
5280
- get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ),
5283
+ get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ), true,
5281
5284
prettyFlags , wrapColumn , 0 );
5282
5285
appendStringInfoChar (buf , ';' );
5283
5286
@@ -5288,13 +5291,23 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5288
5291
/* ----------
5289
5292
* get_query_def - Parse back one query parsetree
5290
5293
*
5291
- * If resultDesc is not NULL, then it is the output tuple descriptor for
5292
- * the view represented by a SELECT query.
5294
+ * query: parsetree to be displayed
5295
+ * buf: output text is appended to buf
5296
+ * parentnamespace: list (initially empty) of outer-level deparse_namespace's
5297
+ * resultDesc: if not NULL, the output tuple descriptor for the view
5298
+ * represented by a SELECT query. We use the column names from it
5299
+ * to label SELECT output columns, in preference to names in the query
5300
+ * colNamesVisible: true if the surrounding context cares about the output
5301
+ * column names at all (as, for example, an EXISTS() context does not);
5302
+ * when false, we can suppress dummy column labels such as "?column?"
5303
+ * prettyFlags: bitmask of PRETTYFLAG_XXX options
5304
+ * wrapColumn: maximum line length, or -1 to disable wrapping
5305
+ * startIndent: initial indentation amount
5293
5306
* ----------
5294
5307
*/
5295
5308
static void
5296
5309
get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
5297
- TupleDesc resultDesc ,
5310
+ TupleDesc resultDesc , bool colNamesVisible ,
5298
5311
int prettyFlags , int wrapColumn , int startIndent )
5299
5312
{
5300
5313
deparse_context context ;
@@ -5332,19 +5345,19 @@ get_query_def(Query *query, StringInfo buf, List *parentnamespace,
5332
5345
switch (query -> commandType )
5333
5346
{
5334
5347
case CMD_SELECT :
5335
- get_select_query_def (query , & context , resultDesc );
5348
+ get_select_query_def (query , & context , resultDesc , colNamesVisible );
5336
5349
break ;
5337
5350
5338
5351
case CMD_UPDATE :
5339
- get_update_query_def (query , & context );
5352
+ get_update_query_def (query , & context , colNamesVisible );
5340
5353
break ;
5341
5354
5342
5355
case CMD_INSERT :
5343
- get_insert_query_def (query , & context );
5356
+ get_insert_query_def (query , & context , colNamesVisible );
5344
5357
break ;
5345
5358
5346
5359
case CMD_DELETE :
5347
- get_delete_query_def (query , & context );
5360
+ get_delete_query_def (query , & context , colNamesVisible );
5348
5361
break ;
5349
5362
5350
5363
case CMD_NOTHING :
@@ -5468,6 +5481,7 @@ get_with_clause(Query *query, deparse_context *context)
5468
5481
if (PRETTY_INDENT (context ))
5469
5482
appendContextKeyword (context , "" , 0 , 0 , 0 );
5470
5483
get_query_def ((Query * ) cte -> ctequery , buf , context -> namespaces , NULL ,
5484
+ true,
5471
5485
context -> prettyFlags , context -> wrapColumn ,
5472
5486
context -> indentLevel );
5473
5487
if (PRETTY_INDENT (context ))
@@ -5549,7 +5563,7 @@ get_with_clause(Query *query, deparse_context *context)
5549
5563
*/
5550
5564
static void
5551
5565
get_select_query_def (Query * query , deparse_context * context ,
5552
- TupleDesc resultDesc )
5566
+ TupleDesc resultDesc , bool colNamesVisible )
5553
5567
{
5554
5568
StringInfo buf = context -> buf ;
5555
5569
List * save_windowclause ;
@@ -5573,13 +5587,14 @@ get_select_query_def(Query *query, deparse_context *context,
5573
5587
*/
5574
5588
if (query -> setOperations )
5575
5589
{
5576
- get_setop_query (query -> setOperations , query , context , resultDesc );
5590
+ get_setop_query (query -> setOperations , query , context , resultDesc ,
5591
+ colNamesVisible );
5577
5592
/* ORDER BY clauses must be simple in this case */
5578
5593
force_colno = true;
5579
5594
}
5580
5595
else
5581
5596
{
5582
- get_basic_select_query (query , context , resultDesc );
5597
+ get_basic_select_query (query , context , resultDesc , colNamesVisible );
5583
5598
force_colno = false;
5584
5599
}
5585
5600
@@ -5749,7 +5764,7 @@ get_simple_values_rte(Query *query, TupleDesc resultDesc)
5749
5764
5750
5765
static void
5751
5766
get_basic_select_query (Query * query , deparse_context * context ,
5752
- TupleDesc resultDesc )
5767
+ TupleDesc resultDesc , bool colNamesVisible )
5753
5768
{
5754
5769
StringInfo buf = context -> buf ;
5755
5770
RangeTblEntry * values_rte ;
@@ -5805,7 +5820,7 @@ get_basic_select_query(Query *query, deparse_context *context,
5805
5820
}
5806
5821
5807
5822
/* Then we tell what to select (the targetlist) */
5808
- get_target_list (query -> targetList , context , resultDesc );
5823
+ get_target_list (query -> targetList , context , resultDesc , colNamesVisible );
5809
5824
5810
5825
/* Add the FROM clause if needed */
5811
5826
get_from_clause (query , " FROM " , context );
@@ -5877,11 +5892,13 @@ get_basic_select_query(Query *query, deparse_context *context,
5877
5892
* get_target_list - Parse back a SELECT target list
5878
5893
*
5879
5894
* This is also used for RETURNING lists in INSERT/UPDATE/DELETE.
5895
+ *
5896
+ * resultDesc and colNamesVisible are as for get_query_def()
5880
5897
* ----------
5881
5898
*/
5882
5899
static void
5883
5900
get_target_list (List * targetList , deparse_context * context ,
5884
- TupleDesc resultDesc )
5901
+ TupleDesc resultDesc , bool colNamesVisible )
5885
5902
{
5886
5903
StringInfo buf = context -> buf ;
5887
5904
StringInfoData targetbuf ;
@@ -5932,8 +5949,13 @@ get_target_list(List *targetList, deparse_context *context,
5932
5949
else
5933
5950
{
5934
5951
get_rule_expr ((Node * ) tle -> expr , context , true);
5935
- /* We'll show the AS name unless it's this: */
5936
- attname = "?column?" ;
5952
+
5953
+ /*
5954
+ * When colNamesVisible is true, we should always show the
5955
+ * assigned column name explicitly. Otherwise, show it only if
5956
+ * it's not FigureColname's fallback.
5957
+ */
5958
+ attname = colNamesVisible ? NULL : "?column?" ;
5937
5959
}
5938
5960
5939
5961
/*
@@ -6012,7 +6034,7 @@ get_target_list(List *targetList, deparse_context *context,
6012
6034
6013
6035
static void
6014
6036
get_setop_query (Node * setOp , Query * query , deparse_context * context ,
6015
- TupleDesc resultDesc )
6037
+ TupleDesc resultDesc , bool colNamesVisible )
6016
6038
{
6017
6039
StringInfo buf = context -> buf ;
6018
6040
bool need_paren ;
@@ -6038,6 +6060,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
6038
6060
if (need_paren )
6039
6061
appendStringInfoChar (buf , '(' );
6040
6062
get_query_def (subquery , buf , context -> namespaces , resultDesc ,
6063
+ colNamesVisible ,
6041
6064
context -> prettyFlags , context -> wrapColumn ,
6042
6065
context -> indentLevel );
6043
6066
if (need_paren )
@@ -6080,7 +6103,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
6080
6103
else
6081
6104
subindent = 0 ;
6082
6105
6083
- get_setop_query (op -> larg , query , context , resultDesc );
6106
+ get_setop_query (op -> larg , query , context , resultDesc , colNamesVisible );
6084
6107
6085
6108
if (need_paren )
6086
6109
appendContextKeyword (context , ") " , - subindent , 0 , 0 );
@@ -6124,7 +6147,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
6124
6147
subindent = 0 ;
6125
6148
appendContextKeyword (context , "" , subindent , 0 , 0 );
6126
6149
6127
- get_setop_query (op -> rarg , query , context , resultDesc );
6150
+ get_setop_query (op -> rarg , query , context , resultDesc , false );
6128
6151
6129
6152
if (PRETTY_INDENT (context ))
6130
6153
context -> indentLevel -= subindent ;
@@ -6459,7 +6482,8 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
6459
6482
* ----------
6460
6483
*/
6461
6484
static void
6462
- get_insert_query_def (Query * query , deparse_context * context )
6485
+ get_insert_query_def (Query * query , deparse_context * context ,
6486
+ bool colNamesVisible )
6463
6487
{
6464
6488
StringInfo buf = context -> buf ;
6465
6489
RangeTblEntry * select_rte = NULL ;
@@ -6569,6 +6593,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6569
6593
{
6570
6594
/* Add the SELECT */
6571
6595
get_query_def (select_rte -> subquery , buf , context -> namespaces , NULL ,
6596
+ false,
6572
6597
context -> prettyFlags , context -> wrapColumn ,
6573
6598
context -> indentLevel );
6574
6599
}
@@ -6662,7 +6687,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6662
6687
{
6663
6688
appendContextKeyword (context , " RETURNING" ,
6664
6689
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6665
- get_target_list (query -> returningList , context , NULL );
6690
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6666
6691
}
6667
6692
}
6668
6693
@@ -6672,7 +6697,8 @@ get_insert_query_def(Query *query, deparse_context *context)
6672
6697
* ----------
6673
6698
*/
6674
6699
static void
6675
- get_update_query_def (Query * query , deparse_context * context )
6700
+ get_update_query_def (Query * query , deparse_context * context ,
6701
+ bool colNamesVisible )
6676
6702
{
6677
6703
StringInfo buf = context -> buf ;
6678
6704
RangeTblEntry * rte ;
@@ -6717,7 +6743,7 @@ get_update_query_def(Query *query, deparse_context *context)
6717
6743
{
6718
6744
appendContextKeyword (context , " RETURNING" ,
6719
6745
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6720
- get_target_list (query -> returningList , context , NULL );
6746
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6721
6747
}
6722
6748
}
6723
6749
@@ -6879,7 +6905,8 @@ get_update_query_targetlist_def(Query *query, List *targetList,
6879
6905
* ----------
6880
6906
*/
6881
6907
static void
6882
- get_delete_query_def (Query * query , deparse_context * context )
6908
+ get_delete_query_def (Query * query , deparse_context * context ,
6909
+ bool colNamesVisible )
6883
6910
{
6884
6911
StringInfo buf = context -> buf ;
6885
6912
RangeTblEntry * rte ;
@@ -6920,7 +6947,7 @@ get_delete_query_def(Query *query, deparse_context *context)
6920
6947
{
6921
6948
appendContextKeyword (context , " RETURNING" ,
6922
6949
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6923
- get_target_list (query -> returningList , context , NULL );
6950
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6924
6951
}
6925
6952
}
6926
6953
@@ -10532,7 +10559,7 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
10532
10559
if (need_paren )
10533
10560
appendStringInfoChar (buf , '(' );
10534
10561
10535
- get_query_def (query , buf , context -> namespaces , NULL ,
10562
+ get_query_def (query , buf , context -> namespaces , NULL , false,
10536
10563
context -> prettyFlags , context -> wrapColumn ,
10537
10564
context -> indentLevel );
10538
10565
@@ -10778,6 +10805,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
10778
10805
/* Subquery RTE */
10779
10806
appendStringInfoChar (buf , '(' );
10780
10807
get_query_def (rte -> subquery , buf , context -> namespaces , NULL ,
10808
+ true,
10781
10809
context -> prettyFlags , context -> wrapColumn ,
10782
10810
context -> indentLevel );
10783
10811
appendStringInfoChar (buf , ')' );
0 commit comments