アレッ*_*ックス 3 postgresql plpgsql postgresql-9.2
我正试图使用临时表:
CREATE OR REPLACE FUNCTION test1(user_id BIGINT) RETURNS BIGINT AS
$BODY$
BEGIN
create temp table temp_table1
ON COMMIT DELETE ROWS
as SELECT table1.column1, table1.column2
FROM table1
INNER JOIN -- ............
if exists (select * from temp_table1) then
-- work with the result
return 777;
else
return 0;
end if;
END;
$BODY$
LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
我希望temp_table1立即或尽快删除该行,这就是我添加的原因ON COMMIT DELETE ROWS.显然,我收到了错误:
ERROR: relation "temp_table1" already exists
Run Code Online (Sandbox Code Playgroud)
我试图添加,IF NOT EXISTS但我不能,我根本找不到它的工作示例,这将是我正在寻找的.
你的建议?
Ile*_*tel 10
每次创建TEMP表之前的DROP表如下所示:
BEGIN
DROP TABLE IF EXISTS temp_table1;
create temp table temp_table1
-- Your rest Code comes here
Run Code Online (Sandbox Code Playgroud)
临时表的问题在于,删除并重新创建临时表会使 pg_attribute 严重膨胀,因此在一个阳光明媚的早晨,您会发现数据库性能已失效,并且 pg_attribute 为 200+ GB,而您的数据库大约为 10GB。
因此,我们非常依赖具有 >500 rps 的临时表和通过 Nodejs 进行的异步 i\o,因此经历了 pg_attribute 的严重膨胀。剩下的就是非常激进的吸尘,这会停止性能。这里给出的所有答案都不能解决这个问题,因为它们都使 pg_attribute 严重膨胀。
所以解决方案很优雅
create temp table if not exists my_temp_table (description) on commit delete rows;
Run Code Online (Sandbox Code Playgroud)
所以你继续使用临时表并保存你的 pg_attribute。