@@ -6739,11 +6739,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
6739
6739
int varno = ((RangeTblRef * ) jtnode )-> rtindex ;
6740
6740
RangeTblEntry * rte = rt_fetch (varno , query -> rtable );
6741
6741
char * refname = get_rtable_name (varno , context );
6742
- bool gavealias = false ;
6742
+ bool printalias ;
6743
6743
6744
6744
if (rte -> lateral )
6745
6745
appendStringInfoString (buf , "LATERAL " );
6746
6746
6747
+ /* Print the FROM item proper */
6747
6748
switch (rte -> rtekind )
6748
6749
{
6749
6750
case RTE_RELATION :
@@ -6776,11 +6777,12 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
6776
6777
break ;
6777
6778
}
6778
6779
6780
+ /* Print the relation alias, if needed */
6781
+ printalias = false;
6779
6782
if (rte -> alias != NULL )
6780
6783
{
6781
6784
/* Always print alias if user provided one */
6782
- appendStringInfo (buf , " %s" , quote_identifier (refname ));
6783
- gavealias = true;
6785
+ printalias = true;
6784
6786
}
6785
6787
else if (rte -> rtekind == RTE_RELATION )
6786
6788
{
@@ -6790,10 +6792,7 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
6790
6792
* resolve a conflict).
6791
6793
*/
6792
6794
if (strcmp (refname , get_relation_name (rte -> relid )) != 0 )
6793
- {
6794
- appendStringInfo (buf , " %s" , quote_identifier (refname ));
6795
- gavealias = true;
6796
- }
6795
+ printalias = true;
6797
6796
}
6798
6797
else if (rte -> rtekind == RTE_FUNCTION )
6799
6798
{
@@ -6802,16 +6801,28 @@ get_from_clause_item(Node *jtnode, Query *query, deparse_context *context)
6802
6801
* renaming of the function and/or instability of the
6803
6802
* FigureColname rules for things that aren't simple functions.
6804
6803
*/
6805
- appendStringInfo (buf , " %s" , quote_identifier (refname ));
6806
- gavealias = true;
6804
+ printalias = true;
6805
+ }
6806
+ else if (rte -> rtekind == RTE_CTE )
6807
+ {
6808
+ /*
6809
+ * No need to print alias if it's same as CTE name (this would
6810
+ * normally be the case, but not if set_rtable_names had to
6811
+ * resolve a conflict).
6812
+ */
6813
+ if (strcmp (refname , rte -> ctename ) != 0 )
6814
+ printalias = true;
6807
6815
}
6816
+ if (printalias )
6817
+ appendStringInfo (buf , " %s" , quote_identifier (refname ));
6808
6818
6819
+ /* Print the column definitions or aliases, if needed */
6809
6820
if (rte -> rtekind == RTE_FUNCTION )
6810
6821
{
6811
6822
if (rte -> funccoltypes != NIL )
6812
6823
{
6813
6824
/* Function returning RECORD, reconstruct the columndefs */
6814
- if (!gavealias )
6825
+ if (!printalias )
6815
6826
appendStringInfo (buf , " AS " );
6816
6827
get_from_clause_coldeflist (rte -> eref -> colnames ,
6817
6828
rte -> funccoltypes ,
0 commit comments