简体   繁体   中英

creating a view in a postgresql function

I have a function that proccess some data:

CREATE OR REPLACE FUNCTION A(x integer, y timestamp with time zone, z integer[])
      RETURNS SETOF typea AS

$BODY$
SELECT *
FROM func1($1,$2,$3) as a
WHERE ...


$BODY$
LANGUAGE sql VOLATILE

How can I convert the query:

SELECT *
FROM func1($1,$2,$3) as a
WHERE ...

into a VIEW ? The thing is that func1 needs the function A parameters...

What I have now is this:

CREATE OR REPLACE FUNCTION A(x integer, y timestamp with time zone, z integer[])
      RETURNS SETOF typea AS
$BODY$
CREATE OR REPLACE VIEW   myView as (select * from  func1($1,$2,$3));
select * from myView ;
$BODY$
LANGUAGE sql VOLATILE

and i get

ERROR: relation "myView " does not exist

in the select * from myView ;

I think the problem is that Postgres functions are handled in two steps. In the first step, the function is compiled. In the second, it is executed.

If the view does not exist, then it is not created during the compilation stage. However, the select will fail, because the view doesn't exist.

Creating a view in a function seems strange to me. However, I can think of two work-arounds:

  • Be sure the view exists before the function is created.
  • Use dynamic SQL to access the view within the function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM