2020#include "access/sysattr.h"
2121#include "catalog/pg_class.h"
2222#include "catalog/pg_operator.h"
23- #include "catalog/pg_type.h"
2423#include "foreign/fdwapi.h"
2524#include "nodes/makefuncs.h"
2625#include "nodes/nodeFuncs.h"
@@ -2117,6 +2116,7 @@ remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
21172116foreach (lc ,subquery -> targetList )
21182117{
21192118TargetEntry * tle = (TargetEntry * )lfirst (lc );
2119+ Node * texpr = (Node * )tle -> expr ;
21202120
21212121/*
21222122 * If it has a sortgroupref number, it's used in some sort/group
@@ -2140,28 +2140,24 @@ remove_unused_subquery_outputs(Query *subquery, RelOptInfo *rel)
21402140 * If it contains a set-returning function, we can't remove it since
21412141 * that could change the number of rows returned by the subquery.
21422142 */
2143- if (expression_returns_set (( Node * ) tle -> expr ))
2143+ if (expression_returns_set (texpr ))
21442144continue ;
21452145
21462146/*
21472147 * If it contains volatile functions, we daren't remove it for fear
21482148 * that the user is expecting their side-effects to happen.
21492149 */
2150- if (contain_volatile_functions (( Node * ) tle -> expr ))
2150+ if (contain_volatile_functions (texpr ))
21512151continue ;
21522152
21532153/*
21542154 * OK, we don't need it. Replace the expression with a NULL constant.
2155- *We can just make theconstant be ofINT4 type, since nothing's
2156- *going to look at it anyway .
2155+ *Preserve theexposed type ofthe expression, in case something
2156+ *looks at the rowtype of the subquery's result .
21572157 */
2158- tle -> expr = (Expr * )makeConst (INT4OID ,
2159- -1 ,
2160- InvalidOid ,
2161- sizeof (int32 ),
2162- (Datum )0 ,
2163- true,/* isnull */
2164- true/* byval */ );
2158+ tle -> expr = (Expr * )makeNullConst (exprType (texpr ),
2159+ exprTypmod (texpr ),
2160+ exprCollation (texpr ));
21652161}
21662162}
21672163