Skip to content

Commit 1011745

Browse files
cbandybenjaminjbdsessler7
committed
Make the default Postgres log directory outside its data directory
This has been the recommended practice for ages, but PGO left this parameter at Postgres' packaged default. Being outside the data directory makes the log directory and files: - easier to create - easier to consume from the OpenTelemetry Collector - easier to exclude from backups - persistent across failures that recover by recreating the data directory Users that wish to log to the default directory can set "spec.config.parameters.log_directory" to "log" on PostgresCluster. Co-authored-by: Benjamin Blattberg <ben.blattberg@crunchydata.com> Co-authored-by: Drew Sessler <drew.sessler@crunchydata.com> Issue: PGO-2558
1 parent a2e37b2 commit 1011745

File tree

7 files changed

+16
-26
lines changed

7 files changed

+16
-26
lines changed

internal/collector/instance.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,8 @@ func AddToPod(
181181
// startCommand generates the command script used by the collector container
182182
func startCommand(logDirectories []string, includeLogrotate bool) []string {
183183
var mkdirScript string
184-
if len(logDirectories) != 0 {
185-
for _, logDir := range logDirectories {
186-
mkdirScript = mkdirScript + `
187-
` + shell.MakeDirectories(logDir, "receiver")
188-
}
184+
for _, logDir := range logDirectories {
185+
mkdirScript += "\n" + shell.MakeDirectories(logDir, "receiver")
189186
}
190187

191188
var logrotateCommand string

internal/collector/postgres.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ func PostgreSQLParameters(ctx context.Context,
6464

6565
// Log in a timezone the OpenTelemetry Collector understands.
6666
outParameters.Mandatory.Add("log_timezone", "UTC")
67-
68-
// TODO(logs): Remove this call and do it in [postgres.NewParameters] regardless of the gate.
69-
outParameters.Mandatory.Add("log_directory", fmt.Sprintf("%s/logs/postgres", postgres.DataStorage(inCluster)))
7067
}
7168
}
7269

internal/collector/postgres_test.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ func TestEnablePostgresLogging(t *testing.T) {
3333
config := NewConfig(nil)
3434
params := postgres.NewParameters()
3535

36-
// NOTE: This call is necessary only because the default "log_directory" is not absolute.
37-
PostgreSQLParameters(ctx, cluster, &params)
38-
EnablePostgresLogging(ctx, cluster, params.Mandatory, config)
36+
EnablePostgresLogging(ctx, cluster, params.Default, config)
3937

4038
result, err := config.ToYAML()
4139
assert.NilError(t, err)
@@ -297,9 +295,7 @@ service:
297295
config := NewConfig(cluster.Spec.Instrumentation)
298296
params := postgres.NewParameters()
299297

300-
// NOTE: This call is necessary only because the default "log_directory" is not absolute.
301-
PostgreSQLParameters(ctx, cluster, &params)
302-
EnablePostgresLogging(ctx, cluster, params.Mandatory, config)
298+
EnablePostgresLogging(ctx, cluster, params.Default, config)
303299

304300
result, err := config.ToYAML()
305301
assert.NilError(t, err)

internal/controller/postgrescluster/instance.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,10 @@ func (r *Reconciler) reconcileInstance(
11941194
// set includeLogrotate to true, but only if backups are enabled
11951195
// and local volumes are available.
11961196
includeLogrotate := backupsSpecFound && pgbackrest.RepoHostVolumeDefined(cluster)
1197+
1198+
// The log directories here do not include Postgres "log_directory" because it might be a subdirectory of "data_directory".
1199+
// In that case, the "log_directory" must be created *after* initdb runs, not during container startup here.
1200+
// TODO(sidecar): Create these directories sometime other than startup.
11971201
collector.AddToPod(ctx, cluster.Spec.Instrumentation, cluster.Spec.ImagePullPolicy, instanceConfigMap, &instance.Spec.Template,
11981202
[]corev1.VolumeMount{postgres.DataVolumeMount()}, pgPassword,
11991203
[]string{naming.PGBackRestPGDataLogPath}, includeLogrotate, true)

internal/postgres/parameters.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package postgres
77
import (
88
"fmt"
99
"maps"
10+
"path"
1011
"slices"
1112
"strings"
1213
)
@@ -48,16 +49,13 @@ func NewParameters() Parameters {
4849
// - https://www.postgresql.org/docs/current/auth-password.html
4950
parameters.Default.Add("password_encryption", "scram-sha-256")
5051

51-
// Explicitly use Postgres' default log directory. This value is relative to the "data_directory" parameter.
52+
// Log outside of the Postgres data directory by default.
5253
//
53-
// Controllers look at this parameter to grant group-write S_IWGRP on the directory.
54-
// Postgres does not grant this permission on the directories it creates.
55-
//
56-
// TODO(logs): A better default would be *outside* the data directory.
57-
// This parameter needs to be configurable and documented before the default can change.
54+
// When log files are inside the data directory, they are destroyed along with the data directory
55+
// during replica creation and major upgrades. Being outside also reduces the size of backups.
5856
//
5957
// PostgreSQL must be reloaded when changing this parameter.
60-
parameters.Default.Add("log_directory", "log")
58+
parameters.Default.Add("log_directory", path.Join(dataMountPath, "logs/postgres"))
6159

6260
// Pod "securityContext.fsGroup" ensures processes and filesystems agree on a GID;
6361
// use the same permissions for group and owner.

internal/postgres/parameters_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ func TestNewParameters(t *testing.T) {
2828
assert.DeepEqual(t, parameters.Default.AsMap(), map[string]string{
2929
"jit": "off",
3030

31-
"log_directory": "log",
32-
31+
"log_directory": "/pgdata/logs/postgres",
3332
"password_encryption": "scram-sha-256",
3433
})
3534
}

internal/postgres/reconcile_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,8 @@ initContainers:
288288
bootstrap_dir="${postgres_data_directory}_bootstrap"
289289
[[ -d "${bootstrap_dir}" ]] && postgres_data_directory="${bootstrap_dir}" && results 'bootstrap directory' "${bootstrap_dir}"
290290
dataDirectory "${postgres_data_directory}" || halt "$(permissions "${postgres_data_directory}" ||:)"
291-
[[ ! -f '/pgdata/pg11/PG_VERSION' ]] ||
292-
(mkdir -p '/pgdata/pg11/log' && { chmod 0775 '/pgdata/pg11/log' || :; }) ||
293-
halt "$(permissions '/pgdata/pg11/log' ||:)"
291+
(mkdir -p '/pgdata/logs/postgres' && { chmod 0775 '/pgdata/logs/postgres' '/pgdata/logs' || :; }) ||
292+
halt "$(permissions '/pgdata/logs/postgres' ||:)"
294293
(mkdir -p '/pgdata/patroni/log' && { chmod 0775 '/pgdata/patroni/log' '/pgdata/patroni' || :; }) ||
295294
halt "$(permissions '/pgdata/patroni/log' ||:)"
296295
(mkdir -p '/pgdata/pgbackrest/log' && { chmod 0775 '/pgdata/pgbackrest/log' '/pgdata/pgbackrest' || :; }) ||

0 commit comments

Comments
 (0)