From: Julien Rouhaud <rjuju123@gmail.com>
To: Pierre Forstmann <pierre.forstmann@gmail.com>
Cc: pgsql-general@postgresql.org
Subject: Re: how to use query_tree_walker to get all relations used in a query
Date: Sun, 20 Aug 2023 10:53:01 +0800
Message-ID: <20230820025301.4sre5chx6jelc2ry@jrouhaud> (raw)
In-Reply-To: <CAM-sOH8Ogr-EmKrVcFwpx1romhyc8t2ttgfzSaWmgxW7kP+t1Q@mail.gmail.com>
References: <CAM-sOH8Ogr-EmKrVcFwpx1romhyc8t2ttgfzSaWmgxW7kP+t1Q@mail.gmail.com>
Hi,
On Sat, Aug 19, 2023 at 03:26:06PM +0200, Pierre Forstmann wrote:
>
> I am trying to get the list of all relations used in a SELECT query using
> the post parse analyze hook.
>
> I can get all relations from top level FROM clause but I cannot get them
> for a simple subquery like:
>
> select * from t1 where x1=(select max(x2) from t2);
You're missing at least the sublinks. Since you're looking at
AcquireRewriteLocks() as an example, look at how acquireLocksOnSubLinks() is
being called.
In general, if you're not sure of how some constructs are represented in a
Query the easiest way to get a good overview is to simply do something like
static void pgds_build_rel_array(Query *query)
{
[...]
elog(WARNING, "found %s", nodeToString(query));
You will get a textual representation of the query (or any specific node if
needed) in the client and server log, so you can easily look for a given relid,
Var or anything and identify what you were missing and adapt your walker
function.
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: rjuju123@gmail.com, pierre.forstmann@gmail.com
Subject: Re: how to use query_tree_walker to get all relations used in a query
In-Reply-To: <20230820025301.4sre5chx6jelc2ry@jrouhaud>
References: <CAM-sOH8Ogr-EmKrVcFwpx1romhyc8t2ttgfzSaWmgxW7kP+t1Q@mail.gmail.com> <20230820025301.4sre5chx6jelc2ry@jrouhaud>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox