Getting relations accessed by a query using the raw query string

From: Amin <amin(dot)fallahi(at)gmail(dot)com>
To: pgsql-hackers(at)postgresql(dot)org
Subject: Getting relations accessed by a query using the raw query string
Date: 2023-01-24 23:34:59
Message-ID: CAF-KA89hG0Z09gcwkqTaa+v3ivXba8xMr4xn6a_DqTyAe-G38w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Having a query string, I am trying to use the postgres parser to find which
relations the query accesses. This is what I currently have:

const char *query_string="select * from dummytable;";
List *parsetree_list=pg_parse_query(query_string);
ListCell *parsetree_item;

foreach(parsetree_item,parsetree_list){
RawStmt *parsetree=lfirst_node(RawStmt,parsetree_item);
Query *query=parse_analyze(parsetree,query_string,NULL,0,NULL);
}

However, when I inspect the variable "query", it is not populated
correctly. For example, commandType is set to CMD_DELETE while I have
passed a SELECT query.
- What am I doing wrong?
- Once I get the query correctly, how can I get the list of relations it
gets access to?
- Or any other ways to get the list of relations from raw query string
through postgres calls?

Thank you!

Browse pgsql-hackers by date

  From Date Subject
Next Message Thomas Munro 2023-01-24 23:40:02 Re: 011_crash_recovery.pl intermittently fails
Previous Message Peter Smith 2023-01-24 23:30:19 Re: Perform streaming logical transactions by background workers and parallel apply