Re: Queries w/ "computed" table names? (eval in Pg?)

Lists: pgsql-general
From: "Kynn Jones" <kynnjo(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Queries w/ "computed" table names? (eval in Pg?)
Date: 2008-02-22 23:40:28
Message-ID: c2350ba40802221540q8a7b658u4b170e9887dc0623@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Hi. Suppose I have a database that contains a "meta table" that holds the
names of other the tables in the database, keyed by human-readable but
longish strings. I would like to write queries that first "compute" the
names of some tables (i.e. by looking them up in "meta table"), and after
that they execute subqueries using these computed table names. The
following invalid SQL illustrates the kind of maneuver I'd like to do:
SELECT x, y, z
FROM [ SELECT table_name FROM meta_table
WHERE human_readable_key =
'some veeeery long and unwieldy string' ];

The stuff in [ brackets ] is not meant to be valid SQL, but rather to
suggest that the name of the table for the "outer" query corresponds to the
string returned by the "inner" (bracketed) query.

Some programming languages allow the run-time evaluation of a string
representing some code in the language. One way to do what I'd like to do
is based on this idea: I would construct the source code for the desired
subquery as a string (including the name of the table obtained at run-time
from meta_table), and "somehow" evaluate this string. This "somehow" is
what I'm missing. Is there a way in PostgreSQL to evaluate a string as SQL?

TIA!

kynn


From: Jeff Davis <pgsql(at)j-davis(dot)com>
To: Kynn Jones <kynnjo(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Queries w/ "computed" table names? (eval in Pg?)
Date: 2008-02-22 23:50:44
Message-ID: 1203724244.7878.44.camel@dogma.ljc.laika.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, 2008-02-22 at 18:40 -0500, Kynn Jones wrote:
> Hi. Suppose I have a database that contains a "meta table" that holds
> the names of other the tables in the database, keyed by human-readable
> but longish strings. I would like to write queries that first
> "compute" the names of some tables (i.e. by looking them up in "meta
> table"), and after that they execute subqueries using these computed
> table names. The following invalid SQL illustrates the kind of

You can create a PL/pgSQL function and use "EXECUTE".

Depending on how you want to use it, you may need to make it a set-
returning function (a.k.a. table function).

Regards,
Jeff Davis


From: Steve Atkins <steve(at)blighty(dot)com>
To: pgsql-general General <pgsql-general(at)postgresql(dot)org>
Subject: Re: Queries w/ "computed" table names? (eval in Pg?)
Date: 2008-02-22 23:51:08
Message-ID: F78A9EC3-DA79-49B4-BADF-994FFC547A10@blighty.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


On Feb 22, 2008, at 3:40 PM, Kynn Jones wrote:

> Hi. Suppose I have a database that contains a "meta table" that
> holds the names of other the tables in the database, keyed by human-
> readable but longish strings. I would like to write queries that
> first "compute" the names of some tables (i.e. by looking them up in
> "meta table"), and after that they execute subqueries using these
> computed table names. The following invalid SQL illustrates the
> kind of maneuver I'd like to do:
>
> SELECT x, y, z
> FROM [ SELECT table_name FROM meta_table
> WHERE human_readable_key =
> 'some veeeery long and unwieldy string' ];
>
> The stuff in [ brackets ] is not meant to be valid SQL, but rather
> to suggest that the name of the table for the "outer" query
> corresponds to the string returned by the "inner" (bracketed) query.
>
> Some programming languages allow the run-time evaluation of a string
> representing some code in the language. One way to do what I'd like
> to do is based on this idea: I would construct the source code for
> the desired subquery as a string (including the name of the table
> obtained at run-time from meta_table), and "somehow" evaluate this
> string. This "somehow" is what I'm missing. Is there a way in
> PostgreSQL to evaluate a string as SQL?
>

You can do it from within pl/pgsql - see http://www.postgresql.org/docs/8.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

I don't think there's any way to do it from plain sql, but you could
probably create a small pl/pgsql wrapper function to do it.

Cheers,
Steve


From: "Kynn Jones" <kynnjo(at)gmail(dot)com>
To: "Steve Atkins" <steve(at)blighty(dot)com>
Cc: "pgsql-general General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Queries w/ "computed" table names? (eval in Pg?)
Date: 2008-02-23 13:28:57
Message-ID: c2350ba40802230528v51661072mf3d465287f320acb@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Fri, Feb 22, 2008 at 6:51 PM, Steve Atkins <steve(at)blighty(dot)com> wrote:

>
> On Feb 22, 2008, at 3:40 PM, Kynn Jones wrote:
>
> > Hi. Suppose I have a database that contains a "meta table" that
> > holds the names of other the tables in the database, keyed by human-
> > readable but longish strings. I would like to write queries that
> > first "compute" the names of some tables (i.e. by looking them up in
> > "meta table"), and after that they execute subqueries using these
> > computed table names. The following invalid SQL illustrates the
> > kind of maneuver I'd like to do:
> >
> > SELECT x, y, z
> > FROM [ SELECT table_name FROM meta_table
> > WHERE human_readable_key =
> > 'some veeeery long and unwieldy string' ];
> >
> > The stuff in [ brackets ] is not meant to be valid SQL, but rather
> > to suggest that the name of the table for the "outer" query
> > corresponds to the string returned by the "inner" (bracketed) query.
> >
> > Some programming languages allow the run-time evaluation of a string
> > representing some code in the language. One way to do what I'd like
> > to do is based on this idea: I would construct the source code for
> > the desired subquery as a string (including the name of the table
> > obtained at run-time from meta_table), and "somehow" evaluate this
> > string. This "somehow" is what I'm missing. Is there a way in
> > PostgreSQL to evaluate a string as SQL?
> >
>
> You can do it from within pl/pgsql - see
> http://www.postgresql.org/docs/8.3/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
>
> I don't think there's any way to do it from plain sql, but you could
> probably create a small pl/pgsql wrapper function to do it.

Yep, that's just what I was looking for. Thanks to you, and to Jeff also!

Kynn