由于目前无法COPY
仅使用,您必须求助于外部文件处理。
这是一个使用大对象函数将文件数据放入单个表条目的版本:
CREATE TABLE snapshots (
id serial primary key,
data bytea
);
CREATE FUNCTION exportsnapshot()
RETURNS integer
LANGUAGE plpgsql
AS $$
DECLARE
data_file_id oid;
snapshot_id integer;
BEGIN
COPY (my_query) TO '/tmp/test.csv' With DELIMITER ';' CSV HEADER;
-- assign an OID to the external file...
SELECT lo_import('/tmp/test.csv') INTO STRICT data_file_id;
-- create a new snapshots entry by reading the complete file contents...
INSERT INTO snapshots (data)
SELECT lo_get(data_file_id)
RETURNING id
INTO snapshot_id;
-- forget about the external file...
PERFORM lo_unlink(data_file_id);
-- delete the file...
COPY (SELECT 1) TO PROGRAM 'rm /tmp/test.csv';
-- return the new snapshot ID...
RETURN snapshot_id;
END;
$$;
对于压缩,您应该真正考虑text
而不是bytea
.