简体   繁体   English

Postgres 12 - 如何减小 wal 目录的大小

[英]Postgres 12 - how to reduce size of wal-directory

I have upgraded postgres 9 to 12 with pgdump and restore it to new database.我已经使用 pgdump 将 postgres 9 升级到 12,并将其恢复到新数据库。 All seems to be ok.一切似乎都还好。 Afterwards I noticed, that size of../12/main/ has grown a lot and most of it is in pg_wal-directory.后来我注意到,../12/main/ 的大小已经增长了很多,而且大部分都在 pg_wal-directory 中。 Size of./base is 27GB and size of./pg_wal is 61GB. ./base 的大小为 27GB,./pg_wal 的大小为 61GB。 On postgres 9 values were 31GB / 1GB (pg_xlog).在 postgres 9 上,值为 31GB / 1GB (pg_xlog)。

After some checking, postgres.conf-file shows default values as:经过一些检查后,postgres.conf 文件显示默认值如下:

# max_wal_size = 64GB 
min_wal_size = 5GB

I change value of max_wal_size to 4GB and removed comment我将 max_wal_size 的值更改为 4GB 并删除了评论

SHOW ALL;

told告诉

max_wal_size | 4GB 

But size of pg_wal stays on 61GB.但是 pg_wal 的大小保持在 61GB。 Is there some other parameter, I should change?还有其他一些参数,我应该改变吗?

The default value is 1GB – you must be using a modified version of PostgreSQL.默认值为 1GB – 您必须使用 PostgreSQL 的修改版本。

I would first check pg_stat_archiver and the log file if there are problems archiving WAL segments.如果归档 WAL 段有问题,我会首先检查pg_stat_archiver和日志文件。 Then I would check for stale replication slots.然后我会检查过时的复制槽。 Finally, I would check the setting of wal_keep_segments .最后,我会检查wal_keep_segments的设置。

If all three don't account for the size, you just have to wait: WAL segments are removed at every checkpoint.如果这三个都不考虑大小,您只需要等待:WAL 段在每个检查点都被删除。 The more activity there is, the faster WAL segments will get filled and deleted.活动越多,填充和删除 WAL 段的速度就越快。

Following on from jjanes' answer...继jjanes的回答之后......

If you have a server with very low traffic, a development server for example, have done something like a data import which generates a lot of writes, and so have a large number of wal segment files, the amount of storage consumed by wal segments may grow up to max_wal_size.如果你有一个流量非常低的服务器,例如一个开发服务器,做了一些像数据导入这样的事情会产生大量的写入,所以有大量的 wal 段文件,wal 段消耗的存储量可能长大到 max_wal_size。 If you reduce max_wal_size in the server config, the actual number of wal segment files won't drop until the server cycles through those old files.如果您减少服务器配置中的 max_wal_size,则 wal 段文件的实际数量不会下降,直到服务器循环遍历那些旧文件。

Here's a function which will force the server to cycle through all of the existing segment files.这是一个 function,它将强制服务器循环遍历所有现有的段文件。 It does this by repeatedly calling pg_switch_wal() to switch to a new wal segment and then checkpoint to force writing a checkpoint on the new wal segment.它通过重复调用 pg_switch_wal() 来切换到一个新的 wal 段然后检查点以强制在新的 wal 段上写入一个检查点来做到这一点。

In the scenario described this will trigger postgres to reduce the number of segment files down to the current max_wal_size and possibly below.在所描述的场景中,这将触发 postgres 将段文件的数量减少到当前的 max_wal_size 甚至可能更低。

This may be counterproductive on a live server, particularly if you are doing wal archiving, so please only use this if your situation matches the description above.这在实时服务器上可能会适得其反,特别是如果您正在做 wal 归档,所以请仅在您的情况符合上述描述时才使用它。

Depending on how many wal segments you have, this may take some time to complete.根据您有多少个 wal 段,这可能需要一些时间才能完成。

create or replace function pg_wal_cycle_all() 
returns int language plpgsql 
as $$
declare
    wal_count int;
    wal_seg varchar;
begin 
    select count(*) - 1 
    into wal_count 
    from pg_ls_dir('pg_wal');

    for wal in 1..wal_count loop 
        select pg_walfile_name(pg_switch_wal()) into wal_seg;
        raise notice 'segment %', wal_seg;
        checkpoint;
    end loop;
    return wal_count;
end;$$;

Execute with:执行:

select pg_wal_cycle_all();

It seems like at some point, the server must have actually been running with max_wal_size = 64GB , probably during the initial import.似乎在某些时候,服务器实际上必须以max_wal_size = 64GB运行,可能是在初始导入期间。 If it were undergoing intense activity, it could have recycled most of the wal files for future use, thinking it will need them very soon based on high recent activity.如果它正在进行激烈的活动,它可能会回收大部分 wal 文件以供将来使用,并认为根据最近的高活动它很快就会需要它们。 Once recycled, the files are never removed until after they are used.一旦回收,文件在使用之前永远不会被删除。 If the server has low activity once the initial import is done, it could take a very long time to use that amount of WAL files.如果服务器在初始导入完成后活动较少,则可能需要很长时间才能使用该数量的 WAL 文件。 You could check to see how many of the files are recycled for future use by a query like this:您可以通过如下查询查看有多少文件被回收以备将来使用:

select count(*) as all, 
    count(*) filter (where filename > pg_walfile_name(pg_current_wal_lsn())) as future 
from pg_ls_dir('pg_wal') as t(filename);

If you are desperate to get that space back, you could very very carefully delete the files with names which are the furthest in the future.如果您迫切希望收回该空间,您可以非常非常小心地删除名称在未来最远的文件。 Deleting the wrong ones though would thoroughly destroy your database.删除错误的将彻底破坏您的数据库。 If you don't really need the space, it is safest to just let them alone, and they will slowly be re-used and deleted naturally over time.如果你真的不需要这个空间,最安全的方法就是让他们自己呆着,随着时间的推移,他们会慢慢地被重新使用和自然删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM