我想从OS文件系统中读取文件,并将其内容打印为表格。最可取的方式是使用plperl (因为不需要使用临时表,并且plperl在大多数postgres安装中默认是内置的)。但不幸的是,我不熟悉perl,并且我的函数无法工作。它是已读文件,但不打印任何内容。
你能检查一下函数体,然后说出错误在哪里吗?
CREATE OR REPLACE FUNCTION public.file_read(text)
RETURNS table(maj int, min int, dev text, reads bigint, rmerges bigint, rsects bigint, rspent bigint, writes bigint, wmerges bigint, wsects bigint, wspent bigint, inprog bigint, spent bigint, weighted bigint) AS
$$
open (datfile, $_[0]);
while ($line = <$datfile>) {
chomp $line;
@line = split(/\s+/, $line);
return_next({maj => $line[0], min => $line[1], dev => $line[2],
reads => $line[3], rmerges => $line[4], rsects => $line[5], rspent => $line[6],
writes => $line[7], wmerges => $line[8], wsects => $line[9], wspent => $line[10],
inprog => $line[11], spent => $line[12], weighted => $line[13]
});
}
close (datfile);
return undef;
$$ LANGUAGE plperlu;
# select * from file_read('/proc/diskstats');
maj | min | dev | reads | rmerges | rsects | rspent | writes | wmerges | wsects | wspent | inprog | spent | weighted
-----+-----+-----+-------+---------+--------+--------+--------+---------+--------+--------+--------+-------+----------
(0 rows)谢谢!
发布于 2016-12-18 20:31:13
使用here描述的Pavel Stěhule的函数示例完全解决了问题。
https://stackoverflow.com/questions/41201725
复制相似问题