---
#--------------------------------------------------------------#
# Remove Follower                                   [pg_replica]
#--------------------------------------------------------------#
# remove followers first to avoid unnecessary failover
# especially when removing entire cluster
- name: remove postgres replica
  tags: pg_replica
  become: true
  when: pg_role != 'primary'
  block:

    - name: stop postgres replica services with timeout
      shell: |
        # stop services with timeout, then force kill if needed
        timeout 30s systemctl stop patroni || systemctl kill patroni
        timeout 15s systemctl stop postgres || systemctl kill postgres
        systemctl disable patroni postgres
        systemctl daemon-reload
        
        # force shutdown postgres processes
        if ps -u {{ pg_dbsu }} -o pid:1,command | grep -E 'postmaster|postgres:|-D' ; then
            {{ pg_bin_dir }}/pg_ctl -D {{ pg_data }} stop -m immediate || /bin/true
        fi
        
        # final cleanup: kill any remaining postgres processes
        sleep 2
        if ps -u {{ pg_dbsu }} -o pid:1,command | grep -E 'postmaster|postgres:|-D' ; then
            ps -u {{ pg_dbsu }} -o pid:1,command | grep -E 'postmaster|postgres:|-D' | awk '{print $1}' | xargs -r kill -9
        fi
      args: { executable: /bin/bash }

#--------------------------------------------------------------#
# Remove Leader                                     [pg_primary]
#--------------------------------------------------------------#
# remove primary last to avoid unnecessary failover
# we will try gentle shutdown with a 30s timeout first
# then shutdown remaining processes with brute force
- name: remove postgres primary
  tags: pg_primary
  become: true
  when: pg_role == 'primary'
  block:

    - name: stop postgres primary services with timeout
      shell: |
        # stop services with timeout, then force kill if needed
        timeout 30s systemctl stop patroni || systemctl kill patroni
        timeout 15s systemctl stop postgres || systemctl kill postgres
        systemctl disable patroni
        systemctl disable postgres
        systemctl daemon-reload
        
        # force shutdown postgres processes
        if ps -u {{ pg_dbsu }} -o pid:1,command | grep -E 'postmaster|postgres:|-D' ; then
            {{ pg_bin_dir }}/pg_ctl -D {{ pg_data }} stop -m immediate || /bin/true
        fi
        
        # final cleanup: kill any remaining postgres processes
        sleep 2
        if ps -u {{ pg_dbsu }} -o pid:1,command | grep -E 'postmaster|postgres:|-D' ; then
            ps -u {{ pg_dbsu }} -o pid:1,command | grep -E 'postmaster|postgres:|-D' | awk '{print $1}' | xargs -r kill -9
        fi
      args: { executable: /bin/bash }

#--------------------------------------------------------------#
# Remove ETCD Meta Data                                [pg_meta]
#--------------------------------------------------------------#
# when cleanup primary, remove etcd metadata, too.
# delegate to first etcd node (which has etcdctl and certs)
- name: remove postgres metadata from etcd
  tags: pg_meta
  when:
    - pg_role == 'primary'
    - pg_cluster != ''
    - pg_cluster != 'root'  # fail if you choose 'root' as cluster name
    - groups['etcd'] is defined
    - groups['etcd'] | length > 0
  delegate_to: '{{ groups["etcd"][0] }}'
  become: true
  environment:
    ETCDCTL_ENDPOINTS: "{{ groups['etcd'] | map('regex_replace', '^(.*)$', 'https://\\1:' + etcd_port|string) | join(',') }}"
    ETCDCTL_CACERT: "/etc/etcd/ca.crt"
    ETCDCTL_CERT: "/etc/etcd/server.crt"
    ETCDCTL_KEY: "/etc/etcd/server.key"
    ETCDCTL_USER: "root:{{ etcd_root_password }}"
  vars:
    pg_meta_prefix: "{% if pg_mode == 'citus' %}{{ pg_namespace }}/{{ pg_shard }}/{{ pg_group }}{% else %}{{ pg_namespace }}/{{ pg_cluster }}{% endif %}"
  shell: |
    etcdctl del "{{ pg_meta_prefix }}" --prefix=true
  args: { executable: /bin/bash }
  no_log: true
...
