sql - How to get the current free disk space in Postgres? -


i need sure have @ least 1gb of free disk space before start doing work in database. i'm looking this:

select pg_get_free_disk_space(); 

is possible? (i found nothing in docs).

pg: 9.3 & os: linux/windows

postgresql not have features directly expose disk space.

for 1 thing, disk? production postgresql instance looks this:

  • /pg/pg94/: raid6 of fast reliable storage on bbu raid controller in wb mode, catalogs , important data
  • /pg/pg94/pg_xlog: fast reliable raid1, transaction logs
  • /pg/tablespace-lowredundancy: raid10 of fast cheap storage things indexes , unlogged tables don't care losing can use lower-redundancy storage
  • /pg/tablespace-bulkdata: raid6 or similar of slow near-line magnetic storage used old audit logs, historical data, write-mostly data, , other things can slower access.
  • the postgresql logs somewhere else again, if fills up, system may still stop. depends on number of configuration settings, of can't see postgresql @ all, syslog options.

then there's fact "free" space doesn't mean postgresql can use (think: disk quotas, system-reserved disk space), , fact free blocks/bytes isn't constraint, many file systems have limits on number of files (inodes).

how aselect pg_get_free_disk_space() report this?

knowing free disk space security concern. if supported, it's that'd exposed superuser, @ least.

what can use untrusted procedural language plpythonu make operating system calls interrogate host os disk space information, using queries against pg_catalog.pg_tablespace , using data_directory setting pg_settings discover postgresql keeping stuff on host os. have check mount points (unix/mac) / junction points (windows) discover if pg_xlog, etc, on separate storage. still won't space logs, though.

i'd quite have select * pg_get_free_diskspace reported main datadir space, , mount points or junction points within pg_xlog or pg_clog, , reported each tablespace , mount points within it. it'd set-returning function. cares enough have bother implement for target platforms though, , right now, nobody wants enough work.


in mean time, if you're willing simplify needs to:

  • one file system
  • target os unix/posix-compatible linux
  • there's no quota system enabled
  • there's no root-reserved block percentage
  • inode exhaustion not concern

then can create language plpython3u; , create function language plpython3u function like:

import os st = os.statvfs(datadir_path) return st.f_bavail * st.f_frsize 

in function returns bigint , either takes datadir_path argument, or discovers doing spi query select setting pg_settings name = 'data_directory' within pl/python.

if want support windows too, see cross-platform space remaining on volume using python . i'd use windows management interface (wmi) queries rather using ctypes call windows api though.

or use function wrote in pl/perlu using df , mount command output parsing, work on linux, hey, it's prewritten.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -