From: Shigeru Hanada Date: Thu, 24 Feb 2011 02:45:38 +0000 (+0900) Subject: Gather used column from baserestrictinfo and reltargetlist of RelOptInfo. X-Git-Url: http://git.postgresql.org/gitweb/static/gitweb.js?a=commitdiff_plain;h=a371ca5f3bf7a9d7602aa5531a90e64c62111135;p=users%2Fhanada%2Fpostgres.git Gather used column from baserestrictinfo and reltargetlist of RelOptInfo. --- 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