Getting relations accessed by a query using the raw query string - Mailing list pgsql-hackers

From Amin
Subject Getting relations accessed by a query using the raw query string
Date
Msg-id CAF-KA89hG0Z09gcwkqTaa+v3ivXba8xMr4xn6a_DqTyAe-G38w@mail.gmail.com
Whole thread Raw
List 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!

pgsql-hackers by date:

Previous
From: Peter Smith
Date:
Subject: Re: Perform streaming logical transactions by background workers and parallel apply
Next
From: Thomas Munro
Date:
Subject: Re: 011_crash_recovery.pl intermittently fails