@@ -395,26 +395,29 @@ static void make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
395
395
static void make_viewdef (StringInfo buf , HeapTuple ruletup , TupleDesc rulettc ,
396
396
int prettyFlags , int wrapColumn );
397
397
static void get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
398
- TupleDesc resultDesc ,
398
+ TupleDesc resultDesc , bool colNamesVisible ,
399
399
int prettyFlags , int wrapColumn , int startIndent );
400
400
static void get_values_def (List * values_lists , deparse_context * context );
401
401
static void get_with_clause (Query * query , deparse_context * context );
402
402
static void get_select_query_def (Query * query , deparse_context * context ,
403
- TupleDesc resultDesc );
404
- static void get_insert_query_def (Query * query , deparse_context * context );
405
- static void get_update_query_def (Query * query , deparse_context * context );
403
+ TupleDesc resultDesc , bool colNamesVisible );
404
+ static void get_insert_query_def (Query * query , deparse_context * context ,
405
+ bool colNamesVisible );
406
+ static void get_update_query_def (Query * query , deparse_context * context ,
407
+ bool colNamesVisible );
406
408
static void get_update_query_targetlist_def (Query * query , List * targetList ,
407
409
deparse_context * context ,
408
410
RangeTblEntry * rte );
409
- static void get_delete_query_def (Query * query , deparse_context * context );
411
+ static void get_delete_query_def (Query * query , deparse_context * context ,
412
+ bool colNamesVisible );
410
413
static void get_utility_query_def (Query * query , deparse_context * context );
411
414
static void get_basic_select_query (Query * query , deparse_context * context ,
412
- TupleDesc resultDesc );
415
+ TupleDesc resultDesc , bool colNamesVisible );
413
416
static void get_target_list (List * targetList , deparse_context * context ,
414
- TupleDesc resultDesc );
417
+ TupleDesc resultDesc , bool colNamesVisible );
415
418
static void get_setop_query (Node * setOp , Query * query ,
416
419
deparse_context * context ,
417
- TupleDesc resultDesc );
420
+ TupleDesc resultDesc , bool colNamesVisible );
418
421
static Node * get_rule_sortgroupclause (Index ref , List * tlist ,
419
422
bool force_colno ,
420
423
deparse_context * context );
@@ -1544,7 +1547,8 @@ pg_get_querydef(Query *query, bool pretty)
1544
1547
1545
1548
initStringInfo (& buf );
1546
1549
1547
- get_query_def (query , & buf , NIL , NULL , prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
1550
+ get_query_def (query , & buf , NIL , NULL , true,
1551
+ prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
1548
1552
1549
1553
return buf .data ;
1550
1554
}
@@ -3548,7 +3552,7 @@ print_function_sqlbody(StringInfo buf, HeapTuple proctup)
3548
3552
3549
3553
/* It seems advisable to get at least AccessShareLock on rels */
3550
3554
AcquireRewriteLocks (query , false, false);
3551
- get_query_def (query , buf , list_make1 (& dpns ), NULL ,
3555
+ get_query_def (query , buf , list_make1 (& dpns ), NULL , false,
3552
3556
PRETTYFLAG_INDENT , WRAP_COLUMN_DEFAULT , 1 );
3553
3557
appendStringInfoChar (buf , ';' );
3554
3558
appendStringInfoChar (buf , '\n' );
@@ -3562,7 +3566,7 @@ print_function_sqlbody(StringInfo buf, HeapTuple proctup)
3562
3566
3563
3567
/* It seems advisable to get at least AccessShareLock on rels */
3564
3568
AcquireRewriteLocks (query , false, false);
3565
- get_query_def (query , buf , list_make1 (& dpns ), NULL ,
3569
+ get_query_def (query , buf , list_make1 (& dpns ), NULL , false,
3566
3570
0 , WRAP_COLUMN_DEFAULT , 0 );
3567
3571
}
3568
3572
}
@@ -5299,7 +5303,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5299
5303
foreach (action , actions )
5300
5304
{
5301
5305
query = (Query * ) lfirst (action );
5302
- get_query_def (query , buf , NIL , viewResultDesc ,
5306
+ get_query_def (query , buf , NIL , viewResultDesc , true,
5303
5307
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
5304
5308
if (prettyFlags )
5305
5309
appendStringInfoString (buf , ";\n" );
@@ -5313,7 +5317,7 @@ make_ruledef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5313
5317
Query * query ;
5314
5318
5315
5319
query = (Query * ) linitial (actions );
5316
- get_query_def (query , buf , NIL , viewResultDesc ,
5320
+ get_query_def (query , buf , NIL , viewResultDesc , true,
5317
5321
prettyFlags , WRAP_COLUMN_DEFAULT , 0 );
5318
5322
appendStringInfoChar (buf , ';' );
5319
5323
}
@@ -5387,7 +5391,7 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5387
5391
5388
5392
ev_relation = table_open (ev_class , AccessShareLock );
5389
5393
5390
- get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ),
5394
+ get_query_def (query , buf , NIL , RelationGetDescr (ev_relation ), true,
5391
5395
prettyFlags , wrapColumn , 0 );
5392
5396
appendStringInfoChar (buf , ';' );
5393
5397
@@ -5398,13 +5402,23 @@ make_viewdef(StringInfo buf, HeapTuple ruletup, TupleDesc rulettc,
5398
5402
/* ----------
5399
5403
* get_query_def - Parse back one query parsetree
5400
5404
*
5401
- * If resultDesc is not NULL, then it is the output tuple descriptor for
5402
- * the view represented by a SELECT query.
5405
+ * query: parsetree to be displayed
5406
+ * buf: output text is appended to buf
5407
+ * parentnamespace: list (initially empty) of outer-level deparse_namespace's
5408
+ * resultDesc: if not NULL, the output tuple descriptor for the view
5409
+ * represented by a SELECT query. We use the column names from it
5410
+ * to label SELECT output columns, in preference to names in the query
5411
+ * colNamesVisible: true if the surrounding context cares about the output
5412
+ * column names at all (as, for example, an EXISTS() context does not);
5413
+ * when false, we can suppress dummy column labels such as "?column?"
5414
+ * prettyFlags: bitmask of PRETTYFLAG_XXX options
5415
+ * wrapColumn: maximum line length, or -1 to disable wrapping
5416
+ * startIndent: initial indentation amount
5403
5417
* ----------
5404
5418
*/
5405
5419
static void
5406
5420
get_query_def (Query * query , StringInfo buf , List * parentnamespace ,
5407
- TupleDesc resultDesc ,
5421
+ TupleDesc resultDesc , bool colNamesVisible ,
5408
5422
int prettyFlags , int wrapColumn , int startIndent )
5409
5423
{
5410
5424
deparse_context context ;
@@ -5442,19 +5456,19 @@ get_query_def(Query *query, StringInfo buf, List *parentnamespace,
5442
5456
switch (query -> commandType )
5443
5457
{
5444
5458
case CMD_SELECT :
5445
- get_select_query_def (query , & context , resultDesc );
5459
+ get_select_query_def (query , & context , resultDesc , colNamesVisible );
5446
5460
break ;
5447
5461
5448
5462
case CMD_UPDATE :
5449
- get_update_query_def (query , & context );
5463
+ get_update_query_def (query , & context , colNamesVisible );
5450
5464
break ;
5451
5465
5452
5466
case CMD_INSERT :
5453
- get_insert_query_def (query , & context );
5467
+ get_insert_query_def (query , & context , colNamesVisible );
5454
5468
break ;
5455
5469
5456
5470
case CMD_DELETE :
5457
- get_delete_query_def (query , & context );
5471
+ get_delete_query_def (query , & context , colNamesVisible );
5458
5472
break ;
5459
5473
5460
5474
case CMD_NOTHING :
@@ -5578,6 +5592,7 @@ get_with_clause(Query *query, deparse_context *context)
5578
5592
if (PRETTY_INDENT (context ))
5579
5593
appendContextKeyword (context , "" , 0 , 0 , 0 );
5580
5594
get_query_def ((Query * ) cte -> ctequery , buf , context -> namespaces , NULL ,
5595
+ true,
5581
5596
context -> prettyFlags , context -> wrapColumn ,
5582
5597
context -> indentLevel );
5583
5598
if (PRETTY_INDENT (context ))
@@ -5659,7 +5674,7 @@ get_with_clause(Query *query, deparse_context *context)
5659
5674
*/
5660
5675
static void
5661
5676
get_select_query_def (Query * query , deparse_context * context ,
5662
- TupleDesc resultDesc )
5677
+ TupleDesc resultDesc , bool colNamesVisible )
5663
5678
{
5664
5679
StringInfo buf = context -> buf ;
5665
5680
List * save_windowclause ;
@@ -5683,13 +5698,14 @@ get_select_query_def(Query *query, deparse_context *context,
5683
5698
*/
5684
5699
if (query -> setOperations )
5685
5700
{
5686
- get_setop_query (query -> setOperations , query , context , resultDesc );
5701
+ get_setop_query (query -> setOperations , query , context , resultDesc ,
5702
+ colNamesVisible );
5687
5703
/* ORDER BY clauses must be simple in this case */
5688
5704
force_colno = true;
5689
5705
}
5690
5706
else
5691
5707
{
5692
- get_basic_select_query (query , context , resultDesc );
5708
+ get_basic_select_query (query , context , resultDesc , colNamesVisible );
5693
5709
force_colno = false;
5694
5710
}
5695
5711
@@ -5859,7 +5875,7 @@ get_simple_values_rte(Query *query, TupleDesc resultDesc)
5859
5875
5860
5876
static void
5861
5877
get_basic_select_query (Query * query , deparse_context * context ,
5862
- TupleDesc resultDesc )
5878
+ TupleDesc resultDesc , bool colNamesVisible )
5863
5879
{
5864
5880
StringInfo buf = context -> buf ;
5865
5881
RangeTblEntry * values_rte ;
@@ -5915,7 +5931,7 @@ get_basic_select_query(Query *query, deparse_context *context,
5915
5931
}
5916
5932
5917
5933
/* Then we tell what to select (the targetlist) */
5918
- get_target_list (query -> targetList , context , resultDesc );
5934
+ get_target_list (query -> targetList , context , resultDesc , colNamesVisible );
5919
5935
5920
5936
/* Add the FROM clause if needed */
5921
5937
get_from_clause (query , " FROM " , context );
@@ -5987,11 +6003,13 @@ get_basic_select_query(Query *query, deparse_context *context,
5987
6003
* get_target_list - Parse back a SELECT target list
5988
6004
*
5989
6005
* This is also used for RETURNING lists in INSERT/UPDATE/DELETE.
6006
+ *
6007
+ * resultDesc and colNamesVisible are as for get_query_def()
5990
6008
* ----------
5991
6009
*/
5992
6010
static void
5993
6011
get_target_list (List * targetList , deparse_context * context ,
5994
- TupleDesc resultDesc )
6012
+ TupleDesc resultDesc , bool colNamesVisible )
5995
6013
{
5996
6014
StringInfo buf = context -> buf ;
5997
6015
StringInfoData targetbuf ;
@@ -6042,8 +6060,13 @@ get_target_list(List *targetList, deparse_context *context,
6042
6060
else
6043
6061
{
6044
6062
get_rule_expr ((Node * ) tle -> expr , context , true);
6045
- /* We'll show the AS name unless it's this: */
6046
- attname = "?column?" ;
6063
+
6064
+ /*
6065
+ * When colNamesVisible is true, we should always show the
6066
+ * assigned column name explicitly. Otherwise, show it only if
6067
+ * it's not FigureColname's fallback.
6068
+ */
6069
+ attname = colNamesVisible ? NULL : "?column?" ;
6047
6070
}
6048
6071
6049
6072
/*
@@ -6122,7 +6145,7 @@ get_target_list(List *targetList, deparse_context *context,
6122
6145
6123
6146
static void
6124
6147
get_setop_query (Node * setOp , Query * query , deparse_context * context ,
6125
- TupleDesc resultDesc )
6148
+ TupleDesc resultDesc , bool colNamesVisible )
6126
6149
{
6127
6150
StringInfo buf = context -> buf ;
6128
6151
bool need_paren ;
@@ -6148,6 +6171,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
6148
6171
if (need_paren )
6149
6172
appendStringInfoChar (buf , '(' );
6150
6173
get_query_def (subquery , buf , context -> namespaces , resultDesc ,
6174
+ colNamesVisible ,
6151
6175
context -> prettyFlags , context -> wrapColumn ,
6152
6176
context -> indentLevel );
6153
6177
if (need_paren )
@@ -6190,7 +6214,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
6190
6214
else
6191
6215
subindent = 0 ;
6192
6216
6193
- get_setop_query (op -> larg , query , context , resultDesc );
6217
+ get_setop_query (op -> larg , query , context , resultDesc , colNamesVisible );
6194
6218
6195
6219
if (need_paren )
6196
6220
appendContextKeyword (context , ") " , - subindent , 0 , 0 );
@@ -6234,7 +6258,7 @@ get_setop_query(Node *setOp, Query *query, deparse_context *context,
6234
6258
subindent = 0 ;
6235
6259
appendContextKeyword (context , "" , subindent , 0 , 0 );
6236
6260
6237
- get_setop_query (op -> rarg , query , context , resultDesc );
6261
+ get_setop_query (op -> rarg , query , context , resultDesc , false );
6238
6262
6239
6263
if (PRETTY_INDENT (context ))
6240
6264
context -> indentLevel -= subindent ;
@@ -6570,7 +6594,8 @@ get_rule_windowspec(WindowClause *wc, List *targetList,
6570
6594
* ----------
6571
6595
*/
6572
6596
static void
6573
- get_insert_query_def (Query * query , deparse_context * context )
6597
+ get_insert_query_def (Query * query , deparse_context * context ,
6598
+ bool colNamesVisible )
6574
6599
{
6575
6600
StringInfo buf = context -> buf ;
6576
6601
RangeTblEntry * select_rte = NULL ;
@@ -6680,6 +6705,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6680
6705
{
6681
6706
/* Add the SELECT */
6682
6707
get_query_def (select_rte -> subquery , buf , context -> namespaces , NULL ,
6708
+ false,
6683
6709
context -> prettyFlags , context -> wrapColumn ,
6684
6710
context -> indentLevel );
6685
6711
}
@@ -6773,7 +6799,7 @@ get_insert_query_def(Query *query, deparse_context *context)
6773
6799
{
6774
6800
appendContextKeyword (context , " RETURNING" ,
6775
6801
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6776
- get_target_list (query -> returningList , context , NULL );
6802
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6777
6803
}
6778
6804
}
6779
6805
@@ -6783,7 +6809,8 @@ get_insert_query_def(Query *query, deparse_context *context)
6783
6809
* ----------
6784
6810
*/
6785
6811
static void
6786
- get_update_query_def (Query * query , deparse_context * context )
6812
+ get_update_query_def (Query * query , deparse_context * context ,
6813
+ bool colNamesVisible )
6787
6814
{
6788
6815
StringInfo buf = context -> buf ;
6789
6816
RangeTblEntry * rte ;
@@ -6828,7 +6855,7 @@ get_update_query_def(Query *query, deparse_context *context)
6828
6855
{
6829
6856
appendContextKeyword (context , " RETURNING" ,
6830
6857
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
6831
- get_target_list (query -> returningList , context , NULL );
6858
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
6832
6859
}
6833
6860
}
6834
6861
@@ -6990,7 +7017,8 @@ get_update_query_targetlist_def(Query *query, List *targetList,
6990
7017
* ----------
6991
7018
*/
6992
7019
static void
6993
- get_delete_query_def (Query * query , deparse_context * context )
7020
+ get_delete_query_def (Query * query , deparse_context * context ,
7021
+ bool colNamesVisible )
6994
7022
{
6995
7023
StringInfo buf = context -> buf ;
6996
7024
RangeTblEntry * rte ;
@@ -7031,7 +7059,7 @@ get_delete_query_def(Query *query, deparse_context *context)
7031
7059
{
7032
7060
appendContextKeyword (context , " RETURNING" ,
7033
7061
- PRETTYINDENT_STD , PRETTYINDENT_STD , 1 );
7034
- get_target_list (query -> returningList , context , NULL );
7062
+ get_target_list (query -> returningList , context , NULL , colNamesVisible );
7035
7063
}
7036
7064
}
7037
7065
@@ -11039,7 +11067,7 @@ get_sublink_expr(SubLink *sublink, deparse_context *context)
11039
11067
if (need_paren )
11040
11068
appendStringInfoChar (buf , '(' );
11041
11069
11042
- get_query_def (query , buf , context -> namespaces , NULL ,
11070
+ get_query_def (query , buf , context -> namespaces , NULL , false,
11043
11071
context -> prettyFlags , context -> wrapColumn ,
11044
11072
context -> indentLevel );
11045
11073
@@ -11548,6 +11576,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
11548
11576
/* Subquery RTE */
11549
11577
appendStringInfoChar (buf , '(' );
11550
11578
get_query_def (rte -> subquery , buf , context -> namespaces , NULL ,
11579
+ true,
11551
11580
context -> prettyFlags , context -> wrapColumn ,
11552
11581
context -> indentLevel );
11553
11582
appendStringInfoChar (buf , ')' );
0 commit comments