Re: building a row with a plpgsql function

From: Joe Conway <mail(at)joeconway(dot)com>
To: Raphael Bauduin <raphael(dot)bauduin(at)be(dot)easynet(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: building a row with a plpgsql function
Date: 2004-11-05 22:14:04
Message-ID: 418BFB2C.7040905@joeconway.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Raphael Bauduin wrote:
> A little update on what I do (in case someone gets in the same situation
> as I am).
> Rather than writing the AS ( field type, ....) part of the query, I
> build it in my application
> each time a crosstab query is issued.
> For example for this query:
>
> SELECT * FROM crosstab
> (
> 'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
> 'SELECT DISTINCT attribute FROM cth ORDER BY 1'
> )
> AS
> (
> XXXXX
> );
>
> the application code replaces the XXXXX by getting the results of
> "SELECT DISTINCT attribute FROM cth ORDER BY 1" and iterating over
> the attributes returned to build the columns list (all columns are text).
>
> This works really fine. My problem now is that the query to get the
> attributes
> is taking a looong time (2.7 seconds), and it is issued twice!
>

Sorry for the slow response. Couple of thoughts:

1. As long as you are building the query in your application, use the
results of the distinct query to build the category sql as a UNION ALL
of literals -- e.g.:

SELECT * FROM crosstab
(
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
'SELECT ''temperature''
UNION ALL SELECT ''test_result''
UNION ALL SELECT ''test_startdate''
UNION ALL SELECT ''volts'''
)
AS
(
rowid text,
rowdt timestamp,
temperature int4,
test_result text,
test_startdate timestamp,
volts float8
);

2. How often do new attributes show up? If it is relatively infrequent,
you might want to build a table ("materialized view") from
"SELECT DISTINCT attribute FROM cth ORDER BY 1"
and then refresh it periodically.

Joe

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Joe Conway 2004-11-05 22:19:37 Re: c extension
Previous Message Felix.Onyango 2004-11-05 19:57:31 Pgsql install