---
- name: Install postgres repo
  ansible.builtin.dnf:
    name: https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
    state: present

- name: Ensure PostgreSQL packages are installed.
  ansible.builtin.dnf:
    name: "{{ postgresql_packages }}"
    state: present
    enablerepo: "{{ postgresql_enablerepo | default(omit, true) }}"
    # Don't let postgresql-contrib cause the /usr/bin/python symlink
    # to be installed, which breaks later Ansible runs on Fedora 30,
    # and affects system behavior in multiple ways.
    exclude: python-unversioned-command

- name: Ensure PostgreSQL Python libraries are installed.
  ansible.builtin.dnf:
    name: "{{ postgresql_python_library }}"
    state: present
    enablerepo: "{{ postgresql_enablerepo | default(omit, true) }}"

- name: Add TimescaleDB repository
  ansible.builtin.yum_repository:
    name: timescale_timescaledb
    description: TimescaleDB Repository
    baseurl: https://packagecloud.io/timescale/timescaledb/el/{{ ansible_distribution_major_version }}/$basearch
    gpgcheck: true
    gpgkey: https://packagecloud.io/timescale/timescaledb/gpgkey
    enabled: true

- name: Install TimescaleDB
  ansible.builtin.dnf:
    name: timescaledb-2-postgresql-{{ postgresql_version }}
    state: present
    enablerepo: timescale_timescaledb
    disable_gpg_check: true
  when: timescaledb_enabled is true

- name: Add kernel parameter to sysctl.d
  ansible.posix.sysctl:
    name: kernel.io_uring_disabled # The parameter name
    value: "0" # The desired value (use strings)
    sysctl_file: /etc/sysctl.d/postgres.conf # Custom drop-in path
    state: present # Ensure it is in the file
    reload: true # Apply to running kernel now
    sysctl_set: true # Verify value with 'sysctl -w'
