How to create a temporary function in PostgreSQL?

前端 未结 4 1816
离开以前
离开以前 2020-11-27 15:09

I have to execute a loop in database. This is only a one time requirement. After executing the function, I am dropping the function now.

Is there any good approach f

4条回答
  •  孤城傲影
    2020-11-27 15:26

    I needed to know how to do a many time use in a script I was writing. Turns out you can create a temporary function using the pg_temp schema. This is a schema that is created on demand for your connection and is where temporary tables are stored. When your connection is closed or expires this schema is dropped. Turns out if you create a function on this schema, the schema will be created automatically. Therefore,

    create function pg_temp.testfunc() returns text as 
    $$ select 'hello'::text $$ language sql;
    

    will be a function that will stick around as long as your connection sticks around. No need to call a drop command.

提交回复
热议问题