PG_RESETWAL(1)

🌸 PG_RESETWAL(1) β€” PostgreSQL 18.6 Documentation 🌸

okay bestie, sit DOWN. this is pg_resetwal, aka the literal last-resort crowbar of the PostgreSQL world πŸ’… it's from the PostgreSQL 18.6 Documentation, section 1, and it's the tool you pray you never need. but if you need it, you NEED it. so let's go 🫢


✨ NAME ✨

pg_resetwal β€” reset the write-ahead log and other control information of a PostgreSQL database cluster πŸ”₯


πŸŽ€ SYNOPSIS πŸŽ€

pg_resetwal [-f | --force] [-n | --dry-run] [option...] [-D |
            --pgdata]datadir

πŸ’– DESCRIPTION πŸ’–

so pg_resetwal clears the write-ahead log (WAL) and, if you want, resets some other control information living in the pg_control file. this whole thing is sometimes the only move if those files got corrupted. and it is strictly a last resort β€” like, only when the server refuses to start because of that corruption. no cap 🚨

some options β€” --wal-segsize, for example (peek below πŸ‘€) β€” can also be used to tweak certain global settings of a database cluster without having to rerun initdb. and that part is actually safe to do on an otherwise healthy cluster, as long as you don't touch any of the dangerous modes mentioned below. so like, one of these is not like the others πŸ’…

now for the important bit. if you run pg_resetwal on a data directory where the server was cleanly shut down and the control file is sound, it will have no effect on the contents of your database system β€” except that no-longer-used WAL files get cleared away. literally that's it. any other use is potentially dangerous and must be done with great care 😭 and pg_resetwal will demand the -f (force) option before it will work on a data directory that's in an unclean shutdown state or that has a corrupted control file. it's giving "are you SURE bestie" energy.

after running this command on a data directory with corrupted WAL or a corrupted control file, it should be possible to start the server β€” but bear in mind the database might contain inconsistent data, because of partially-committed transactions πŸ™ˆ so: you should immediately dump your data, run initdb, and restore. after restore, check for inconsistencies and repair as needed. do not skip steps, i'm begging.

if pg_resetwal complains that it cannot determine valid data for pg_control, you can force it to proceed anyway by specifying the -f (force) option. in that case, plausible values will be substituted for the missing data. most of the fields can be expected to match, but manual assistance might be needed for these ones:

  • the next OID
  • the next transaction ID and epoch
  • the next multitransaction ID and offset
  • the WAL starting location fields

you can set all of those with the options discussed below πŸ‘‡ and if you can't determine correct values for all of them, -f can still be used β€” but the recovered database must be treated with even more suspicion than usual: an immediate dump and restore is imperative. do NOT execute any data-modifying operations in the database before you dump, because any such action is likely to make the corruption worse πŸ’€ seriously.

and one more thing: this utility can only be run by the user who installed the server, because it requires read/write access to the data directory. sorry, permissions are permissions 🀷


✨ OPTIONS ✨

  • datadir, -D datadir, --pgdata=datadir β€” tells it where the database directory lives. for safety reasons you must specify the data directory right there on the command line. pg_resetwal does not use the environment variable PGDATA. no shortcuts, bestie.
  • -f, --force β€” forces pg_resetwal to proceed even in situations where it could be dangerous, as explained above. specifically, this option is required to proceed if the server had not been cleanly shut down, or if pg_resetwal cannot determine valid data for pg_control.
  • -n, --dry-run β€” this one tells pg_resetwal to print the values reconstructed from pg_control and the values about to be changed, and then exit without modifying anything. it's mainly a debugging tool, but it's also lowkey useful as a sanity check before you let pg_resetwal go for real. vibe check first, chaos later πŸ¦‹
  • -V, --version β€” display version information, then exit.
  • -?, --help β€” show help, then exit.

the following options are only needed when pg_resetwal is unable to determine appropriate values by reading pg_control. safe values can be determined as described below. for values that take numeric arguments, hexadecimal values can be specified by using the prefix 0x. note that these instructions only apply with the standard block size of 8 kB ✨

  • -c xid,xid, --commit-timestamp-ids=xid,xid β€” manually set the oldest and newest transaction IDs for which the commit time can be retrieved.

a safe value for the oldest transaction ID for which the commit time can be retrieved (the first part) can be determined by looking for the numerically smallest file name in the directory pg_commit_ts under the data directory. conversely, a safe value for the newest transaction ID for which the commit time can be retrieved (the second part) can be determined by looking for the numerically greatest file name in that same directory. the file names are in hexadecimal.

  • -e xid_epoch, --epoch=xid_epoch β€” manually set the next transaction ID's epoch.

the transaction ID epoch is not actually stored anywhere in the database except in the field that is set by pg_resetwal, so any value will work as far as the database itself is concerned. you might need to adjust this value to make sure replication systems such as Slony-I and Skytools work correctly β€” and if so, an appropriate value should be obtainable from the state of the downstream replicated database.

  • -l walfile, --next-wal-file=walfile β€” manually set the WAL starting location by specifying the name of the next WAL segment file.

the name of the next WAL segment file should be larger than any WAL segment file name currently existing in the directory pg_wal under the data directory. these names are also in hexadecimal and have three parts. the first part is the "timeline ID" and should usually be kept the same. for example, if 00000001000000320000004A is the largest entry in pg_wal, use -l 00000001000000320000004B or higher.

note that when using nondefault WAL segment sizes, the numbers in the WAL file names are different from the LSNs that are reported by system functions and system views. this option takes a WAL file name, not an LSN.

Note πŸ“Œ pg_resetwal itself looks at the files in pg_wal and chooses a default -l setting beyond the last existing file name. therefore, manual adjustment of -l should only be needed if you are aware of WAL segment files that are not currently present in pg_wal β€” like entries in an offline archive β€” or if the contents of pg_wal have been lost entirely. 😭
  • -m mxid,mxid, --multixact-ids=mxid,mxid β€” manually set the next and oldest multitransaction ID.

a safe value for the next multitransaction ID (the first part) can be determined by looking for the numerically largest file name in the directory pg_multixact/offsets under the data directory, adding one, and then multiplying by 65536 (0x10000). conversely, a safe value for the oldest multitransaction ID (the second part of -m) can be determined by looking for the numerically smallest file name in that same directory and multiplying by 65536. the file names are in hexadecimal, so the easiest way to do this is to specify the option value in hexadecimal and append four zeroes ✨

  • -o oid, --next-oid=oid β€” manually set the next OID.

there is no comparably easy way to determine a next OID that's beyond the largest one in the database β€” but fortunately, getting the next-OID setting right is not critical πŸ™Œ

  • -O mxoff, --multixact-offset=mxoff β€” manually set the next multitransaction offset.

a safe value can be determined by looking for the numerically largest file name in the directory pg_multixact/members under the data directory, adding one, and then multiplying by 52352 (0xCC80). the file names are in hexadecimal. there is no simple recipe like the ones for other options of appending zeroes. you're on your own for this one πŸ’€

  • -u xid, --oldest-transaction-id=xid β€” manually set the oldest unfrozen transaction ID.

a safe value can be determined by looking for the numerically smallest file name in the directory pg_xact under the data directory and then multiplying by 1048576 (0x100000). note that the file names are in hexadecimal. it's usually easiest to specify the option value in hexadecimal too. for example, if 0007 is the smallest entry in pg_xact, -u 0x700000 will work (five trailing zeroes provide the proper multiplier). slay πŸŽ€

  • -x xid, --next-transaction-id=xid β€” manually set the next transaction ID.

a safe value can be determined by looking for the numerically largest file name in the directory pg_xact under the data directory, adding one, and then multiplying by 1048576 (0x100000). note that the file names are in hexadecimal. it's usually easiest to specify the option value in hexadecimal too. for example, if 0011 is the largest entry in pg_xact, -x 0x1200000 will work (five trailing zeroes provide the proper multiplier). same energy, different direction πŸ’…

  • --char-signedness=option β€” manually set the default char signedness. possible values are signed and unsigned.

for a database cluster that pg_upgrade upgraded from a PostgreSQL version before 18, the safe value would be the default char signedness of the platform that ran the cluster before that upgrade. for all other clusters, signed would be the safe value. however, this option is exclusively for use with pg_upgrade and should not normally be used manually. hands off, bestie πŸ™ˆ

  • --wal-segsize=wal_segment_size β€” set the new WAL segment size, in megabytes. the value must be set to a power of 2 between 1 and 1024 (megabytes). see the same option of initdb(1) for more information.

this option can also be used to change the WAL segment size of an existing database cluster, which means you get to avoid the whole re-initdb situation πŸ‘‘

Note πŸ“Œ while pg_resetwal will set the WAL starting address beyond the latest existing WAL segment file, some segment size changes can cause previous WAL file names to be reused. it is recommended to use -l together with this option to manually set the WAL starting address if WAL file name overlap will cause problems with your archiving strategy. 🚨

🌈 ENVIRONMENT 🌈

  • PG_COLOR β€” specifies whether to use color in diagnostic messages. possible values are always, auto and never.

πŸ’œ NOTES πŸ’œ

this command must not be used when the server is running. pg_resetwal will refuse to start up if it finds a server lock file in the data directory. if the server crashed, then a lock file might have been left behind β€” in that case you can remove the lock file to allow pg_resetwal to run. but before you do so, make doubly certain that there is no server process still alive. i mean it. doubly. πŸ’€

pg_resetwal works only with servers of the same major version.


✨ SEE ALSO ✨

okay so bestie, if you wanna peek at the other one in this lil duo, it's literally right here:

  • pg_controldata(1) β€” πŸ’– the little tool that shows you all the control file info, aka the receipts on what state your cluster thinks it's in. Go read it, it's giving informed decisions.
🌸 fr fr, go look at pg_controldata(1) before you mess with anything. Knowing is half the battle and the other half is not panicking at 3am. 🫢

and that's the whole page, babes πŸ’… you read all of it, every last flag and warning, and honestly? slay. πŸ‘‘

this lil masterpiece is from PostgreSQL 18.6, dated 2026 β€” yes, we're being very specific about that, iykyk. πŸ’–

now go forth and back up your cluster first, bestie. no cap. πŸ¦‹βœ¨