Thread: Traversing targetlist to find accessed columns
Hi,
Having a query, I am trying to find out all the columns that need to be accessed (their varattno and vartype). I have access to a targetlist representing a tree like this. So, I am looking for a function that recursively traverses the tree and gives me the VARs. So, for SELECT a,b,b+c from tab; I am interested in [a,b]. Is such a function currently implemented in postgresql? How can I use it?
:targetlist (
{TARGETENTRY
:expr
{VAR
:varno 1
:varattno 1
:vartype 23
:vartypmod -1
:varcollid 0
:varlevelsup 0
:varnosyn 1
:varattnosyn 1
:location 7
}
:resno 1
:resname l_orderkey
:ressortgroupref 0
:resorigtbl 24805
:resorigcol 1
:resjunk false
}
{TARGETENTRY
:expr
{VAR
:varno 1
:varattno 2
:vartype 23
:vartypmod -1
:varcollid 0
:varlevelsup 0
:varnosyn 1
:varattnosyn 2
:location 18
}
:resno 2
:resname l_partkey
:ressortgroupref 0
:resorigtbl 24805
:resorigcol 2
:resjunk false
}
{TARGETENTRY
:expr
{OPEXPR
:opno 551
:opfuncid 177
:opresulttype 23
:opretset false
:opcollid 0
:inputcollid 0
:args (
{OPEXPR
:opno 551
:opfuncid 177
:opresulttype 23
:opretset false
:opcollid 0
:inputcollid 0
:args (
{VAR
:varno 1
:varattno 1
:vartype 23
:vartypmod -1
:varcollid 0
:varlevelsup 0
:varnosyn 1
:varattnosyn 1
:location 28
}
{VAR
:varno 1
:varattno 2
:vartype 23
:vartypmod -1
:varcollid 0
:varlevelsup 0
:varnosyn 1
:varattnosyn 2
:location 39
}
)
:location 38
}
{VAR
:varno 1
:varattno 3
:vartype 23
:vartypmod -1
:varcollid 0
:varlevelsup 0
:varnosyn 1
:varattnosyn 3
:location 49
}
)
:location 48
}
:resno 3
:resname ?column?
:ressortgroupref 0
:resorigtbl 0
:resorigcol 0
:resjunk false
}
)
Amin <amin.fallahi@gmail.com> writes: > Having a query, I am trying to find out all the columns that need to be > accessed (their varattno and vartype). I have access to a targetlist > representing a tree like this. So, I am looking for a function that > recursively traverses the tree and gives me the VARs. So, for SELECT > a,b,b+c from tab; I am interested in [a,b]. Is such a function currently > implemented in postgresql? How can I use it? pull_var_clause() might help you, or one of its siblings in src/backend/optimizer/util/var.c, or you could use that as a template to write your own --- it doesn't take much code if you use expression_tree_walker to do the dirty work. regards, tom lane