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. pdfium去掉v8支持

    GYP_DEFINES='pdf_enable_v8=0 pdf_enable_xfa=0' build/gyp_pdfium 未测试  ???????????

  2. Grafana中mysql作为数据源的配置方法

    需求 近期在使用python写一套模拟API请求的监控项目,考虑数据可视化这方面就采用grafana来呈现,下面来看看怎么弄. 数据源准备 首先安装好mysql,将监控的日志数据写入到mysql之中. ...

  3. 201871010135 张玉晶《面向对象程序设计(java)》第十二周学习总结

      内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/zyja/p/118654 ...

  4. VSCode变换python的调试解释器

    假如一个电脑上有多个Python的环境,想要设置不同的python解释器用于调试. VSCode 的设置,是通过.json的文本来配置的.打开文本的方式: 打开后的文件如下所示: 可以试试“new s ...

  5. LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)

    题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...

  6. 求职-如何选择offer

    如何选择offer呢?下面我们从这几部分一起聊聊: HR问你目前拿到哪几个offer了怎么回答好? 选择小公司还是大公司? 为什么刚入行不要去没有人带的部门? 正式员工.合同工和外包人员有什么区别? ...

  7. vue-quill-editor富文本编辑器,添加了汉化样式却汉化不了

    背景 今天在做后台管理系统时,尝试整合 vue-quill-editor 富文本编辑器,整合完成后,想进行汉化,查阅资料发现,只需自己定义样式替换即可. 原因 当进行汉化时,发现样式并没有替换,汉化失 ...

  8. CSP考前复习

    前言 因为loceaner太菜了,他什么东西都不会 所以他打算学一个东西就记录一下 不过因为他很菜,所以他不会写原理-- 而且,他希望在2019CSP之前不会断更 就酱紫,就是写给他自己的--因为他太 ...

  9. [LeetCode] 350. Intersection of Two Arrays II 两个数组相交之二

    Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1 ...

  10. [LeetCode] 99. Recover Binary Search Tree 复原二叉搜索树

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...