SGShardedDbOps allows you to perform day-2 database operations on sharded clusters, including restarts, resharding, security upgrades, and minor and major version upgrades.
The
restartandsecurityUpgradeoperations are logically equivalent since the SGShardedCluster version is updated on any restart. These operations can also be performed without creating an SGShardedDbOps by using the rollout functionality, which allows the operator to automatically roll out Pod updates based on the cluster’s update strategy.
| Operation | Description | Use Case |
|---|---|---|
restart |
Rolling restart of all pods | Apply configuration changes, clear memory |
resharding |
Rebalance data across workers | After adding workers, optimize distribution |
securityUpgrade |
Upgrade security patches | Apply security fixes |
minorVersionUpgrade |
Upgrade PostgreSQL minor version | Apply PostgreSQL minor releases |
majorVersionUpgrade |
Upgrade PostgreSQL major version | Move to a new PostgreSQL major version |
Restart all pods in the sharded cluster:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: cluster-restart
spec:
sgShardedCluster: my-sharded-cluster
op: restart
Restarts pods without creating additional replicas. Faster but may cause brief unavailability:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: inplace-restart
spec:
sgShardedCluster: my-sharded-cluster
op: restart
restart:
method: InPlace
Creates a new replica before restarting each pod, minimizing impact:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: reduced-impact-restart
spec:
sgShardedCluster: my-sharded-cluster
op: restart
restart:
method: ReducedImpact
Restart only pods that require a restart (e.g., after configuration change):
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: pending-restart
spec:
sgShardedCluster: my-sharded-cluster
op: restart
restart:
method: ReducedImpact
onlyPendingRestart: true
Resharding rebalances data distribution across workers. This is essential after adding new workers.
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: rebalance-workers
spec:
sgShardedCluster: my-sharded-cluster
op: resharding
resharding:
citus:
threshold: 0.1 # Rebalance if nodes differ by 10% in utilization
The threshold determines when rebalancing occurs based on utilization difference:
| Threshold | Behavior |
|---|---|
0.0 |
Always rebalance (aggressive) |
0.1 |
Rebalance if >10% difference |
0.2 |
Rebalance if >20% difference |
1.0 |
Never rebalance |
Move all data off specific workers before removal:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: drain-workers
spec:
sgShardedCluster: my-sharded-cluster
op: resharding
resharding:
citus:
drainOnly: true
Use a specific Citus rebalance strategy:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: custom-rebalance
spec:
sgShardedCluster: my-sharded-cluster
op: resharding
resharding:
citus:
threshold: 0.1
rebalanceStrategy: by_disk_size
Available strategies depend on Citus version:
by_shard_count: Balance number of workers (default)by_disk_size: Balance disk usageApply security patches without changing PostgreSQL version:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: security-upgrade
spec:
sgShardedCluster: my-sharded-cluster
op: securityUpgrade
securityUpgrade:
method: ReducedImpact
Upgrade PostgreSQL to a newer minor version. The operation updates the SGShardedCluster postgres version and then performs a minor version upgrade SGDbOps on the coordinator and each worker cluster, one at a time:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: minor-version-upgrade
spec:
sgShardedCluster: my-sharded-cluster
op: minorVersionUpgrade
minorVersionUpgrade:
postgresVersion: "16.2"
method: ReducedImpact
Upgrade PostgreSQL to a new major version (see also pg_upgrade).
An SGPostgresConfig for the target major version is required. The operation updates the
SGShardedCluster postgres version and configuration and then performs a major version
upgrade SGDbOps on the coordinator and each worker cluster, one at a time:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: major-version-upgrade
spec:
sgShardedCluster: my-sharded-cluster
op: majorVersionUpgrade
majorVersionUpgrade:
postgresVersion: "16.2"
sgPostgresConfig: postgres-16-config
link: true
Important: a major version upgrade cannot be performed if a required extension is not available for the target major version. In such cases you may specify the target extension versions using the
postgresExtensionssection. Backups for the previous major version are kept in their paths while new backups will be stored in new paths that can be specified with thebackupPathsfield (one path for the coordinator followed by one path for each worker cluster) or are generated automatically.
While the SGShardedDbOps is running, the SGShardedCluster stops reconciling its child SGClusters
(the names of the created child SGDbOps are tracked in
SGShardedCluster.status.dbOps.majorVersionUpgrade.sgDbOps) so that each child SGDbOps is in full
control of the major version upgrade of its SGCluster.
The SGShardedDbOps always creates the child SGDbOps with manualRollback: true, so each child
pauses
on failure (and, on success, before its cleanup).
manualRollback is false) the decision for each child is taken
automatically: a failed child is rolled back and a succeeded child is continued/cleaned up.manualRollback is true, whenever a child pauses the SGShardedDbOps
mirrors the child state to its own .status.majorVersionUpgrade.status, the operator raises the
WaitingRollback condition, and the SGShardedDbOps waits until you set
.status.majorVersionUpgrade.rollback. That value (false to continue assuming a manual upgrade,
true to roll back) is then propagated to the waiting child SGDbOps. The
maxErrorsAfterContinueOnFailure field is propagated to each child as well.apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: major-version-upgrade
spec:
sgShardedCluster: my-sharded-cluster
op: majorVersionUpgrade
majorVersionUpgrade:
postgresVersion: "16.2"
sgPostgresConfig: postgres-16-config
link: true
manualRollback: true
# When the SGShardedDbOps is waiting (condition WaitingRollback=True), resolve it:
kubectl patch sgshardeddbops major-version-upgrade --type merge \
-p '{"status":{"majorVersionUpgrade":{"rollback":false}}}'
Schedule an operation for a future time:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: scheduled-restart
spec:
sgShardedCluster: my-sharded-cluster
op: restart
runAt: "2024-01-20T03:00:00Z" # Run at 3 AM UTC
restart:
method: ReducedImpact
Set a maximum duration for the operation:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: restart-with-timeout
spec:
sgShardedCluster: my-sharded-cluster
op: restart
timeout: PT2H # Fail if not completed in 2 hours
restart:
method: ReducedImpact
Configure automatic retries on failure:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: restart-with-retry
spec:
sgShardedCluster: my-sharded-cluster
op: restart
maxRetries: 3
restart:
method: ReducedImpact
# List all operations
kubectl get sgshardeddbops
# View detailed status
kubectl get sgshardeddbops cluster-restart -o yaml
status:
conditions:
- type: Running
status: "True"
reason: OperationRunning
- type: Completed
status: "False"
- type: Failed
status: "False"
opStarted: "2024-01-15T10:00:00Z"
opRetries: 0
restart:
pendingToRestartSgClusters:
- my-sharded-cluster-worker1
restartedSgClusters:
- my-sharded-cluster-coord
- my-sharded-cluster-worker0
| Condition | Description |
|---|---|
Running |
Operation is in progress |
Completed |
Operation finished successfully |
Failed |
Operation failed |
OperationTimedOut |
Operation exceeded timeout |
kubectl get sgshardeddbops cluster-restart -w
Control where operation pods run:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: scheduled-maintenance
spec:
sgShardedCluster: my-sharded-cluster
op: restart
scheduling:
nodeSelector:
node-type: maintenance
tolerations:
- key: maintenance
operator: Exists
effect: NoSchedule
After adding workers, rebalance data:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: post-scale-rebalance
spec:
sgShardedCluster: my-sharded-cluster
op: resharding
resharding:
citus:
threshold: 0.0 # Force rebalance
Schedule restart during maintenance window:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: maintenance-restart
spec:
sgShardedCluster: my-sharded-cluster
op: restart
runAt: "2024-01-21T02:00:00Z"
timeout: PT4H
restart:
method: ReducedImpact
onlyPendingRestart: true
Apply urgent security update:
apiVersion: stackgres.io/v1
kind: SGShardedDbOps
metadata:
name: urgent-security-upgrade
spec:
sgShardedCluster: my-sharded-cluster
op: securityUpgrade
securityUpgrade:
method: InPlace # Faster for urgent patches
To cancel a running operation, delete the resource:
kubectl delete sgshardeddbops cluster-restart
Note: Cancellation may leave the cluster in an intermediate state. Review cluster status after cancellation.
runAt for maintenance windows