Given a view relation OID, how to construct a Query?

Started by Eric Ridgeabout 10 years ago5 messages
#1Eric Ridge
eebbrr@gmail.com

I'm doing some extension development (in C) and have a situation where I
need to examine the target list of a view, but all I have is the view's oid.

An approach that works is (pseudocode):
SPI_connect();
"SELECT ev_action FROM pg_catalog.pg_rewrite WHERE rulename = '_RETURN'
and ev_class=?oid";
Query *query = linitial(stringToNode(ev_action));
...
SPI_finish();

I backed into this by tracing through pg_getviewdef(). Is there a more
direct way to do this without going through SPI?

I also looked at using postgres.c#pg_analyze_and_rewrite() against a query
like "SELECT * FROM viewname" but the target list of the actual query
wasn't what I was expecting (individual entry tags don't match those of the
SPI approach above).

Thanks for your time!

eric

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Eric Ridge (#1)
Re: Given a view relation OID, how to construct a Query?

Eric Ridge <eebbrr@gmail.com> writes:

I'm doing some extension development (in C) and have a situation where I
need to examine the target list of a view, but all I have is the view's oid.

Open the relation and use get_view_query(), perhaps.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#3Eric Ridge
eebbrr@gmail.com
In reply to: Tom Lane (#2)
Re: Given a view relation OID, how to construct a Query?

On Wed, Dec 9, 2015 at 4:04 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Eric Ridge <eebbrr@gmail.com> writes:

I'm doing some extension development (in C) and have a situation where I
need to examine the target list of a view, but all I have is the view's

oid.

Open the relation and use get_view_query(), perhaps.

I figured there was something simple, but I couldn't find it. Thanks!
Sadly, it's static.

eric

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Eric Ridge (#3)
Re: Given a view relation OID, how to construct a Query?

Eric Ridge <eebbrr@gmail.com> writes:

On Wed, Dec 9, 2015 at 4:04 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Open the relation and use get_view_query(), perhaps.

I figured there was something simple, but I couldn't find it. Thanks!
Sadly, it's static.

FWIW, it's exposed in 9.4 and up. But in older branches you could
probably just copy it, it's not that big.

regards, tom lane

--
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

#5Eric Ridge
eebbrr@gmail.com
In reply to: Tom Lane (#4)
Re: Given a view relation OID, how to construct a Query?

On Wed, Dec 9, 2015 at 5:07 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

FWIW, it's exposed in 9.4 and up. But in older branches you could
probably just copy it, it's not that big.

That's good to know, thanks. I did copy it and it's almost 3x faster than
going through SPI. Thanks again for the pointer.

eric