本文介绍了如何在 PostgreSQL 中创建临时函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在数据库中执行一个循环.这只是一次性要求.执行完函数后,我现在正在删除该函数.

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 for creating temporary / disposable functions?

推荐答案

我需要知道如何在我正在编写的脚本中多次使用.原来你可以使用 pg_temp 模式创建一个临时函数.这是一个为您的连接按需创建的架构,是存储临时表的地方.当您的连接关闭或过期时,此架构将被删除.事实证明,如果您在此架构上创建一个函数,该架构将自动创建.因此,

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.

这篇关于如何在 PostgreSQL 中创建临时函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 18:27