Completes PostgreSQL log capture for the `pg-*` CI jobs by covering the Windows and macOS runners, which use embedded PostgreSQL (`scripts/embedded-pg/main.go`) instead of the Docker container used on Linux. This enables PostgreSQL's own `logging_collector` via `ALTER SYSTEM` (applied on the restart the script already performs), so the server persists logs to `<data>/log/postgresql.log`. The composite action then uploads that directory as a `postgres-logs-<job>-<os>` artifact on failure. <details> <summary>Notes</summary> - Default log verbosity is kept (no `log_statement=all`); the suite creates thousands of databases, matching the Linux jobs which set `TEST_POSTGRES_LOG_STATEMENT=none`. - Only `test-go-pg` exercises the embedded path (macOS/Windows). `test-go-pg-17` and `test-go-race-pg` are Linux-only, so the new step is a no-op there. - The Linux side of this (`docker logs` upload) is in #29217; this PR is independent and adds distinct steps. </details> <sub>Opened by Coder Agents on behalf of @spikecurtis.</sub>
121 lines
4.2 KiB
Go
121 lines
4.2 KiB
Go
// Start an embedded postgres database on port 5432. Used in CI on macOS and Windows.
|
|
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
|
|
)
|
|
|
|
func main() {
|
|
var customPath string
|
|
var cachePath string
|
|
flag.StringVar(&customPath, "path", "", "Optional custom path for postgres data directory")
|
|
flag.StringVar(&cachePath, "cache", "", "Optional custom path for embedded postgres binaries")
|
|
flag.Parse()
|
|
|
|
postgresPath := filepath.Join(os.TempDir(), "coder-test-postgres")
|
|
if customPath != "" {
|
|
postgresPath = customPath
|
|
}
|
|
if err := os.MkdirAll(postgresPath, os.ModePerm); err != nil {
|
|
log.Fatalf("Failed to create directory %s: %v", postgresPath, err)
|
|
}
|
|
if cachePath == "" {
|
|
cachePath = filepath.Join(postgresPath, "cache")
|
|
}
|
|
if err := os.MkdirAll(cachePath, os.ModePerm); err != nil {
|
|
log.Fatalf("Failed to create directory %s: %v", cachePath, err)
|
|
}
|
|
|
|
ep := embeddedpostgres.NewDatabase(
|
|
embeddedpostgres.DefaultConfig().
|
|
Version(embeddedpostgres.V16).
|
|
BinariesPath(filepath.Join(postgresPath, "bin")).
|
|
BinaryRepositoryURL("https://repo.maven.apache.org/maven2").
|
|
DataPath(filepath.Join(postgresPath, "data")).
|
|
RuntimePath(filepath.Join(postgresPath, "runtime")).
|
|
CachePath(cachePath).
|
|
Username("postgres").
|
|
Password("postgres").
|
|
Database("postgres").
|
|
Encoding("UTF8").
|
|
Port(uint32(5432)).
|
|
Logger(os.Stdout),
|
|
)
|
|
err := ep.Start()
|
|
if err != nil {
|
|
log.Fatalf("Failed to start embedded postgres: %v", err)
|
|
}
|
|
|
|
// Troubleshooting: list files in cachePath
|
|
if err := filepath.Walk(cachePath, func(path string, info os.FileInfo, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
switch {
|
|
case info.IsDir():
|
|
log.Printf("D: %s", path)
|
|
case info.Mode().IsRegular():
|
|
log.Printf("F: %s [%s] (%d bytes) %s", path, info.Mode().String(), info.Size(), info.ModTime().Format(time.RFC3339))
|
|
default:
|
|
log.Printf("Other: %s [%s] %s", path, info.Mode(), info.ModTime().Format(time.RFC3339))
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
log.Printf("Failed to list files in cachePath %s: %v", cachePath, err)
|
|
}
|
|
|
|
// We execute these queries instead of using the embeddedpostgres
|
|
// StartParams because it doesn't work on Windows. The library
|
|
// seems to have a bug where it sends malformed parameters to
|
|
// pg_ctl. It encloses each parameter in single quotes, which
|
|
// Windows can't handle.
|
|
// Related issue:
|
|
// https://github.com/fergusstrange/embedded-postgres/issues/145
|
|
paramQueries := []string{
|
|
`ALTER SYSTEM SET effective_cache_size = '1GB';`,
|
|
`ALTER SYSTEM SET fsync = 'off';`,
|
|
`ALTER SYSTEM SET full_page_writes = 'off';`,
|
|
`ALTER SYSTEM SET max_connections = '1000';`,
|
|
`ALTER SYSTEM SET shared_buffers = '1GB';`,
|
|
`ALTER SYSTEM SET synchronous_commit = 'off';`,
|
|
`ALTER SYSTEM SET client_encoding = 'UTF8';`,
|
|
// Enable PostgreSQL's own log collector so the running server persists
|
|
// logs to <data>/log/postgresql.log. The embedded-postgres library only
|
|
// flushes server output to its logger while this process is alive, so
|
|
// once this program exits the ongoing logs would otherwise be lost. CI
|
|
// uploads this directory as an artifact on failure. logging_collector is
|
|
// a postmaster-level setting, so it takes effect on the restart below.
|
|
`ALTER SYSTEM SET logging_collector = 'on';`,
|
|
`ALTER SYSTEM SET log_directory = 'log';`,
|
|
`ALTER SYSTEM SET log_filename = 'postgresql.log';`,
|
|
`ALTER SYSTEM SET log_rotation_age = '0';`,
|
|
`ALTER SYSTEM SET log_rotation_size = '0';`,
|
|
}
|
|
db, err := sql.Open("postgres", "postgres://postgres:postgres@127.0.0.1:5432/postgres?sslmode=disable")
|
|
if err != nil {
|
|
log.Fatalf("Failed to connect to embedded postgres: %v", err)
|
|
}
|
|
for _, query := range paramQueries {
|
|
if _, err := db.Exec(query); err != nil {
|
|
log.Fatalf("Failed to execute setup query %q: %v", query, err)
|
|
}
|
|
}
|
|
if err := db.Close(); err != nil {
|
|
log.Fatalf("Failed to close database connection: %v", err)
|
|
}
|
|
// We restart the database to apply all the parameters.
|
|
if err := ep.Stop(); err != nil {
|
|
log.Fatalf("Failed to stop embedded postgres after applying parameters: %v", err)
|
|
}
|
|
if err := ep.Start(); err != nil {
|
|
log.Fatalf("Failed to start embedded postgres after applying parameters: %v", err)
|
|
}
|
|
}
|