如何限制 pg_dump 使用的资源?

qui*_*lur 6 postgresql memory pg-dump postgresql-9.4

是否可以限制 所使用的系统资源pg_dump

我有一个使用PostgreSQL数据库的资源相当密集的服务器。它正在运行FreeBSD 10.2。服务器有 8 GB 内存。

我使用以下命令运行备份。

pg_dump -h <address> -U <user> -Fc <database> -f <location>
Run Code Online (Sandbox Code Playgroud)

当备份完成时,相关的 postgres 进程的内存使用量不断上升。仅该进程就达到了 2.2 GB。此时内存几乎已用完并FreeBSD开始杀死进程。

我尝试更改备份的完成方式。例如,我尝试使用多个作业(因此将格式更改为目录)。

pg_dump -j 4 -h <address> -U <user> -Fd <database> -f <location>
Run Code Online (Sandbox Code Playgroud)

没有差异。最终,内存使用量将达到上限并FreeBSD终止数据库备份(以及其他进程)。

我还尝试调整一些系统变量。有管理内核资源资源消耗。这些影响了服务器使用的正在运行的 postgres 进程,但对进程使用没有影响pg_dump。在 FreeBSD 开始终止进程​​之前,它仍然上升到 2.2 GB。

我已经通读了pg_dump手册,除了多项作业之外,没有真正看到任何有帮助的内容。

此时我不太确定该怎么办。pg_dump有没有办法限制允许使用的资源?我不介意备份是否慢一些。只是不要在系统资源耗尽时操作系统必须开始终止进程​​。

Eva*_*oll 4

我的假设是 RAM 不是你的问题。但是,您的 IO 陷入困境,以至于您无法像从数据库中读取数据那样快地写入文件。

这里没有解决方案,pg_dump只能让它尽可能快地工作。如果读取数据的速度比写入数据的速度快,则您的内存使用量可能会增加。直到表刷新到磁盘。您可能想要启用压缩,请查看man psql并查找-Z

   -Z 0..9
   --compress=0..9
       Specify the compression level to use. Zero means no compression. For the custom archive format, this specifies compression of individual table-data
       segments, and the default is to compress at a moderate level. For plain text output, setting a nonzero compression level causes the entire output file
       to be compressed, as though it had been fed through gzip; but the default is not to compress. The tar archive format currently does not support
       compression at all.
Run Code Online (Sandbox Code Playgroud)

据我所知,进程不知道系统有多少内存或应该占用多少内存。通常,设置限制只会执​​行此处发生的操作并终止进程。如果它没有杀死进程,它们的进程可能会继续喝内存,直到它开始分页,然后再继续。