Get the current free disk space in PostgreSQL

PostgreSQL获取磁盘空间

from eshizhan

Here has a simple way to get free disk space without any extended language, just define a function using pgsql.

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:

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.

NOTE FOR SECURITY

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.

PROGRAM可以运行任意的命令,所以最好是用固定字符串命令,尽量避免通过参数传入。

How to get the free disk space in PostgreSQL (PostgreSQL获取磁盘空间)的更多相关文章

  1. Disk Space Usage 术语理解:unallocated, unused and reserved

    通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...

  2. [转]Not enough free disk space on disk '/boot'

    Not enough free disk space on disk '/boot' http://my.oschina.net/u/947673/blog/277224 # 解决 出现此情况是因为你 ...

  3. 12 Useful “df” Commands to Check Disk Space in Linux

    On the internet you will find plenty of tools for checking disk space utilization in Linux. However, ...

  4. 14.10.5 Reclaiming Disk Space with TRUNCATE TABLE 回收空间使用TRUNCATE TABLE

    14.10.5 Reclaiming Disk Space with TRUNCATE TABLE 回收空间使用TRUNCATE TABLE 回收操作系统磁盘空间当truncate 一个InnoDB ...

  5. Android Studio模拟器磁盘空间不足(Not enough disk space to run AVD)

    在Android Studio中运行模拟器时,提示Error: Not enough disk space to run AVD '....'. Exiting.是说安装模拟的磁盘空间不足,导致无法运 ...

  6. Ubuntu --- not enough free disk space

    Ubuntu系统更新时出现not enough free disk space. 原因是系统的就内核占满了/boot 的空间,只要将旧内核删除就ok了 首先,命令 uname -r  查看当前内核,( ...

  7. vmware启动虚拟机报错VMware Workstation has paused this virtual machine because the disk on which the virtual machine is stored is almost full. To continue, free an additional 1.4 GB of disk space.

    报错VMware Workstation has paused this virtual machine because the disk on which the virtual machine i ...

  8. 没有磁盘空间 No space left on device

    INSTALL 的解释文件 帮助文件 这里的 pytorch=1.0.1 torchvision=0.2.2 cudatoolkit=9.0,这个ATSS可以运行. 这里最好能够查看一下cuda的版本 ...

  9. No space left on device 解决Linux系统磁盘空间满的办法

    最近Linux电脑在执行mvn时候总是报错: No space left on device   原因是磁盘空间满了,我马上加了20G的硬盘容量,但是还是报错,上网查了一下,发现了解决方法,我用了其中 ...

随机推荐

  1. wpf 窗体添加背景图片

    方法一:xaml中:<控件>   <控件.Background><ImageBrush ImageSource="/WpfApplication1;compon ...

  2. BUUCTF 随便注

    知识点: ##堆叠注入 #预语句注入 https://www.cnblogs.com/0nth3way/articles/7128189.html#autoid-1-0-0 正则过滤了很多关键字导致无 ...

  3. Bacula Plugins

    1. loadPlugin 插件通过加载动态库loadPlugin函数开始,此函数包括bacula的回调和Plugin的注册 bacula的回调 typedef struct s_baculaFunc ...

  4. afnetwork使用

    Usage HTTP Request Operation Manager AFHTTPRequestOperationManager encapsulates the common patterns ...

  5. SQL中的视图(极客时间)

    视图 视图也就是虚拟表, 本身不具备数据, 是SQL中的一个变红要概念. 如图 视图可以帮助我们使用表的一部分, 而不是所有的表, 另一方面可以针对不同的用户制定不同的查询视图. 创建, 更新与删除视 ...

  6. 10.Redis 主从架构

    作者:中华石杉 Redis 主从架构 单机的 redis,能够承载的 QPS 大概就在上万到几万不等.对于缓存来说,一般都是用来支撑读高并发的.因此架构做成主从(master-slave)架构,一主多 ...

  7. windows下git安装过程

    参考廖雪峰博客: https://www.liaoxuefeng.com/wiki/896043488029600/896067074338496 git网站下载程序:   https://git-s ...

  8. 两种方式实现浅拷贝 for in实现和Object.assign({}, 对象1, 对象2);

    浅拷贝只拷贝对象的一层,如果对象的属性还是对象,那么user3和user4这两个对象对应的值都会发生改变 // 拷贝分为浅拷贝和深拷贝. // 浅拷贝的实现 通过for in实现 var user1 ...

  9. springboot-项目获取resources下文件的方法

    spring项目获取resources下文件的方法   最近写读取模板文件做一些后续的处理,将文件放在了项目的resources 下,发现了一个好用的读取方法:   比如上边是你需要读取的文件:  读 ...

  10. openLayers绘制静态底图

    由于项目需要,需要是使用openlayers框架,于是开始安利一波openlayers,可以点击 https://openlayers.org/   进入他的官网下载相关资源和案例 学习的过程总是慢慢 ...