Re: void function and view select

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Philipp Kraus <philipp(dot)kraus(at)tu-clausthal(dot)de>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: void function and view select
Date: 2018-05-07 14:06:45
Message-ID: CAKFQuwbT_ZJWBr9==4WYcZS=WMyzUKYu01G4e3Qf06at=py4xw@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Mon, May 7, 2018 at 6:52 AM, Philipp Kraus <philipp(dot)kraus(at)tu-clausthal(dot)de
> wrote:

> Hello,
>
> I have got a complex query with a dynamic column result e.g.:
>
> select builddata('_foo‘);
> select * from _foo;
>
> The first is a plsql function which creates a temporary table, but the
> function returns void.
> The second call returns all the data from this table. But the columns of
> the temporary table are
> not strict fixed, so I cannot return a table by the function.
> So my question is, how can I build with this two lines a view, so that I
> can run "select * from myFooView“ or
> a function with a dynamic return set of columns e.g. „select myFoo()“?
>

​Executed queries must have a well-defined column structure at
parse/plan-time, execution cannot change the columns that are returned.

By extension, a view's column structure must be stable. Writing:

CREATE VIEW v1 AS
SELECT * FROM tbl1;

Causes the view to defined with all columns of tbl1 as known at the time of
the view's creation (i.e., * is expanded immediately).

You might be able to use cursors to accomplish whatever bigger goal you are
working toward (I'm not particularly fluent with this technique).

​The more direct way to accomplish this is:

SELECT *
FROM func_call() AS (col1 text, col2 int, col3 date)

i.e., have the function return "SETOF record" and then specify the format
of the returned record when calling the function.

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Philipp Kraus 2018-05-07 14:12:28 Re: void function and view select
Previous Message Adrian Klaver 2018-05-07 13:59:10 Re: Is it possible to get username information while writingtrigger?