from eshizhanhtml
Here has a simple way to get free disk space without any extended language, just define a function using pgsql.sql
CREATE OR REPLACE FUNCTION sys_df() RETURNS SETOF text[] LANGUAGE plpgsql $$ BEGIN CREATE TEMP TABLE IF NOT EXISTS tmp_sys_df (content text) ON COMMIT DROP; COPY tmp_sys_df FROM PROGRAM 'df | tail -n +2'; RETURN QUERY SELECT regexp_split_to_array(content, '\s+') FROM tmp_sys_df; END; $$;
Usage:shell
select * from sys_df(); sys_df ------------------------------------------------------------------- {overlay,15148428,6660248,7695656,46%,/} {overlay,15148428,6660248,7695656,46%,/} {tmpfs,65536,0,65536,0%,/dev} {tmpfs,768284,0,768284,0%,/sys/fs/cgroup} {/dev/sda2,15148428,6660248,7695656,46%,/etc/resolv.conf} {/dev/sda2,15148428,6660248,7695656,46%,/etc/hostname} {/dev/sda2,15148428,6660248,7695656,46%,/etc/hosts} {shm,65536,8,65528,0%,/dev/shm} {/dev/sda2,15148428,6660248,7695656,46%,/var/lib/postgresql/data} {tmpfs,65536,0,65536,0%,/proc/kcore} {tmpfs,65536,0,65536,0%,/proc/timer_list} {tmpfs,65536,0,65536,0%,/proc/sched_debug} {tmpfs,768284,0,768284,0%,/sys/firmware} (13 rows)
Using df $PGDATA | tail -n +2
instead of df | tail -n +2
while you saving all data in same path on disk. In this case, the function only return one row disk usage for $PGDATA path.post
NOTE FOR SECURITYthis
PROGRAM can run any command by shell, it like two-edged sword. it is best to use a fixed command string, or at least avoid passing any user input in it. See detail on document.spa
PROGRAM能夠運行任意的命令,因此最好是用固定字符串命令,儘可能避免經過參數傳入。debug