Getting total and free disk space from paths in PGDATA

From: Michael Paquier <michael(dot)paquier(at)gmail(dot)com>
To: PostgreSQL mailing lists <pgsql-hackers(at)postgresql(dot)org>
Subject: Getting total and free disk space from paths in PGDATA
Date: 2015-09-07 02:04:35
Message-ID: CAB7nPqSRZoYJP+hqiH=0PU0=P9pwagLU39YK7pO5CJJF-_k02w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi all,

There is currently no in-core function to query the amount of
available and free space in a path of PGDATA, with something like that
for example:
SELECT * FROM pg_get_diskspace_info('pg_xlog');
total_space | free_space
-------------+------------
4812 MB | 3925 MB
(1 row)

This would be definitely useful for monitoring purposes to have a look
at the disk space bloat in PGDATA, pg_xlog, or even pg_log which are
usually located on different partitions. Some of my customers have
requested such a thing for a couple of times, and even if you can
already do it with for example plpython/os.statvfs or plperl by
parsing the output of df, I am getting the feeling that we should have
something in-core not directly relying on an PL language. genfile.c
has also what is needed to restrict the path used, by blocking
absolute paths that are not part of log_directory or PGDATA.

statvfs is part of the POSIX spec and is "normally" present on modern
platforms (BSD, OSX, Linux and Solaris have it as far as I saw, still
there may be some strange platform without it). Windows does not have
it, though we could use GetDiskFreeSpaceEx to retrieve this
information (and actually this is the reason why I am only proposing
to get the available/free space from a path):
https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937%28v=vs.85%29.aspx

Another minor issue is that fsblkcnt_t is an unsigned long, and the
return values should be bigint for both the free and the available
space, so we could have incorrect results once we have values higher
than 2^63 - 1, or disk space values higher than 8.39EB = 8.39e6TB if
your prefer, but it's not like we're at this scale yet :)

Thoughts?
--
Michael

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2015-09-07 02:47:32 Re: Getting total and free disk space from paths in PGDATA
Previous Message Jeff Janes 2015-09-06 22:47:17 Re: Too many duplicated condition query return wrong value