From a371ca5f3bf7a9d7602aa5531a90e64c62111135 Mon Sep 17 00:00:00 2001 From: Shigeru Hanada Date: Thu, 24 Feb 2011 11:45:38 +0900 Subject: [PATCH] Gather used column from baserestrictinfo and reltargetlist of RelOptInfo. --- contrib/postgresql_fdw/postgresql_fdw.c | 41 ++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/contrib/postgresql_fdw/postgresql_fdw.c b/contrib/postgresql_fdw/postgresql_fdw.c index 70e7500b06..940ff1f8d0 100644 --- a/contrib/postgresql_fdw/postgresql_fdw.c +++ b/contrib/postgresql_fdw/postgresql_fdw.c @@ -27,6 +27,7 @@ #include "optimizer/clauses.h" #include "optimizer/cost.h" #include "optimizer/plancat.h" +#include "optimizer/var.h" #include "parser/parsetree.h" #include "parser/scansup.h" #include "utils/builtins.h" @@ -332,6 +333,8 @@ is_foreign_qual(PlannerInfo *root, RelOptInfo *baserel, Expr *expr) static char * deparseSql(Oid foreigntableid, PlannerInfo *root, RelOptInfo *baserel) { + AttrNumber attr; + List *attr_used = NIL; List *context; StringInfoData sql; ForeignTable *table = GetForeignTable(foreigntableid); @@ -347,24 +350,52 @@ deparseSql(Oid foreigntableid, PlannerInfo *root, RelOptInfo *baserel) context = deparse_context_for("foreigntable", foreigntableid); + /* Collect used columns from restrict information and target list */ + foreach (lc, baserel->baserestrictinfo) + { + List *l; + RestrictInfo *ri = lfirst(lc); + + l = pull_var_clause((Node *) ri->clause, PVC_RECURSE_PLACEHOLDERS); + attr_used = list_union(attr_used, l); + } + attr_used = list_union(attr_used, baserel->reltargetlist); + /* deparse SELECT target list */ appendStringInfoString(&sql, "SELECT "); first = true; - foreach (lc, baserel->reltargetlist) + for (attr = baserel->min_attr; attr < baserel->max_attr; attr++) { - Var *var = lfirst(lc); + Var *var; - /* system columns cannot be retrieved from remote */ - if (var->varattno < 0) + /* System columns cannot be retrieved from remote. */ + if (attr < 0) continue; + /* Separete columns with comma. */ if (!first) appendStringInfoString(&sql, ", "); first = false; - deparse_var(root, &sql, var); + + /* Use "NULL" for column which isn't in reltargetlist. */ + foreach (lc, attr_used) + { + var = lfirst(lc); + if (var->varattno == attr) + break; + var = NULL; + } + + if (var != NULL) + deparse_var(root, &sql, var); + else + appendStringInfo(&sql, "NULL"); } if (strcmp(sql.data, "SELECT ") == 0) + { + ereport(DEBUG1, (errmsg("targetlist is empty, retrieve all columns"))); appendStringInfo(&sql, "*"); + } /* * Deparse FROM -- 2.39.5