gpt4 book ai didi

Postgresql COPY 命令 - 如何删除临时文件?

转载 作者:行者123 更新时间:2023-11-29 12:10:39 24 4
gpt4 key购买 nike

我有程序并且效果很好。

CREATE OR REPLACE FUNCTION f_dynamic_copy(_tbl     text = 'tmp1')
RETURNS void AS
$func$
DECLARE
_filename VARCHAR;
BEGIN
_filename := '/tmp/' || random() || '.csv';
EXECUTE format($$COPY (select id, 1, 1, 1 from my_first_table) TO %L$$, _filename);
EXECUTE format($$COPY my_second_table FROM %L$$, _filename);
END
$func$ LANGUAGE plpgsql;

但我想删除在此过程中创建的临时文件。怎么做?

最佳答案

我找到了 PostgreSQL 9.3+ 的解决方案:

-- Delete temporary file
EXECUTE format(
$$COPY (SELECT 1) TO PROGRAM 'rm %I'$$,
_filename
);

所以,我的功能是

CREATE OR REPLACE FUNCTION f_dynamic_copy(_tbl     text = 'tmp1')
RETURNS void AS
$func$
DECLARE
_filename VARCHAR;
BEGIN
_filename := '/tmp/' || random() || '.csv';
EXECUTE format($$COPY (select id, 1, 1, 1 from my_first_table) TO %L$$, _filename);
EXECUTE format($$COPY my_second_table FROM %L$$, _filename);

-- Delete temporary file
EXECUTE format(
$$COPY (SELECT 1) TO PROGRAM 'rm %I'$$,
_filename
);

END
$func$ LANGUAGE plpgsql;

关于Postgresql COPY 命令 - 如何删除临时文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37283112/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com