0

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.

rook
  • 5
  • 2

1 Answers1

0

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).

Laurenz Albe
  • 209,280
  • 17
  • 206
  • 263
  • 1
    Thanks for your answer. In the case I presented, `pg_wal` directory was still not really increasing in size, but by using `psql -c "select pg_switch_wal ();"` along with your recommendation is very rapidly increased disk space consumption. – rook May 17 '22 at 13:49
  • That's an option. Otherwise you have to generate WAL, like I wrote, by running enough `INSERT` statements. – Laurenz Albe May 17 '22 at 14:12