简体   繁体   中英

How to bloat pg_wal?

For some training purposes, I'd like to reproduce a bloated pg_wal directory so that I can show an example of what happens when Postgres shuts down because of insufficient disk space.

So far I've tried feeding psql the ouptut of the following bash script :

#!/bin/sh
        
for i in {1..100}; do
   echo "create table temp_$i (id text);" >> bloat_wals.sql
   for j in {1..1000}; do
      echo "insert into temp_$i values('`cat /dev/urandom | tr -dc '[:alpha:]' | fold -w ${1:-20} | head -n 1`');" >> bloat_wals.sql
   done
   echo "drop table temp_$i;" >> bloat_wals.sql
done

which generates the following sql file bloat_wals.sql :

create table temp_1 (id text);
insert into temp_1 values('KRzMmncvZzjZlbLTNtQd');
insert into temp_1 values('haXsbsFOUfavkUNeLTSP');
insert into temp_1 values('LYVhfMlWRnfVnhPIvYtb');
...
drop table temp_1;
create table temp_2 (id text);
insert into temp_2 values('qKqRjlCqUddycHENZrQJ');
insert into temp_2 values('SeztNglqJQfWICoyZeCs');
insert into temp_2 values('cgKVcTteWGZXWEHrqTqa');
drop table temp_2;
create table temp_3 (id text);
insert into temp_3 values('OPuidYENyZWfIlsehSvS');
insert into temp_3 values('ZYakjYKzkbwAkjBksKBi');
insert into temp_3 values('oUUBHKMZQeMQejJyvaZL');
...
insert into temp_100 values('SeztNglqJQfWICoyZeCs');
insert into temp_100 values('cgKVcTteWGZXWEHrqTqa');
drop table temp_100;

But this is not increasing the size of the pg_wal directory significantly.

From reading about Postgres wal configuration documentation, it looks like the following parameters/functions could be used to achieve this :

 - checkpoint_timeout
 - max_wal_size
 - pg_switch_wal ()
 - archive_command=/bin/true

Please let me know what would be the easiest way to bloat a pg_wal directory ? I'm using a raw database so any suggestion is welcome.

The simplest way is to configure WAL archiving in a way that is bound to fail:

archive_mode = on
archive_command = '/bin/false'

Then restart PostgreSQL and generate WAL.

Instead of generating a lot of WAL, you could also call the pg_switch_wal() function, which will switch to the next WAL segments (so it acts as if 16MB worth of WAL had been written).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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