pg_query icon indicating copy to clipboard operation
pg_query copied to clipboard

Is it possible to split apart multi-statement queries?

Open harto opened this issue 9 months ago • 2 comments

Hi there, thanks for the library! I was wondering if there was a way to split a multi-statement query into individual statements. For example, given an SQL query like select 1; select 2 — a way to deparse the individual parts back into select 1 and select 2? My use case is that my application has an event listener that can intercept queries and run explain analyze … on them. I want that to be able to support multi-statement queries.

harto avatar May 01 '25 11:05 harto

Thanks for reaching out!

The C library has the split functions for this, which we haven't yet made available in the Ruby bindings. This should be fairly straightforward to do, you'd just have to expand the Ruby-specific C code in pg_query_ruby.c.

Are you interested in making a PR for this?

lfittl avatar May 02 '25 19:05 lfittl

I'll see if I can make headway on that. Thanks for the pointer :)

harto avatar May 04 '25 23:05 harto

This has been added in 0077294d926403bc8291298dd8ddaf2f3695d45f, which can be accessed through PgQuery.split_with_parser(query)

lfittl avatar Jan 21 '26 02:01 lfittl