Getting column names/types from select query? - Mailing list pgsql-hackers

From Wesley Aptekar-Cassels
Subject Getting column names/types from select query?
Date
Msg-id 19cb0bf8-4cc1-4f32-8005-873a14e5cb79@www.fastmail.com
Whole thread Raw
Responses Re: Getting column names/types from select query?  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hi all,

I am interested in figuring out how to get the names and types of the columns from an arbitrary query. Essentially, I
wantto be able to take a query like: 

CREATE TABLE foo(
    bar bigserial,
    baz varchar(256)
);

SELECT * FROM foo WHERE bar = 42;

and figure out programmatically that the select will return a column "bar" of type bigserial, and a column "foo" of
typevarchar(256). I would like this to work for more complex queries as well (joins, CTEs, etc). 

I've found https://wiki.postgresql.org/wiki/Query_Parsing, which talks about related ways to hook into postgres, but
thatseems to only talk about the parse tree — a lot more detail and processing seems to be required in order to figure
outthe output types. It seems like there should be somewhere I can hook into in postgres that will get me this
information,but I have no familiarity with the codebase, so I don't know the best way to get this. 

How would you recommend that I approach this? I'm comfortable patching postgres if needed, although if there's a
solutionthat doesn't require that, I'd prefer that. 

Thanks,

:w



pgsql-hackers by date:

Previous
From: Ashutosh Bapat
Date:
Subject: Re: Printing LSN made easy
Next
From: Pavel Stehule
Date:
Subject: Re: poc - possibility to write window function in PL languages