Skip to content

Instantly share code, notes, and snippets.

@sitano
Last active November 16, 2022 16:46
Show Gist options
  • Save sitano/567775c2b6f51035c26644caabc99cb4 to your computer and use it in GitHub Desktop.
Save sitano/567775c2b6f51035c26644caabc99cb4 to your computer and use it in GitHub Desktop.
how to measure amazon rds postgresql replica lag

What to do when replica creation fails stopping streaming, trying to recover WAL from the archive

Calculate XLOG entries distance between master and replica with:

on master:

> select pg_xlogfile_name(pg_current_xlog_flush_location()), pg_current_xlog_insert_location(), pg_current_xlog_location();

on slave:

> select pg_last_xlog_replay_location();

use pg_lsn difference to evaluate lag

> SELECT pg_current_xlog_location() - 'lsn from pg_last_xlog_replay_location() from slave' as diff;

WAL segment file name = timelineId + (uint32)((LSN-1)/(16M*256))  + (uint32)((LSN-1) % 16M)

calculate and fix your WAL tail size / segments kept / slots requirements based on the start lag when rds replica just created.

Link

Useful functions:

|-|-:|-:| | pg_create_restore_point(name text) | pg_lsn | Create a named point for performing restore (restricted to superusers by default, but other users can be granted EXECUTE to run the function) | | pg_current_xlog_flush_location() | pg_lsn | Get current transaction log flush location | | pg_current_xlog_insert_location() | pg_lsn | Get current transaction log insert location | | pg_current_xlog_location() | pg_lsn | Get current transaction log write location | | pg_start_backup(label text [, fast boolean [, exclusive boolean ]]) | pg_lsn | Prepare for performing on-line backup (restricted to superusers by default, but other users can be granted EXECUTE to run the function) | | pg_stop_backup() | pg_lsn | Finish performing exclusive on-line backup (restricted to superusers by default, but other users can be granted EXECUTE to run the function) | | pg_stop_backup(exclusive boolean) | setof record | Finish performing exclusive or non-exclusive on-line backup (restricted to superusers by default, but other users can be granted EXECUTE to run the function) | | pg_is_in_backup() | bool | True if an on-line exclusive backup is still in progress. | | pg_backup_start_time() | timestamp with time zone | Get start time of an on-line exclusive backup in progress. | | pg_switch_xlog() | pg_lsn | Force switch to a new transaction log file (restricted to superusers by default, but other users can be granted EXECUTE to run the function) | | pg_xlogfile_name(location pg_lsn) | text | Convert transaction log location string to file name | | pg_xlogfile_name_offset(location pg_lsn) | text, integer | Convert transaction log location string to file name and decimal byte offset within file | | pg_xlog_location_diff(location pg_lsn, location pg_lsn) | numeric | Calculate the difference between two transaction log locations |

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment