0
我想從操作系統文件系統讀取文件並將其內容打印爲表格。最好的方法是使用plperl(因爲在大多數postgres安裝中默認情況下不需要使用臨時表和plperl內置)。但不幸的是,我不熟悉Perl,我的功能不起作用。它是讀取文件,但不打印任何內容。讀取文件並用postgresql打印plperl
你能檢查函數體,並說錯誤在哪裏嗎?
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)
謝謝!
爲什麼不用'file_fdw'使用外部表格:https://www.postgresql.org/docs/current/static/file-fdw.html –
/proc中有很多統計文件, file_fdw無法處理其中的一些文件,例如/ proc/meminfo或/ proc/net/dev。在我看來,一套plperl函數將是一個通用的解決方案。 – lesovsky