def pg_rewind(self, conn_kwargs: Dict[str, Any]) -> bool:
        """Do pg_rewind.

        .. note::
            If ``pg_rewind`` doesn't support ``--restore-target-wal`` parameter and exited with non zero code,
            Patroni will parse stderr/stdout to figure out if it failed due to a missing WAL file and will
            repeat an attempt after downloading the missing file using ``restore_command``.

        :param conn_kwargs: :class:`dict` object with connection parameters.

        :returns: ``True`` if ``pg_rewind`` finished successfully, ``False`` otherwise.
        """
        # prepare pg_rewind connection string
        env = self._postgresql.config.write_pgpass(conn_kwargs)
        env.update(LANG='C', LC_ALL='C', PGOPTIONS='-c statement_timeout=0')
        dsn = self._postgresql.config.format_dsn({**conn_kwargs, 'password': None})
        logger.info('running pg_rewind from %s', dsn)

        restore_command = (self._postgresql.config.get('recovery_conf') or EMPTY_DICT).get('restore_command') \
            if self._postgresql.major_version < 120000 else self._postgresql.get_guc_value('restore_command')

        # Until v15 pg_rewind expected postgresql.conf to be inside $PGDATA, which is not the case on e.g. Debian
        pg_rewind_can_restore = restore_command and (self._postgresql.major_version >= 150000
                                                     or (self._postgresql.major_version >= 130000
                                                         and self._postgresql.config.config_dir
                                                         == self._postgresql.data_dir))

        cmd = [self._postgresql.pgcommand('pg_rewind')]
        if pg_rewind_can_restore:
            cmd.append('--restore-target-wal')
            if self._postgresql.major_version >= 150000 and\
                    self._postgresql.config.config_dir != self._postgresql.data_dir:
                cmd.append('--config-file={0}'.format(self._postgresql.config.postgresql_conf))

        cmd.extend(['-D', self._postgresql.data_dir, '--source-server', dsn])

        while True:
            results: Dict[str, bytes] = {}
            ret = self._postgresql.cancellable.call(cmd, env=env, communicate=results)

            logger.info('pg_rewind exit code=%s', ret)
            if ret is None:
                return False

            logger.info(' stdout=%s', results['stdout'].decode('utf-8'))
            logger.info(' stderr=%s', results['stderr'].decode('utf-8'))
            if ret == 0:
                return True

            if not restore_command or pg_rewind_can_restore:
                return False

            missing_wal = self._find_missing_wal(results['stderr']) or self._find_missing_wal(results['stdout'])
            if not missing_wal:
                return False

            if not self._fetch_missing_wal(restore_command, missing_wal):
                logger.info('Failed to fetch WAL segment %s required for pg_rewind', missing_wal)
                return False
Do pg_rewind.

        .. note::
            If ``pg_rewind`` doesn't support ``--restore-target-wal`` parameter and exited with non zero code,
            Patroni will parse stderr/stdout to figure out if it failed due to a missing WAL file and will
            repeat an attempt after downloading the missing file using ``restore_command``.

        :param conn_kwargs: :class:`dict` object with connection parameters.

        :returns: ``True`` if ``pg_rewind`` finished successfully, ``False`` otherwise.
        
patroni/postgresql/rewind.py