libpg_query icon indicating copy to clipboard operation
libpg_query copied to clipboard

WISHLIST: Expose pg_query_nodes_to_json()

Open mdr1384 opened this issue 4 years ago • 2 comments

WISHLIST ITEM:

It would be very nice if you could expose pg_query_nodes_to_json() so that we can convert existing trees to JSON.

E.g. I can get the necessary query tree directly from Postgres via the pg_catalog:

psql -At -U superuser -d testdb -c "select r.ev_action from pg_catalog.pg_rewrite r left join pg_catalog.pg_class c on r.ev_class = c.oid where c.relname = 'my_sick_view'" > /tmp/my_sick_view.qtree

In my case this results in a file over 180KB.

Now it would be nice to be able to write a simple program to convert it to JSON using pg_query_nodes_to_json(), but it is not in the public header.

mdr1384 avatar May 12 '21 13:05 mdr1384

@mdr1384 I think that sounds reasonable - if I understand correctly you would also need a way to go from the built-in node text format (the one thats very obscure and hard to read) to in-memory structs, correct?

Or were you thinking of using this as an extension in a running Postgres server? (then you'd only need access to the function you mention)

lfittl avatar May 24 '21 20:05 lfittl

I was not thinking that anything special needed to be running on the server. Just a way to get the JSON query tree for a given PostgreSQL query tree. Don't need in-memory structs either, there are already libs that convert the JSON string to structs.

FYI my reasoning was that converting the SQL schema definition as in your basic example was failing for me due to the extreme complexity of the schema (a VIEW with about 70 columns and many JOINs and function calls and a couple WITH clauses.) So I figured let PostgreSQL do the SQL parsing and then all I need is to convert PostgreSQL query tree language to JSON. I saw there was a function to do that but it was not public.

mdr1384 avatar May 28 '21 16:05 mdr1384