Skip to content

[Bug]: xmin on standby cluster replication slots causing heavy io on primary due to permanent autovacuum #4802

Closed

Description

@smiyc

Is there an existing issue already for this bug?

  • I have searched for an existing issue, and could not find anything. I believe this is a new bug.

I have read the troubleshooting guide

  • I have read the troubleshooting guide and I think this is a new bug.

I am running a supported version of CloudNativePG

  • I have read the troubleshooting guide and I think this is a new bug.

Contact Details

[email protected]

Version

1.23.1

What version of Kubernetes are you using?

other (unsupported)

What is your Kubernetes environment?

Other

How did you install the operator?

YAML manifest

What happened?

increasing disk iops / throughput on the primary.
The reason for the problem is an table which is autovacuumed all the time but has a lot of dead tuples which are not removed.
We checked for long running transactions, perpared transcation and the replication slots.

On replication slot is not advancing xmin
keycloak-6 (primary)

# select * from pg_replication_slots ;
┌─[ RECORD 1 ]────────┬──────────────────┐
│ slot_name           │ _cnpg_keycloak_3 │
│ plugin              │ ∅                │
│ slot_type           │ physical         │
│ datoid              │ ∅                │
│ database            │ ∅                │
│ temporary           │ f                │
│ active              │ t                │
│ active_pid          │ 341260           │
│ xmin                │ 91922262         │
│ catalog_xmin        │ ∅                │
│ restart_lsn         │ 8B6/90E3C120     │
│ confirmed_flush_lsn │ ∅                │
│ wal_status          │ reserved         │
│ safe_wal_size       │ 96638615264      │
│ two_phase           │ f                │
│ conflicting         │ ∅                │
├─[ RECORD 2 ]────────┼──────────────────┤
│ slot_name           │ _cnpg_keycloak_5 │
│ plugin              │ ∅                │
│ slot_type           │ physical         │
│ datoid              │ ∅                │
│ database            │ ∅                │
│ temporary           │ f                │
│ active              │ t                │
│ active_pid          │ 23293            │
│ xmin                │ 96148135         │
│ catalog_xmin        │ ∅                │
│ restart_lsn         │ 8B6/90E3C120     │
│ confirmed_flush_lsn │ ∅                │
│ wal_status          │ reserved         │
│ safe_wal_size       │ 96638615264      │
│ two_phase           │ f                │
│ conflicting         │ ∅                │
└─────────────────────┴──────────────────┘

Then we checkt the replicaition slots on the standby clusters
keycloak-5 (standby)

postgres=# select * from pg_replication_slots ;
┌─[ RECORD 1 ]────────┬──────────────────┐
│ slot_name           │ _cnpg_keycloak_3 │
│ plugin              │ ∅                │
│ slot_type           │ physical         │
│ datoid              │ ∅                │
│ database            │ ∅                │
│ temporary           │ f                │
│ active              │ f                │
│ active_pid          │ ∅                │
│ xmin                │ ∅                │
│ catalog_xmin        │ ∅                │
│ restart_lsn         │ 8B6/F9078528     │
│ confirmed_flush_lsn │ ∅                │
│ wal_status          │ reserved         │
│ safe_wal_size       │ 9678608138240    │
│ two_phase           │ f                │
│ conflicting         │ ∅                │
└─────────────────────┴──────────────────┘

keycloak-3 (standby)

postgres=# select * from pg_replication_slots ;
┌─[ RECORD 1 ]────────┬──────────────────┐
│ slot_name           │ _cnpg_keycloak_5 │
│ plugin              │ ∅                │
│ slot_type           │ physical         │
│ datoid              │ ∅                │
│ database            │ ∅                │
│ temporary           │ f                │
│ active              │ f                │
│ active_pid          │ ∅                │
│ xmin                │ 91922262         │
│ catalog_xmin        │ ∅                │
│ restart_lsn         │ 8B6/F9520BD8     │
│ confirmed_flush_lsn │ ∅                │
│ wal_status          │ reserved         │
│ safe_wal_size       │ 9678608138240    │
│ two_phase           │ f                │
│ conflicting         │ ∅                │
└─────────────────────┴──────────────────┘

And saw, that _cnpg_keycloak_5 has xmin set.
Now the question is how to get rid of the xmin, so that vacuum can clenaup the table?
cheers

Cluster resource

Name:         keycloak
Namespace:    cnpg-test
Labels:       release=prometheus-operator
Annotations:  cnpg.io/reloadedAt: 2024-04-28T22:56:59.518845+02:00
              kubectl.kubernetes.io/restartedAt: 2024-04-28T22:58:44+02:00
API Version:  postgresql.cnpg.io/v1
Kind:         Cluster
Metadata:
  Creation Timestamp:  2024-04-18T07:06:22Z
  Generation:          30
  Resource Version:    678335599
  UID:                 b2548d1c-7666-43bb-814a-7cdd7bd48dae
Spec:
  Affinity:
    Enable Pod Anti Affinity:  true
    Pod Anti Affinity Type:    preferred
    Topology Key:              kubernetes.io/hostname
  Backup:
    Barman Object Store:
      Data:
        Immediate Checkpoint:  true
        Jobs:                  4
      Destination Path:        s3://cnpg-test
      Endpoint CA:
        Key:         tls.crt
        Name:        xxx-ca
      Endpoint URL:  https://xxx.net
      s3Credentials:
        Access Key Id:
          Key:   ACCESS_KEY_ID
          Name:  xxx-creds
        Secret Access Key:
          Key:      ACCESS_SECRET_KEY
          Name:     xxx-creds
      Server Name:  keycloak
      Wal:
        Max Parallel:  2
    Retention Policy:  31d
    Target:            prefer-standby
  Bootstrap:
    Initdb:
      Data Checksums:  true
      Database:        app
      Encoding:        UTF8
      Locale C Type:   C
      Locale Collate:  C
      Owner:           app
  Enable PDB:               true
  Enable Superuser Access:  false
  Failover Delay:           0
  Image Name:               xxx/cnpg:16.2
  Image Pull Secrets:
    Name:     docker-registry-repo-xxx-at-auth
  Instances:  3
  Log Level:  info
  Max Sync Replicas:  1
  Min Sync Replicas:  1
  Monitoring:
    Custom Queries Config Map:
      Key:                    queries
      Name:                   cnpg-default-monitoring
    Disable Default Queries:  false
    Enable Pod Monitor:       true
  Node Maintenance Window:
    In Progress:  false
    Reuse PVC:    true
  Postgres GID:   26
  Postgres UID:   26
  Postgresql:
    Parameters:
      archive_mode:                      on
      archive_timeout:                   5min
      auto_explain.log_min_duration:     5000ms
      checkpoint_timeout:                30min
      client_connection_check_interval:  60
      dynamic_shared_memory_type:        posix
      effective_cache_size:              6GB
      effective_io_concurrency:          100
      hot_standby_feedback:              on
      log_destination:                   csvlog
      log_directory:                     /controller/log
      log_executor_stats:                off
      log_filename:                      postgres
      log_line_prefix:                   %m [%p] %u@%d [%a]
      log_lock_waits:                    on
      log_rotation_age:                  0
      log_rotation_size:                 0
      log_statement:                     ddl
      log_timezone:                      Europe/Vienna
      log_truncate_on_rotation:          false
      logging_collector:                 on
      maintenance_work_mem:              512MB
      max_connections:                   400
      max_parallel_workers:              32
      max_replication_slots:             32
      max_slot_wal_keep_size:            90GB
      max_wal_size:                      5120MB
      max_worker_processes:              32
      min_wal_size:                      512MB
      pg_stat_statements.max:            10000
      pg_stat_statements.track:          all
      random_page_cost:                  1.25
      shared_buffers:                    2GB
      shared_memory_type:                mmap
      shared_preload_libraries:          
      ssl_max_protocol_version:          TLSv1.3
      ssl_min_protocol_version:          TLSv1.3
      superuser_reserved_connections:    3
      tcp_keepalives_count:              3
      tcp_keepalives_idle:               60
      tcp_keepalives_interval:           30
      Timezone:                          Europe/Vienna
      track_activities:                  true
      track_counts:                      true
      track_functions:                   pl
      track_io_timing:                   on
      track_wal_io_timing:               true
      wal_keep_size:                     512MB
      wal_level:                         logical
      wal_log_hints:                     on
      wal_receiver_timeout:              5s
      wal_sender_timeout:                5s
      work_mem:                          5242kB
    Sync Replica Election Constraint:
      Enabled:              false
  Primary Update Method:    restart
  Primary Update Strategy:  unsupervised
  Replication Slots:
    High Availability:
      Enabled:      true
      Slot Prefix:  _cnpg_
    Synchronize Replicas:
      Enabled:        true
    Update Interval:  30
  Resources:
    Limits:
      Cpu:     4
      Memory:  8Gi
    Requests:
      Cpu:                 4
      Memory:              8Gi
  Smart Shutdown Timeout:  180
  Start Delay:             3600
  Stop Delay:              1800
  Storage:
    Resize In Use Volumes:  true
    Size:                   350Gi
    Storage Class:          pure-k2-cnpg-test
  Switchover Delay:         3600
  Wal Storage:
    Resize In Use Volumes:  true
    Size:                   100Gi
    Storage Class:          pure-k2-cnpg-test
Status:
  Available Architectures:
    Go Arch:  amd64
    Hash:     94527128605ac5100415106fe26c480531d094b3f36626e562a8135f342b89e4
    Go Arch:  arm64
    Hash:     9b7b08592e917ed3b20bb3ae404ea4c0c958bdee73e5411c452d6c464d77f0b4
  Certificates:
    Client CA Secret:  keycloak-ca
    Expirations:
      Keycloak - Ca:           2024-07-17 07:01:23 +0000 UTC
      Keycloak - Replication:  2024-07-17 07:01:23 +0000 UTC
      Keycloak - Server:       2024-07-17 07:01:23 +0000 UTC
    Replication TLS Secret:    keycloak-replication
    Server Alt DNS Names:
      keycloak-rw
      keycloak-rw.cnpg-test
      keycloak-rw.cnpg-test.svc
      keycloak-r
      keycloak-r.cnpg-test
      keycloak-r.cnpg-test.svc
      keycloak-ro
      keycloak-ro.cnpg-test
      keycloak-ro.cnpg-test.svc
    Server CA Secret:             keycloak-ca
    Server TLS Secret:            keycloak-server
  Cloud Native PG Commit Hash:    336ddf53
  Cloud Native PG Operator Hash:  94527128605ac5100415106fe26c480531d094b3f36626e562a8135f342b89e4
  Conditions:
    Last Transition Time:  2024-06-05T13:54:36Z
    Message:               Cluster is Ready
    Reason:                ClusterIsReady
    Status:                True
    Type:                  Ready
    Last Transition Time:  2024-06-05T05:23:00Z
    Message:               Continuous archiving is working
    Reason:                ContinuousArchivingSuccess
    Status:                True
    Type:                  ContinuousArchiving
    Last Transition Time:  2024-06-05T00:00:02Z
    Message:               exit status 1
    Reason:                LastBackupFailed
    Status:                False
    Type:                  LastBackupSucceeded
  Config Map Resource Version:
    Metrics:
      Cnpg - Default - Monitoring:  660594368
  Current Primary:                  keycloak-6
  Current Primary Timestamp:        2024-06-03T23:22:21.161066Z
  First Recoverability Point:       2024-05-27T05:26:57Z
  First Recoverability Point By Method:
    Barman Object Store:  2024-05-27T05:26:57Z
  Healthy PVC:
    keycloak-3
    keycloak-3-wal
    keycloak-5
    keycloak-5-wal
    keycloak-6
    keycloak-6-wal
  Image:  xxx/cnpg:16.2
  Instance Names:
    keycloak-3
    keycloak-5
    keycloak-6
  Instances:  3
  Instances Reported State:
    keycloak-3:
      Is Primary:    false
      Time Line ID:  7
    keycloak-5:
      Is Primary:    false
      Time Line ID:  7
    keycloak-6:
      Is Primary:    true
      Time Line ID:  7
  Instances Status:
    Healthy:
      keycloak-3
      keycloak-5
      keycloak-6
  Last Failed Backup:      2024-06-05T00:28:55Z
  Last Successful Backup:  2024-06-04T02:51:29Z
  Last Successful Backup By Method:
    Barman Object Store:  2024-06-04T02:51:29Z
  Latest Generated Node:  6
  Managed Roles Status:
    By Status:
      Not - Managed:
        app
      Reconciled:
      Reserved:
        postgres
        streaming_replica
    Password Status:
  Phase:                   Cluster in healthy state
  Pooler Integrations:
    Pg Bouncer Integration:
  Pvc Count:        6
  Read Service:     keycloak-r
  Ready Instances:  3
  Secrets Resource Version:
    Application Secret Version:  492750519
    Barman Endpoint CA:          406859735
    Client Ca Secret Version:    646833786
    Managed Role Secret Version:
    Replication Secret Version:         646833788
    Server Ca Secret Version:           646833786
    Server Secret Version:              646833787
  Switch Replica Cluster Status:
  Target Primary:            keycloak-6
  Target Primary Timestamp:  2024-06-04T01:18:13.980624+02:00
  Timeline ID:               7
  Topology:
    Instances:
      keycloak-3:
      keycloak-5:
      keycloak-6:
    Nodes Used:              3
    Successfully Extracted:  true
  Write Service:             keycloak-rw
Events:                      <none>

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Activity

  1. added
    triage
    on Jun 5, 2024
  2. mnencia commented on Jun 5, 2024

    @mnencia
    Member

    This is an interesting issue. The main on a standby slot should not have been initialized, so it happened when node 3 was primary. AFAIK the only way to clear the xmin field is to delete the replication slot. I think the operator should do it automatically in this case.

  3. added this to the 1.23.2 milestone on Jun 5, 2024
  4. added
    bug 🐛
    and removed
    triage
    on Jun 5, 2024
  5. mnencia commented on Jun 5, 2024

    @mnencia
    Member

    I confirm the bug. If hot_standby_feedback is on, the xmin remains set for the slots on the previous primary, which stops the vacuum. We need to drop the slot and recreate it.

  6. moved this to Todo in Roadmapon Jun 5, 2024
  7. smiyc commented on Jun 5, 2024

    @smiyc
    ContributorAuthor

    great!
    so the workaround in my case is
    select pg_drop_replication_slot('_cnpg_keycloak_5');
    on keycloak-3?

  8. smiyc commented on Jun 5, 2024

    @smiyc
    ContributorAuthor

    I tested on a demo cluster and it worked

    postgres=# select * from pg_replication_slots ;
        slot_name     | plugin | slot_type | datoid | database | temporary | active | active_pid |   xmin   | catalog_xmin | restart_lsn  | confirmed_flush_lsn | wal_status | safe_wal_size | two_phase | conflicting 
    ------------------+--------+-----------+--------+----------+-----------+--------+------------+----------+--------------+--------------+---------------------+------------+---------------+-----------+-------------
     _cnpg_keycloak_5 |        | physical  |        |          | f         | f      |            | 58198033 |              | 824/5A000000 |                     | reserved   | 9048875335680 | f         | 
    (1 row)
    
    
    
    postgres=# select pg_drop_replica
    
    postgres=# select pg_drop_replication_slot('_cnpg_keycloak_5');
     pg_drop_replication_slot 
    --------------------------
     
    (1 row)
    
    postgres=# select * from pg_replication_slots ;
        slot_name     | plugin | slot_type | datoid | database | temporary | active | active_pid | xmin | catalog_xmin | restart_lsn  | confirmed_flush_lsn | wal_status | safe_wal_size | two_phase | conflicting 
    ------------------+--------+-----------+--------+----------+-----------+--------+------------+------+--------------+--------------+---------------------+------------+---------------+-----------+-------------
     _cnpg_keycloak_5 |        | physical  |        |          | f         | f      |            |      |              | 824/5A000000 |                     | reserved   | 9048875335680 | f         | 
    
    
  9. 5 remaining items

  10. moved this from Analysis to Waiting for First Review in CloudNativePG operator developmenton Jun 6, 2024
  11. smiyc commented on Jun 7, 2024

    @smiyc
    ContributorAuthor

    Workaround works!

    2c7e8c56-fecc-4659-9eca-74442f870bb9

    c06e574a-7fdc-47f3-a244-2f0045eb52fe

    90aab908-5b18-4b06-b94a-819b36549b28

    410c2f98-6c8f-48a0-b0f0-0664405d1160

    fe180b89-4c87-48df-8c2a-85e4163f0ad6

  12. moved this from In Progress to Done in Roadmapon Jun 10, 2024
  13. moved this from Waiting for First Review to Done in CloudNativePG operator developmenton Jun 10, 2024
  14. added a commit that references this issue on Jun 10, 2024
    c1fd848
  15. added 2 commits that reference this issue on Jun 11, 2024
    4b2f072
    50aa7b0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug 🐛

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions