### Install CloudNativePG Database Cluster Source: https://github.com/cloudnative-pg/charts/blob/main/README.md Installs a CloudNativePG database cluster. Ensure the Helm repository 'cnpg' is added. ```bash helm repo add cnpg https://cloudnative-pg.github.io/charts helm upgrade --install database \ --namespace database \ --create-namespace \ cnpg/cluster ``` -------------------------------- ### Scheduled Backup Configuration Example Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/README.md Example of configuring a daily scheduled backup using Barman. The schedule uses crontab format. ```yaml backups: scheduledBackups: - name: daily-backup schedule: "0 0 0 * * *" # Daily at midnight backupOwnerReference: self ``` -------------------------------- ### Install Barman Cloud CNPG-I Plugin Source: https://github.com/cloudnative-pg/charts/blob/main/README.md Installs the CNPG-I Barman Cloud Plugin. This requires a pre-existing cert-manager installation. Ensure the Helm repository 'cnpg' is added. ```bash helm repo add cnpg https://cloudnative-pg.github.io/charts helm upgrade --install plugin-barman-cloud \ --namespace cnpg-system \ cnpg/plugin-barman-cloud ``` -------------------------------- ### Install CloudNativePG Cluster Chart Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/README.md Installs or upgrades the CloudNativePG cluster chart. This requires the operator to be installed and a values.yaml file for configuration. ```bash helm repo add cnpg https://cloudnative-pg.github.io/charts helm upgrade --install cnpg \ --namespace cnpg-database \ --create-namespace \ --values values.yaml \ cnpg/cluster ``` -------------------------------- ### Install CloudNativePG Operator Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/README.md Installs or upgrades the CloudNativePG operator in the cluster. Ensure the Helm repository is added first. ```bash helm repo add cnpg https://cloudnative-pg.github.io/charts helm upgrade --install cnpg \ --namespace cnpg-system \ --create-namespace \ cnpg/cloudnative-pg ``` -------------------------------- ### Install CloudNativePG Operator (Single Namespace) Source: https://github.com/cloudnative-pg/charts/blob/main/README.md Installs the CloudNativePG operator restricted to a single namespace for enhanced security. This mode cannot coexist with a cluster-wide operator. Ensure the Helm repository 'cnpg' is added. ```bash helm upgrade --install cnpg \ --namespace cnpg-system \ --create-namespace \ --set config.clusterWide=false \ cnpg/cloudnative-pg ``` -------------------------------- ### Manual Worker Management Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Manually reload configuration or force a subscription worker to start. ```sql -- If workers aren't starting automatically SELECT pg_reload_conf(); -- Force subscription to start worker ALTER SUBSCRIPTION subscription_name ENABLE; ALTER SUBSCRIPTION subscription_name REFRESH PUBLICATION; ``` -------------------------------- ### Install CloudNativePG Operator (Cluster-Wide) Source: https://github.com/cloudnative-pg/charts/blob/main/README.md Installs the CloudNativePG operator in a cluster-wide mode. Ensure the Helm repository 'cnpg' is added before running this command. ```bash helm repo add cnpg https://cloudnative-pg.github.io/charts helm upgrade --install cnpg \ --namespace cnpg-system \ --create-namespace \ cnpg/cloudnative-pg ``` -------------------------------- ### Configure Scheduled Backups in YAML Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/Getting Started.md Example of configuring a daily scheduled backup for a CloudNativePG cluster. This configuration should be part of your cluster's YAML definition. ```yaml backups: scheduledBackups: - name: daily-backup schedule: "0 0 0 * * *" # Daily at midnight backupOwnerReference: self ``` -------------------------------- ### Helm Installation/Upgrade Message Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt This snippet displays a success message indicating whether the cluster has been installed or upgraded. ```gohtml {{ if .Release.IsInstall }} The {{ include "cluster.color-info" (include "cluster.fullname" .) }} has been installed successfully. {{ else if .Release.IsUpgrade }} The {{ include "cluster.color-info" (include "cluster.fullname" .) }} has been upgraded successfully. {{ end }} ``` -------------------------------- ### Get All Base Backups Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt Command to retrieve a list of all base backups for a given cluster, filtered by a selector. ```bash kubectl --namespace {{ .Release.Namespace }} get backups --selector cnpg.io/cluster={{ include "cluster.fullname" . }} ``` -------------------------------- ### Get Nodes with Zone Labels Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterZoneSpreadWarning.md This command lists all nodes and includes the 'topology.kubernetes.io/zone' label, which is essential for understanding pod distribution across availability zones. ```bash kubectl get nodes --label-columns topology.kubernetes.io/zone ``` -------------------------------- ### Start a new screen session Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/Console.md Initiates a new detached screen session named 'mysession'. This allows commands to continue running even after disconnecting from the console pod. ```bash screen -S mysession ``` -------------------------------- ### Get Current Chart and App Versions Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Retrieves the current chart version and the associated CloudNativePG application version from the `Chart.yaml` file using `yq`. ```bash OLD_VERSION=$(yq -r '.version' charts/cloudnative-pg/Chart.yaml) OLD_CNPG_VERSION=$(yq -r '.appVersion' charts/cloudnative-pg/Chart.yaml) echo Old chart version: $OLD_VERSION echo Old CNPG version: $OLD_CNPG_VERSION ``` -------------------------------- ### Quick Reference Commands Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Common commands for managing and monitoring subscriptions and clusters. ```bash # Check subscription status kubectl exec -it svc/CLUSTER-rw -n NS -- psql -c "SELECT * FROM pg_stat_subscription;" # Enable subscription kubectl exec -it svc/CLUSTER-rw -n NS -- psql -c "ALTER SUBSCRIPTION sub_name ENABLE;" # Restart subscription kubectl cnpg subscription restart sub_name -n NS # Restart cluster kubectl cnpg restart CLUSTER -n NS # Check replication slots kubectl exec -it svc/PUBLISHER-rw -n NS -- psql -c "SELECT * FROM pg_replication_slots;" # Check workers kubectl exec -it svc/CLUSTER-rw -n NS -- psql -c "SELECT * FROM pg_stat_activity WHERE backend_type = 'logical replication worker';" ``` -------------------------------- ### Check and Create Tables Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Verify table existence and perform schema migration from publisher to subscriber. ```sql -- On subscriber, ensure tables exist SELECT tablename FROM pg_tables WHERE schemaname = 'public'; ``` ```sql -- Export schema from publisher pg_dump -h PUBLISHER-HOST -U postgres -s -t table_name database_name -- Import into subscriber psql -h SUBSCRIBER-HOST -U postgres -d database_name < schema_dump.sql ``` -------------------------------- ### Generate Documentation and Schema Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Regenerates the project's documentation and values schema using the `make` command. ```bash make docs schema ``` -------------------------------- ### Configuration Summary Table Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt Displays a formatted table summarizing the cluster's configuration, including mode, type, image, instances, backups, and storage. ```gohtml ╭───────────────────┬──────────────────────────────────────────────────────────╮ │ Configuration │ Value │ ┝━━━━━━━━━━━━━━━━━━━┿━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┥ │ Cluster mode │ {{ printf "% -56s" $mode }} │ │ Type │ {{ printf "% -56s" .Values.type }} │ │ Image │ {{ include "cluster.color-info" (printf "% -56s" $image) }} │ {{- if eq .Values.mode "recovery" }} │ Source │ {{ printf "% -56s" $source }} │ {{- end }} │ Instances │ {{ include (printf "%s%s" "cluster.color-" $redundancyColor) (printf "% -56s" (toString .Values.cluster.instances)) }} │ │ Backups │ {{ include (printf "%s%s" "cluster.color-" (ternary "ok" "error" .Values.backups.enabled)) (printf "% -56s" (ternary "Enabled" "Disabled" .Values.backups.enabled)) }} │ {{- if .Values.backups.enabled }} │ Backup Provider │ {{ printf "% -56s" (title .Values.backups.provider) }} │ │ Scheduled Backups │ {{ printf "% -56s" $scheduledBackups }} │ {{- end }} │ Storage │ {{ printf "% -56s" .Values.cluster.storage.size }} │ ``` -------------------------------- ### Check Resource Usage and Disk I/O Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterPhysicalReplicationLag.md Monitor pod resource consumption and disk I/O performance to identify bottlenecks. ```bash kubectl top pods -n -l cnpg.io/podRole=instance kubectl exec -it -- iostat -x 1 5 ``` -------------------------------- ### Verify Subscription Details Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Retrieves configuration details for existing subscriptions. ```bash kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT subname, srconninfo, srsynccommit, srslotname, srsyncstate as sync_state FROM pg_subscription; " ``` -------------------------------- ### Connect to the Console Pod Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/Console.md Use this command to establish an interactive bash session within the console pod. Replace `` and `` with your specific values. ```bash kubectl --namespace exec --stdin --tty statefulset/-console -- bash ``` -------------------------------- ### Monitor and Manage Replication via CLI Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationLagging.md Commands to inspect replication slot lag, enable subscriptions, and view detailed subscription status. ```bash # Check replication slot status kubectl exec -it svc/PUBLISHER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT slot_name, active, pg_size_pretty(pg_wal_lsn_diff(pg_current_wal_lsn(), restart_lsn)) as lag_bytes FROM pg_replication_slots WHERE slot_type = 'logical'; " # Force sync (if needed) kubectl cnpg subscription enable SUBSCRIPTION-NAME -n NAMESPACE # Check subscription details kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c "\dRs+" ``` -------------------------------- ### Test Connectivity and Configure Timeouts Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Verify network reachability and adjust subscription parameters for slow networks. ```bash # Test connection from subscriber to publisher kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- \ psql -h PUBLISHER-HOST -U postgres -d database_name -c "SELECT 1;" ``` ```yaml # In subscription configuration spec: parameters: application_name: "my_subscription" synchronous_commit: "off" # Increase timeout for slow networks ``` -------------------------------- ### Check System Resources Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Monitor pod status, resource constraints, and node health. ```bash # Check for OOM kills or resource constraints kubectl describe pod -n NAMESPACE POD-NAME # Check if the pod was restarted kubectl get pods -n NAMESPACE -l app=postgresql # Check node resources kubectl top nodes ``` -------------------------------- ### Connect to Primary Instance Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt Command to connect to the primary read-write instance of the PostgreSQL cluster using `kubectl exec`. ```bash kubectl --namespace {{ .Release.Namespace }} exec --stdin --tty services/{{ include "cluster.fullname" . }}-rw -- bash ``` -------------------------------- ### Enforce Production Readiness Warnings Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt Conditional logic to display warnings when backups are disabled or instance counts are insufficient for high availability. ```Helm {{ if not .Values.backups.enabled }} {{- include "cluster.color-error" "Warning! Backups not enabled. Recovery will not be possible! Do not use this configuration in production.\n" }} {{ end -}} {{ if lt (int .Values.cluster.instances) 2 }} {{- include "cluster.color-error" "Warning! Instance failure will lead to downtime and/or data loss!\n" }} {{- else if lt (int .Values.cluster.instances) 3 -}} {{- include "cluster.color-warning" "Warning! Single instance redundancy available only. Instance failure will put the cluster at risk.\n" }} {{ end -}} ``` -------------------------------- ### Inspect PostgreSQL Configuration Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterPhysicalReplicationLag.md Check current values for replication-related PostgreSQL parameters. ```bash kubectl exec -it services/-rw -- psql -c " SHOW max_wal_senders; SHOW wal_compression; SHOW max_replication_slots; " ``` -------------------------------- ### Verify Publication and Subscription Configuration Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Check the configuration of publications on the publisher and subscriptions on the subscriber to ensure they are correctly set up. This includes verifying which tables are published and subscribed. ```bash # On publisher - check publication tables kubectl exec -it svc/PUBLISHER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT pubname, puballtables, pubinsert, pubupdate, pubdelete FROM pg_publication; " ``` ```bash # On subscriber - check subscription details kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT subname, srconninfo, srschema, srslotname, srsynccommit FROM pg_subscription; " ``` ```bash # Check which tables are being replicated kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT relid::regclass as table_name, srsubstate as state FROM pg_subscription_rel JOIN pg_class ON relid = oid WHERE srsubstate NOT IN ('r', 's'); -- Not ready or synchronizing " ``` -------------------------------- ### Create release branch Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Initialize a new release branch based on the new version. ```bash git switch --create release/plugin-barman-cloud-v$NEW_VERSION ``` -------------------------------- ### List Pods by Label Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterInstancesOnSameNode.md Use this command to list all pods with the label 'cnpg.io/podRole=instance' across all namespaces and display their node assignments. This helps in diagnosing the alert by showing which instances are co-located. ```bash kubectl get pods -A -l "cnpg.io/podRole=instance" -o wide ``` -------------------------------- ### Create release branch Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Create and switch to a new release branch based on the version variable. ```bash git switch --create release/cluster-v$NEW_VERSION ``` -------------------------------- ### Check PostgreSQL Logs Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Inspects pod logs for subscription-related errors. ```bash POD=$(kubectl get pods -n NAMESPACE -l app=postgresql -o name | head -1 | cut -d/ -f2) kubectl logs -n NAMESPACE $POD --tail=200 | grep -i "subscription\|replication\|worker" ``` -------------------------------- ### Check CloudNativePG Instance Logs Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterHACritical.md Retrieve logs for a specific CloudNativePG instance pod within a given namespace. Replace `` and `` with actual values. ```bash kubectl logs --namespace pod/ ``` -------------------------------- ### Enable Subscription Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Commands to re-enable a disabled subscription via SQL or CNPG CLI. ```sql ALTER SUBSCRIPTION subscription_name ENABLE; ``` ```bash kubectl cnpg subscription enable subscription_name -n NAMESPACE ``` -------------------------------- ### Test Connectivity to Publisher Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Extracts connection information and tests the network path to the publisher. ```bash kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT srconninfo FROM pg_subscription WHERE subname = 'your_subscription_name'; " | grep -o "host=[^ ]*" | cut -d= -f2 kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- \ psql "host=PUBLISHER-HOST port=5432 dbname=DATABASE user=USER" -c "SELECT version();" ``` -------------------------------- ### Perform Full Resynchronization Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Refresh the subscription to re-copy data from the publisher. ```bash # Mark subscription for full refresh kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " ALTER SUBSCRIPTION your_subscription REFRESH PUBLICATION WITH (copy_data = true); " # Or restart the subscription kubectl cnpg subscription restart your_subscription -n NAMESPACE ``` -------------------------------- ### Connect to Console Pod Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt Command to connect to the console pod for executing long-running tasks, such as `CREATE INDEX`. This is conditional on the console being enabled. ```bash kubectl --namespace {{ .Release.Namespace }} exec --stdin --tty statefulsets/{{ include "cluster.fullname" . }}-console -- bash ``` -------------------------------- ### Execute Background Command with nohup Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/Console.md Run a command in the background using `nohup`. The output is redirected to `command.log`. Ensure you have the necessary database credentials available as environment variables. ```bash nohup psql "$DB_SUPERUSER_URI/" -c "CREATE INDEX orders_idx ON orders USING bm25 (order_id, customer_name) WITH (key_field='order_id');" 2>&1 > command.log & ``` -------------------------------- ### PostgreSQL Cluster Monitoring and Management Commands Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterPhysicalReplicationLag.md Collection of kubectl commands to inspect replication status, resource usage, long-running queries, and configuration parameters. ```bash # Check replication status kubectl exec -n services/-rw -- psql -c "SELECT * FROM pg_stat_replication;" # Check resource usage kubectl top pods -n -l cnpg.io/podRole=instance # Check long-running queries kubectl exec -it services/-rw -- psql -c " SELECT pid, now() - pg_stat_activity.query_start AS duration, query FROM pg_stat_activity WHERE state = 'active' AND now() - query_start > interval '5 minutes' ORDER BY duration DESC; " # Restart a replica (if needed) kubectl delete pod -n # Check PostgreSQL parameters kubectl exec -it services/-rw -- psql -c "SHOW max_wal_senders; SHOW wal_compression;" ``` -------------------------------- ### Enable WAL Compression Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterPhysicalReplicationLag.md Enable WAL compression in the cluster configuration to reduce replication traffic. ```yaml cluster: postgresql: parameters: wal_compression: "on" ``` -------------------------------- ### Recreate Subscription Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Drop and recreate a subscription to resolve configuration or connection issues. ```sql -- Drop and recreate the subscription DROP SUBSCRIPTION IF EXISTS subscription_name; CREATE SUBSCRIPTION subscription_name CONNECTION 'host=publisher-host port=5432 dbname=database_name user=replication_user password=xxx' PUBLICATION publication_name WITH ( copy_data = true, synchronized_commit = 'off', create_slot = true ); ``` -------------------------------- ### Run Helm Tests Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt Command to execute Helm tests for a specific release and namespace. ```bash helm test --namespace {{ .Release.Namespace }} {{ .Release.Name }} ``` -------------------------------- ### Check current chart version Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Retrieve the current version and appVersion from the Chart.yaml file. ```bash OLD_VERSION=$(yq -r '.version' charts/plugin-barman-cloud/Chart.yaml) OLD_APP_VERSION=$(yq -r '.appVersion' charts/plugin-barman-cloud/Chart.yaml) echo Old chart version: $OLD_VERSION echo Old app version: $OLD_APP_VERSION ``` -------------------------------- ### Manage Subscription Lifecycle Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Restart or delete subscriptions using kubectl. ```bash # WARNING: This will resync all data kubectl cnpg subscription restart SUBSCRIPTION-NAME -n NAMESPACE # Or completely recreate kubectl cnpg subscription delete SUBSCRIPTION-NAME -n NAMESPACE # Recreate with proper configuration ``` -------------------------------- ### Verify Schema Consistency Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Use these commands to inspect table structures on both publisher and subscriber nodes to identify mismatches. ```sql -- On publisher \d+ table_name -- On subscriber \d+ table_name -- Compare columns and types SELECT column_name, data_type, is_nullable FROM information_schema.columns WHERE table_name = 'table_name'; ``` -------------------------------- ### List all screen sessions Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/Console.md Displays a list of all active screen sessions, allowing you to see which sessions are running. ```bash screen -list ``` -------------------------------- ### Fetch latest app version Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Retrieve the latest tag from the plugin-barman-cloud repository. ```bash NEW_APP_VERSION=$(curl -Ssl "https://api.github.com/repos/cloudnative-pg/plugin-barman-cloud/tags" | jq -r '.[0].name') echo New app version: $NEW_APP_VERSION ``` -------------------------------- ### Diff manifest versions Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Compare manifest files between the old and new versions to identify template changes. ```bash vimdiff \ "https://github.com/cloudnative-pg/plugin-barman-cloud/releases/download/${OLD_APP_VERSION}/manifest.yaml" \ "https://github.com/cloudnative-pg/plugin-barman-cloud/releases/download/${NEW_APP_VERSION}/manifest.yaml" ``` -------------------------------- ### Configure Replication Parameters Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationLagging.md Adjust PostgreSQL parameters in the cluster manifest to support multiple publications and increased WAL traffic. ```yaml # If multiple publications postgresql: parameters: max_replication_slots: 10 max_wal_senders: 10 ``` -------------------------------- ### Compare Release YAMLs for Template Updates Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Compares the YAML definitions of two CloudNativePG releases using `vimdiff` to identify necessary updates for the chart's templates. This can be done by downloading release files or by referencing files within the `cloudnative-pg` repository. ```bash vimdiff \ "https://github.com/cloudnative-pg/cloudnative-pg/releases/download/v${OLD_CNPG_VERSION}/cnpg-${OLD_CNPG_VERSION}.yaml" \ "https://github.com/cloudnative-pg/cloudnative-pg/releases/download/v${NEW_CNPG_VERSION}/cnpg-${NEW_CNPG_VERSION}.yaml" ``` ```bash vimdiff releases/cnpg-1.15.0.yaml releases/cnpg-1.15.1.yaml ``` -------------------------------- ### Inspect Network Interface Statistics Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterPhysicalReplicationLag.md Use the ss command to inspect network interface statistics on a specific pod. ```bash kubectl exec -it -- ss -i ``` -------------------------------- ### Configure Cluster Resources Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterPhysicalReplicationLag.md Set CPU and memory requests and limits in the Helm values to ensure QoS Guaranteed. ```yaml cluster: resources: requests: cpu: 4 memory: 16Gi limits: cpu: 4 memory: 16Gi ``` -------------------------------- ### Deploy PostgreSQL Cluster Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cloudnative-pg/templates/NOTES.txt Creates a 3-node PostgreSQL cluster using a YAML manifest applied via kubectl. ```yaml cat <` and `` with your specific values. ```bash kubectl exec --namespace --stdin --tty services/-rw -- psql -c "SELECT * FROM pg_stat_replication;" ``` -------------------------------- ### Tune PostgreSQL Parameters for Write Performance Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationLagging.md Optimize PostgreSQL settings related to WAL buffers, checkpointing, and WAL size to improve write performance and reduce apply lag. These parameters affect how WAL data is written and processed. ```yaml postgresql: parameters: # Increase for better write performance wal_buffers: '16MB' checkpoint_completion_target: 0.9 # Reduce checkpoint frequency max_wal_size: '4GB' min_wal_size: '1GB' ``` -------------------------------- ### Check Subscription Status and Errors Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Connect to the subscriber cluster to check the status of logical replication subscriptions and identify any apply or sync errors. Use this to quickly see if errors are occurring. ```bash kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT subname, subenabled, apply_error_count, sync_error_count, stats_reset FROM pg_stat_subscription WHERE apply_error_count > 0 OR sync_error_count > 0; " ``` ```bash kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT subname, last_msg_receipt_time, latest_end_time, CASE WHEN apply_error_count > 0 THEN 'Apply errors detected' WHEN sync_error_count > 0 THEN 'Sync errors detected' END as error_type FROM pg_stat_subscription; " ``` -------------------------------- ### Verify Replication Worker Configuration Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationLagging.md Check the PostgreSQL configuration for worker processes on the subscriber. Ensure that `max_worker_processes` is sufficient to accommodate `max_parallel_workers` and `max_logical_replication_workers`. ```bash kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " SHOW max_worker_processes; SHOW max_logical_replication_workers; SHOW max_parallel_workers; " ``` -------------------------------- ### Restart Replication Components Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationLagging.md Use kubectl commands to restart specific subscriptions or the entire subscriber cluster when replication stalls. ```bash # If subscriber is stuck kubectl cnpg subscription restart SUBSCRIPTION-NAME -n NAMESPACE # Or restart the entire cluster kubectl cnpg restart SUBSCRIBER-CLUSTER -n NAMESPACE ``` -------------------------------- ### Display Cluster Configuration Status Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt Templates for rendering storage, PGBouncer, and monitoring status in a formatted table layout. ```Helm │ Storage Class │ {{ printf "%-56s" (default "Default" .Values.cluster.storage.storageClass) }} │ │ PGBouncer │ {{ printf "%-56s" (ternary "Enabled" "Disabled" (gt (len .Values.poolers) 0)) }} │ │ Monitoring │ {{ include (printf "%s%s" "cluster.color-" (ternary "ok" "error" .Values.cluster.monitoring.enabled)) (printf "%-56s" (ternary "Enabled" "Disabled" .Values.cluster.monitoring.enabled)) }} │ ``` -------------------------------- ### Restart Subscription or Cluster Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Attempts to restart the subscription or the entire cluster to clear stuck states. ```bash kubectl cnpg subscription restart subscription_name -n NAMESPACE kubectl cnpg restart subscriber-cluster -n NAMESPACE ``` -------------------------------- ### Add and Update Helm Repository Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Adds the `cloudnative-pg` Helm chart repository if it doesn't exist and updates the local repository cache to fetch the latest chart information. ```bash helm repo add cnpg https://cloudnative-pg.github.io/charts helm repo update helm search repo cnpg ``` -------------------------------- ### Identify Replication Conflicts Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Search logs for specific constraint violations or replication errors. ```bash kubectl logs -n NAMESPACE $POD | grep "conflict detected\|duplicate key" ``` -------------------------------- ### Configure WAL Receiver Settings Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationLagging.md Adjust `wal_sender_timeout` and `wal_receiver_status_interval` in the subscriber's PostgreSQL configuration to optimize network communication and reduce receipt lag. ```yaml # In the subscriber's postgresql configuration postgresql: parameters: wal_sender_timeout: '60s' wal_receiver_status_interval: '10s' ``` -------------------------------- ### Replication Slots Monitoring Configuration Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cloudnative-pg/README.md Defines the SQL query to fetch replication slot information and maps the results to Prometheus metrics. Use this to monitor replication lag and slot status. ```yaml pg_replication_slots: query: | SELECT slot_name, slot_type, database, active, (CASE pg_catalog.pg_is_in_recovery() WHEN TRUE THEN pg_catalog.pg_wal_lsn_diff(pg_catalog.pg_last_wal_receive_lsn(), restart_lsn) ELSE pg_catalog.pg_wal_lsn_diff(pg_catalog.pg_current_wal_lsn(), restart_lsn) END) as pg_wal_lsn_diff FROM pg_catalog.pg_replication_slots WHERE NOT temporary metrics: - slot_name: usage: "LABEL" description: "Name of the replication slot" - slot_type: usage: "LABEL" description: "Type of the replication slot" - database: usage: "LABEL" description: "Name of the database" - active: usage: "GAUGE" description: "Flag indicating whether the slot is active" - pg_wal_lsn_diff: usage: "GAUGE" description: "Replication lag in bytes" ``` -------------------------------- ### Check current chart version Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Retrieve the current version from the Chart.yaml file. ```bash yq -r '.version' charts/cluster/Chart.yaml ``` -------------------------------- ### Check and Increase WAL Retention Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Verify current WAL settings and increase retention to prevent replication lag issues. ```bash # On publisher, check wal_keep_size kubectl exec -it svc/PUBLISHER-CLUSTER-rw -n NAMESPACE -- psql -c "SHOW wal_keep_size;" # Check if WAL was removed before subscription could catch up kubectl exec -it svc/PUBLISHER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT slot_name, restart_lsn, pg_current_wal_lsn() FROM pg_replication_slots; " ``` ```yaml # In publisher configuration postgresql: parameters: wal_keep_size: '2GB' max_slot_wal_keep_size: '4GB' ``` -------------------------------- ### Check PostgreSQL Logs for Replication Errors Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationErrors.md Retrieve and filter PostgreSQL logs from the subscriber pod to identify specific replication or subscription-related errors. Use `--tail` for recent errors or `-f` for real-time streaming. ```bash # Get the pod name POD=$(kubectl get pods -n NAMESPACE -l app=postgresql -o name | head -1 | cut -d/ -f2) # Check recent logs for errors kubectl logs -n NAMESPACE $POD --tail=100 | grep -i "replication\|subscription\|error" ``` ```bash # Stream logs for real-time monitoring kubectl logs -n NAMESPACE $POD -f | grep -i "replication\|subscription\|error" ``` -------------------------------- ### pg_stat_checkpointer Metrics Configuration Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cloudnative-pg/README.md Configures metrics for the pg_stat_checkpointer view, tracking checkpoint counts, timings, and buffer writes. Useful for performance tuning related to checkpoints. ```yaml pg_stat_checkpointer: runonserver: ">=17.0.0" query: | SELECT num_timed AS checkpoints_timed , num_requested AS checkpoints_req , restartpoints_timed , restartpoints_req , restartpoints_done , write_time , sync_time , buffers_written , EXTRACT(EPOCH FROM stats_reset) AS stats_reset_time FROM pg_catalog.pg_stat_checkpointer metrics: - checkpoints_timed: usage: "COUNTER" description: "Number of scheduled checkpoints that have been performed" - checkpoints_req: usage: "COUNTER" description: "Number of requested checkpoints that have been performed" - restartpoints_timed: usage: "COUNTER" description: "Number of scheduled restartpoints due to timeout or after a failed attempt to perform it" - restartpoints_req: usage: "COUNTER" description: "Number of requested restartpoints that have been performed" - restartpoints_done: usage: "COUNTER" description: "Number of restartpoints that have been performed" - write_time: usage: "COUNTER" description: "Total amount of time that has been spent in the portion of processing checkpoints and restartpoints where files are written to disk, in milliseconds" - sync_time: usage: "COUNTER" description: "Total amount of time that has been spent in the portion of processing checkpoints and restartpoints where files are synchronized to disk, in milliseconds" - buffers_written: usage: "COUNTER" description: "Number of buffers written during checkpoints and restartpoints" - stats_reset_time: usage: "GAUGE" description: "Time at which these statistics were last reset" ``` -------------------------------- ### Full Resync Procedure Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Perform a full resynchronization of tables by disabling the subscription and truncating data. ```bash # Step 1: Mark all tables for resync kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT schemaname, tablename FROM pg_tables WHERE schemaname NOT IN ('pg_catalog', 'information_schema'); " # Step 2: Disable subscription kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " ALTER SUBSCRIPTION subscription_name DISABLE; " # Step 3: Truncate subscriber tables (if safe) kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " TRUNCATE TABLE table_name CASCADE; -- Repeat for each table " # Step 4: Re-enable with full copy kubectl exec -it svc/SUBSCRIBER-CLUSTER-rw -n NAMESPACE -- psql -c " ALTER SUBSCRIPTION subscription_name ENABLE; ALTER SUBSCRIPTION subscription_name REFRESH PUBLICATION WITH (copy_data = true); " ``` -------------------------------- ### Verify Cluster Status Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cloudnative-pg/templates/NOTES.txt Lists all PostgreSQL clusters across all namespaces to verify the deployment. ```bash kubectl get -A cluster ``` -------------------------------- ### Commit release changes Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Stage and commit the changes with a release message. ```bash git add . git commit -S -s -m "Release cluster-v$NEW_VERSION" --edit ``` -------------------------------- ### Verify Publication Existence Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Check if the publication exists on the publisher cluster. ```bash # On publisher kubectl exec -it svc/PUBLISHER-CLUSTER-rw -n NAMESPACE -- psql -c " SELECT pubname FROM pg_publication; " ``` -------------------------------- ### Configure PostgreSQL Monitoring Queries Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cloudnative-pg/README.md Define custom Prometheus queries for various PostgreSQL metrics by configuring the `monitoringQueriesConfigMap.queries` value. This allows for detailed monitoring of database backends, locks, database sizes, and replication status. ```yaml monitoringQueriesConfigMap: queries: | backends: query: | SELECT sa.datname , sa.usename , sa.application_name , states.state , COALESCE(sa.count, 0) AS total , COALESCE(sa.max_tx_secs, 0) AS max_tx_duration_seconds FROM ( VALUES ('active') , ('idle') , ('idle in transaction') , ('idle in transaction (aborted)') , ('fastpath function call') , ('disabled') ) AS states(state) LEFT JOIN ( SELECT datname , state , usename , COALESCE(application_name, '') AS application_name , pg_catalog.count(*) , COALESCE(EXTRACT (EPOCH FROM (pg_catalog.now() OPERATOR(pg_catalog.-) xact_start))), 0) AS max_tx_secs FROM pg_catalog.pg_stat_activity GROUP BY datname, state, usename, application_name ) sa ON states.state OPERATOR(pg_catalog.=) sa.state WHERE sa.usename IS NOT NULL metrics: - datname: usage: "LABEL" description: "Name of the database" - usename: usage: "LABEL" description: "Name of the user" - application_name: usage: "LABEL" description: "Name of the application" - state: usage: "LABEL" description: "State of the backend" - total: usage: "GAUGE" description: "Number of backends" - max_tx_duration_seconds: usage: "GAUGE" description: "Maximum duration of a transaction in seconds" backends_waiting: query: | SELECT pg_catalog.count(*) AS total FROM pg_catalog.pg_locks blocked_locks JOIN pg_catalog.pg_locks blocking_locks ON blocking_locks.locktype OPERATOR(pg_catalog.=) blocked_locks.locktype AND blocking_locks.database IS NOT DISTINCT FROM blocked_locks.database AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid AND blocking_locks.pid OPERATOR(pg_catalog.<>) blocked_locks.pid JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid OPERATOR(pg_catalog.=) blocking_locks.pid WHERE NOT blocked_locks.granted metrics: - total: usage: "GAUGE" description: "Total number of backends that are currently waiting on other queries" pg_database: query: | SELECT datname , pg_catalog.pg_database_size(datname) AS size_bytes , pg_catalog.age(datfrozenxid) AS xid_age , pg_catalog.mxid_age(datminmxid) AS mxid_age FROM pg_catalog.pg_database WHERE datallowconn metrics: - datname: usage: "LABEL" description: "Name of the database" - size_bytes: usage: "GAUGE" description: "Disk space used by the database" - xid_age: usage: "GAUGE" description: "Number of transactions from the frozen XID to the current one" - mxid_age: usage: "GAUGE" description: "Number of multiple transactions (Multixact) from the frozen XID to the current one" pg_postmaster: query: | SELECT EXTRACT(EPOCH FROM pg_postmaster_start_time) AS start_time FROM pg_catalog.pg_postmaster_start_time() metrics: - start_time: usage: "GAUGE" description: "Time at which postgres started (based on epoch)" pg_replication: query: | SELECT CASE WHEN ( NOT pg_catalog.pg_is_in_recovery() OR pg_catalog.pg_last_wal_receive_lsn() OPERATOR(pg_catalog.=) pg_catalog.pg_last_wal_replay_lsn()) THEN 0 ELSE GREATEST (0, EXTRACT(EPOCH FROM (pg_catalog.now() OPERATOR(pg_catalog.-) pg_catalog.pg_last_xact_replay_timestamp()))) END AS lag, pg_catalog.pg_is_in_recovery() AS in_recovery, EXISTS (TABLE pg_catalog.pg_stat_wal_receiver) AS is_wal_receiver_up, (SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_replication) AS streaming_replicas metrics: - lag: usage: "GAUGE" description: "Replication lag behind primary in seconds" - in_recovery: usage: "GAUGE" description: "Whether the instance is in recovery" - is_wal_receiver_up: usage: "GAUGE" description: "Whether the instance wal_receiver is up" - streaming_replicas: usage: "GAUGE" description: "Number of streaming replicas connected to the inst ``` -------------------------------- ### Commit release changes Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Stage and commit the changes with the release version message. ```bash git add . git commit -S -s -m "Release plugin-barman-cloud-v$NEW_VERSION" --edit ``` -------------------------------- ### Generate CRDs YAML Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Generates the `crds.yaml` file by building the CloudNativePG CRDs using `kustomize` from a remote GitHub repository. This command requires the `kustomize` tool. ```bash echo '{{- if .Values.crds.create }}' > ./charts/cloudnative-pg/templates/crds/crds.yaml kustomize build https://github.com/cloudnative-pg/cloudnative-pg/config/helm/\?ref\=v$NEW_CNPG_VERSION >> ./charts/cloudnative-pg/templates/crds/crds.yaml echo '{{- end }}' >> ./charts/cloudnative-pg/templates/crds/crds.yaml ``` -------------------------------- ### Deprecated Pooler Configuration Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/templates/NOTES.txt This snippet checks for the deprecated `.Values.pooler` configuration and fails the Helm release if found, advising users to use `.Values.poolers` instead. ```gohtml {{- if .Values.pooler -}} {{ fail ".Values.pooler has been deprecated. Use .Values.poolers instead." }} {{- end -}} ``` -------------------------------- ### Configure Subscription Retry Parameters Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterLogicalReplicationStopped.md Set high availability parameters for WAL receiver timeouts and retries. ```yaml # Configure subscription retry parameters postgresql: parameters: wal_receiver_timeout: '60s' wal_receiver_status_interval: '10s' wal_retrieve_retry_interval: '5s' ``` -------------------------------- ### Check PostgreSQL Replication Status Source: https://github.com/cloudnative-pg/charts/blob/main/charts/cluster/docs/runbooks/CNPGClusterHighReplicationLag.md Execute this command to check the replication status of your CloudNativePG cluster. Ensure you replace `` and `` with your specific values. ```bash kubectl exec --namespace --stdin --tty services/-rw -- psql -c "SELECT * from pg_stat_replication;" ``` -------------------------------- ### Create Release Branch Source: https://github.com/cloudnative-pg/charts/blob/main/RELEASE.md Creates a new Git branch for the release, named according to the new version. ```bash git switch --create release/cloudnative-pg-v$NEW_VERSION ```