Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions internal/controller/postgrescluster/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"sort"
"strings"

gover "github.com/hashicorp/go-version"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -191,6 +192,16 @@ func (r *Reconciler) reconcilePostgresDatabases(
const container = naming.ContainerDatabase
var podExecutor postgres.Executor

log := logging.FromContext(ctx)

// K8SPG-377
for _, inst := range instances.forCluster {
if matches, known := inst.PodMatchesPodTemplate(); !matches || !known {
log.V(1).Info("Waiting for instance to be updated", "instance", inst.Name)
return nil
}
}

// Find the PostgreSQL instance that can execute SQL that writes system
// catalogs. When there is none, return early.
pod, _ := instances.writablePod(container)
Expand Down Expand Up @@ -233,10 +244,24 @@ func (r *Reconciler) reconcilePostgresDatabases(
}

// Calculate a hash of the SQL that should be executed in PostgreSQL.

// K8SPG-375, K8SPG-577
// K8SPG-375, K8SPG-577, K8SPG-699
var pgAuditOK, pgStatMonitorOK, pgStatStatementsOK, pgvectorOK, postgisInstallOK bool
create := func(ctx context.Context, exec postgres.Executor) error {
// validate version string before running it in database
_, err := gover.NewVersion(cluster.Labels[naming.LabelVersion])
if err != nil {
return err
}

_, _, err = exec.ExecInAllDatabases(ctx,
fmt.Sprintf("SELECT '%s';", cluster.Labels[naming.LabelVersion]),
map[string]string{
"QUIET": "on", // Do not print successful commands to stdout.
})
if err != nil {
return err
}

if cluster.Spec.Extensions.PGStatMonitor {
if pgStatMonitorOK = pgstatmonitor.EnableInPostgreSQL(ctx, exec) == nil; !pgStatMonitorOK {
// pg_stat_monitor can only be enabled after its shared library is loaded,
Expand Down Expand Up @@ -281,6 +306,7 @@ func (r *Reconciler) reconcilePostgresDatabases(
}
}

// K8SPG-699
if cluster.Spec.Extensions.PGVector {
if pgvectorOK = pgvector.EnableInPostgreSQL(ctx, exec) == nil; !pgvectorOK {
r.Recorder.Event(cluster, corev1.EventTypeWarning, "pgvectorDisabled",
Expand Down Expand Up @@ -338,7 +364,7 @@ func (r *Reconciler) reconcilePostgresDatabases(
err = errors.WithStack(create(logging.NewContext(ctx, log), podExecutor))
}
// K8SPG-472
if err == nil && pgStatMonitorOK && pgAuditOK && postgisInstallOK {
if err == nil && pgStatMonitorOK && pgAuditOK && pgvectorOK && postgisInstallOK {
cluster.Status.DatabaseRevision = revision
}

Expand Down
2 changes: 1 addition & 1 deletion internal/pgaudit/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func EnableInPostgreSQL(ctx context.Context, exec postgres.Executor) error {
// Quiet the NOTICE from IF EXISTS, and install the pgAudit event triggers.
// - https://www.postgresql.org/docs/current/runtime-config-client.html
// - https://github.com/pgaudit/pgaudit#settings
`SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS pgaudit;`,
`SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS pgaudit; ALTER EXTENSION pgaudit UPDATE;`,
map[string]string{
"ON_ERROR_STOP": "on", // Abort when any one command fails.
"QUIET": "on", // Do not print successful commands to stdout.
Expand Down
2 changes: 1 addition & 1 deletion internal/pgaudit/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestEnableInPostgreSQL(t *testing.T) {
b, err := io.ReadAll(stdin)
assert.NilError(t, err)
assert.Equal(t, string(b), strings.Trim(`
SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS pgaudit;
SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS pgaudit; ALTER EXTENSION pgaudit UPDATE;
`, "\t\n"))

return expected
Expand Down
2 changes: 1 addition & 1 deletion internal/pgstatmonitor/pgstatmonitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func EnableInPostgreSQL(ctx context.Context, exec postgres.Executor) error {
log := logging.FromContext(ctx)

stdout, stderr, err := exec.ExecInAllDatabases(ctx,
`SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS pg_stat_monitor;`,
`SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS pg_stat_monitor; ALTER EXTENSION pg_stat_monitor UPDATE;`,
map[string]string{
"ON_ERROR_STOP": "on", // Abort when any one command fails.
"QUIET": "on", // Do not print successful commands to stdout.
Expand Down
2 changes: 1 addition & 1 deletion internal/pgstatmonitor/pgstatmonitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestEnableInPostgreSQL(t *testing.T) {
b, err := io.ReadAll(stdin)
assert.NilError(t, err)
assert.Equal(t, string(b), strings.Trim(`
SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS pg_stat_monitor;
SET client_min_messages = WARNING; CREATE EXTENSION IF NOT EXISTS pg_stat_monitor; ALTER EXTENSION pg_stat_monitor UPDATE;
`, "\t\n"))

return expected
Expand Down
2 changes: 1 addition & 1 deletion percona/watcher/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func WatchCommitTimestamps(ctx context.Context, cli client.Client, eventChan cha
}

latestRestorableTime := latestBackup.Status.LatestRestorableTime
log.Info("Latest commit timestamp", "timestamp", ts, "latestRestorableTime", latestRestorableTime.Time)
log.V(1).Info("Latest commit timestamp", "timestamp", ts, "latestRestorableTime", latestRestorableTime.Time)
if latestRestorableTime.Time == nil || latestRestorableTime.UTC().Before(ts.Time) {
log.Info("Triggering PGBackup reconcile",
"latestBackup", latestBackup.Name,
Expand Down
Loading