0

I want to read file from OS filesystem and print its content as a table. The most preferable way is the using plperl (because there is no need to use temp tables and plperl is builtin by default in most postgres installations). But unfortunately, I'm not familiar with perl and my function doesn't work. It's read file but print nothing.

Could you check function body, and say where is the mistake?

    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)

Thanks!

lesovsky
  • 326
  • 2
  • 14
  • Why don't you use a foreign table with `file_fdw`: https://www.postgresql.org/docs/current/static/file-fdw.html –  Dec 17 '16 at 18:30
  • There are a lot of stats files in /proc and file_fdw is unable to handle a few of those files, for example /proc/meminfo or /proc/net/dev. It seems to me set of plperl functions will be a universal solution. – lesovsky Dec 17 '16 at 18:52

1 Answers1

0

Problem completely solved using the function example by Pavel Stěhule which is described here.

lesovsky
  • 326
  • 2
  • 14