def _get_checkpoint_end(self, timeline: int, lsn: int) -> int:
        """Get the end of checkpoint record from WAL.

        .. note::
            The checkpoint record size in WAL depends on postgres major version and platform (memory alignment).
            Hence, the only reliable way to figure out where it ends, is to read the record from file with the
            help of ``pg_waldump`` and parse the output.

            We are trying to read two records, and expect that it will fail to read the second record with message:

                fatal: error in WAL record at 0/182E220: invalid record length at 0/182E298: wanted 24, got 0; or

                fatal: error in WAL record at 0/182E220: invalid record length at 0/182E298: expected at least 24, got 0

            The error message contains information about LSN of the next record, which is exactly where checkpoint ends.

        :param timeline: the checkpoint *timeline* from ``pg_controldata``.
        :param lsn: the checkpoint *location* as :class:`int` from ``pg_controldata``.

        :returns: the end of checkpoint record as :class:`int` or ``0`` if failed to parse ``pg_waldump`` output.
        """
        lsn8 = format_lsn(lsn, True)
        lsn_str = format_lsn(lsn)
        out, err = self._postgresql.waldump(timeline, lsn_str, 2)
        if out is not None and err is not None:
            out = out.decode('utf-8').rstrip().split('\n')
            err = err.decode('utf-8').rstrip().split('\n')
            pattern = 'error in WAL record at {0}: invalid record length at '.format(lsn_str)

            if len(out) == 1 and len(err) == 1 and ', lsn: {0}, prev '.format(lsn8) in out[0] and pattern in err[0]:
                i = err[0].find(pattern) + len(pattern)
                # Message format depends on the major version:
                # * expected at least -- starting from v16
                # * wanted -- before v16
                # * nothing (end of message) 9.5 and older
                # We will simply check all possible combinations.
                for pattern in (': expected at least ', ': wanted ', '\n'):
                    j = (err[0] + '\n').find(pattern, i)
                    if j > -1:
                        try:
                            return parse_lsn(err[0][i:j])
                        except Exception as e:
                            logger.error('Failed to parse lsn %s: %r', err[0][i:j], e)
            logger.error('Failed to parse pg_%sdump output', self._postgresql.wal_name)
            logger.error(' stdout=%s', '\n'.join(out))
            logger.error(' stderr=%s', '\n'.join(err))

        return 0
Get the end of checkpoint record from WAL.

        .. note::
            The checkpoint record size in WAL depends on postgres major version and platform (memory alignment).
            Hence, the only reliable way to figure out where it ends, is to read the record from file with the
            help of ``pg_waldump`` and parse the output.

            We are trying to read two records, and expect that it will fail to read the second record with message:

                fatal: error in WAL record at 0/182E220: invalid record length at 0/182E298: wanted 24, got 0; or

                fatal: error in WAL record at 0/182E220: invalid record length at 0/182E298: expected at least 24, got 0

            The error message contains information about LSN of the next record, which is exactly where checkpoint ends.

        :param timeline: the checkpoint *timeline* from ``pg_controldata``.
        :param lsn: the checkpoint *location* as :class:`int` from ``pg_controldata``.

        :returns: the end of checkpoint record as :class:`int` or ``0`` if failed to parse ``pg_waldump`` output.
        
patroni/postgresql/rewind.py