Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(pg,redshift,cockroachdb): read the session time zone once per statement (#21877)
Danny Xuc1e0d0efae29
diff --git a/backend/plugin/db/cockroachdb/cockroachdb.go b/backend/plugin/db/cockroachdb/cockroachdb.goindex 1fe1ad1638..7b662aaa33 100644--- a/backend/plugin/db/cockroachdb/cockroachdb.go+++ b/backend/plugin/db/cockroachdb/cockroachdb.go@@ -655,6 +655,7 @@ func (*Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string,startTime := time.Now()queryResult, err := func() (*v1pb.QueryResult, error) {if allQuery {+ location := util.PgSessionLocation(conn)var r *v1pb.QueryResultif err := crdb.Execute(func() error {rows, err := conn.QueryContext(ctx, statement)@@ -662,7 +663,7 @@ func (*Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement string,return err}defer rows.Close()- r, err = util.RowsToQueryResult(rows, makeValueByTypeName, convertValue, queryContext.MaximumSQLResultSize)+ r, err = util.RowsToQueryResult(rows, makeValueByTypeName, convertValueIn(location), queryContext.MaximumSQLResultSize)if err != nil {return err}diff --git a/backend/plugin/db/cockroachdb/query.go b/backend/plugin/db/cockroachdb/query.goindex fbcec88b1c..ab35e1d60f 100644--- a/backend/plugin/db/cockroachdb/query.go+++ b/backend/plugin/db/cockroachdb/query.go@@ -37,7 +37,15 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {var timeTzOID = fmt.Sprintf("%d", pgtype.TimetzOID)-func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {+// convertValueIn binds the row value converter to the location timestamptz values+// render in, which the caller reads from the session once per statement.+func convertValueIn(location *time.Location) func(string, *sql.ColumnType, any) *v1pb.RowValue {+ return func(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {+ return convertValue(typeName, columnType, value, location)+ }+}++func convertValue(typeName string, columnType *sql.ColumnType, value any, location *time.Location) *v1pb.RowValue {switch raw := value.(type) {case *sql.NullString:if raw.Valid {@@ -127,7 +135,7 @@ func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.},}}- zone, offset := raw.Time.Zone()+ zone, offset := raw.Time.In(location).Zone()return &v1pb.RowValue{Kind: &v1pb.RowValue_TimestampTzValue{TimestampTzValue: &v1pb.RowValue_TimestampTZ{diff --git a/backend/plugin/db/pg/pg.go b/backend/plugin/db/pg/pg.goindex d46ab27408..23918ae963 100644--- a/backend/plugin/db/pg/pg.go+++ b/backend/plugin/db/pg/pg.go@@ -845,12 +845,13 @@ func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement stringstartTime := time.Now()queryResult, err := func() (*v1pb.QueryResult, error) {if allQuery {+ location := util.PgSessionLocation(conn)rows, err := conn.QueryContext(ctx, statement)if err != nil {return nil, err}defer rows.Close()- r, err := util.RowsToQueryResult(rows, makeValueByTypeName, convertValue, queryContext.MaximumSQLResultSize)+ r, err := util.RowsToQueryResult(rows, makeValueByTypeName, convertValueIn(location), queryContext.MaximumSQLResultSize)if err != nil {return nil, err}diff --git a/backend/plugin/db/pg/query.go b/backend/plugin/db/pg/query.goindex f2b6f430e4..74b07eafcc 100644--- a/backend/plugin/db/pg/query.go+++ b/backend/plugin/db/pg/query.go@@ -39,7 +39,15 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {var timeTzOID = fmt.Sprintf("%d", pgtype.TimetzOID)-func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {+// convertValueIn binds the row value converter to the location timestamptz values+// render in, which the caller reads from the session once per statement.+func convertValueIn(location *time.Location) func(string, *sql.ColumnType, any) *v1pb.RowValue {+ return func(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {+ return convertValue(typeName, columnType, value, location)+ }+}++func convertValue(typeName string, columnType *sql.ColumnType, value any, location *time.Location) *v1pb.RowValue {switch raw := value.(type) {case *sql.NullString:if raw.Valid {@@ -119,7 +127,15 @@ func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.if scale == -1 {scale = 6}- return buildTimestamptzRowValue(typeName, raw.Time, int32(scale))+ // Only timestamptz carries a zone to render. A zone-less timestamp must keep+ // its wall clock, and buildTimestamptzRowValue formats those fields verbatim+ // for a date outside the protobuf timestamp range, so converting it there+ // would move the value rather than relabel it.+ t := raw.Time+ if typeName != "TIMESTAMP" {+ t = t.In(location)+ }+ return buildTimestamptzRowValue(typeName, t, int32(scale))}default:}diff --git a/backend/plugin/db/pg/query_test.go b/backend/plugin/db/pg/query_test.goindex d73e108601..e7986d9e44 100644--- a/backend/plugin/db/pg/query_test.go+++ b/backend/plugin/db/pg/query_test.go@@ -10,6 +10,7 @@ import ("github.com/bytebase/bytebase/backend/common/testcontainer"v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"+ "github.com/bytebase/bytebase/backend/plugin/db/util")func TestBuildTimestamptzRowValue(t *testing.T) {@@ -152,10 +153,11 @@ func TestPadZeroes(t *testing.T) {}// TestConvertValueTimestampFromDriver reads timestamps through the driver rather than-// from a constructed time.Time, covering the two behaviors Bytebase's pgx fork adds and-// upstream does not: timestamptz values arrive in the session time zone, and-// ColumnType.DecimalSize reports the column's fractional-second scale. A pgx upgrade-// that loses either patch fails here instead of silently changing query results.+// from a constructed time.Time, exercising the session time zone the way QueryConn does:+// read once per statement, then applied to every timestamptz in the result. It also+// covers ColumnType.DecimalSize reporting the column's fractional-second scale, which+// Bytebase's pgx fork adds and upstream does not, so a pgx upgrade that loses that patch+// fails here instead of silently changing query results.func TestConvertValueTimestampFromDriver(t *testing.T) {const (sessionZone = "Asia/Shanghai"@@ -172,16 +174,22 @@ func TestConvertValueTimestampFromDriver(t *testing.T) {_, err = conn.ExecContext(ctx, "SET TimeZone TO '"+sessionZone+"'")require.NoError(t, err)+ // QueryConn reads the zone once per statement, after any preceding SET.+ location := util.PgSessionLocation(conn)+ require.Equal(t, sessionZone, location.String())+rows, err := conn.QueryContext(ctx, `SELECT'2026-03-04 05:06:07.891234+00'::timestamptz AS tz_default,'2026-03-04 05:06:07.891234+00'::timestamptz(3) AS tz_scale3,- '2026-03-04 05:06:07.891234'::timestamp AS ts_default`)+ '2026-03-04 05:06:07.891234'::timestamp AS ts_default,+ '10000-01-01 00:00:00+00'::timestamptz AS tz_out_of_range,+ '10000-01-01 00:00:00'::timestamp AS ts_out_of_range`)require.NoError(t, err)defer rows.Close()columnTypes, err := rows.ColumnTypes()require.NoError(t, err)- require.Len(t, columnTypes, 3)+ require.Len(t, columnTypes, 5)require.True(t, rows.Next())dest := make([]any, len(columnTypes))@@ -193,7 +201,7 @@ func TestConvertValueTimestampFromDriver(t *testing.T) {values := make(map[string]*v1pb.RowValue, len(columnTypes))for i, columnType := range columnTypes {- values[columnType.Name()] = convertValue(columnType.DatabaseTypeName(), columnType, dest[i])+ values[columnType.Name()] = convertValue(columnType.DatabaseTypeName(), columnType, dest[i], location)}tzDefault := values["tz_default"].GetTimestampTzValue()@@ -216,4 +224,11 @@ func TestConvertValueTimestampFromDriver(t *testing.T) {require.NotNil(t, tsDefault)require.EqualValues(t, 6, tsDefault.Accuracy)require.Equal(t, "2026-03-04T05:06:07.891234Z", tsDefault.GoogleTimestamp.AsTime().Format(time.RFC3339Nano))++ // A date past the protobuf timestamp range falls back to a formatted string built+ // from wall-clock fields, which is the one path where converting the location moves+ // the value instead of relabeling it. timestamptz renders in the session zone; a+ // zone-less timestamp keeps the stored wall clock and gains no offset.+ require.Equal(t, "10000-01-01 08:00:00+08:00", values["tz_out_of_range"].GetStringValue())+ require.Equal(t, "10000-01-01 00:00:00", values["ts_out_of_range"].GetStringValue())}diff --git a/backend/plugin/db/redshift/query.go b/backend/plugin/db/redshift/query.goindex 3c8e5a852a..84a36355f6 100644--- a/backend/plugin/db/redshift/query.go+++ b/backend/plugin/db/redshift/query.go@@ -37,7 +37,15 @@ func makeValueByTypeName(typeName string, _ *sql.ColumnType) any {var timeTzOID = fmt.Sprintf("%d", pgtype.TimetzOID)-func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {+// convertValueIn binds the row value converter to the location timestamptz values+// render in, which the caller reads from the session once per statement.+func convertValueIn(location *time.Location) func(string, *sql.ColumnType, any) *v1pb.RowValue {+ return func(typeName string, columnType *sql.ColumnType, value any) *v1pb.RowValue {+ return convertValue(typeName, columnType, value, location)+ }+}++func convertValue(typeName string, columnType *sql.ColumnType, value any, location *time.Location) *v1pb.RowValue {switch raw := value.(type) {case *sql.NullString:if raw.Valid {@@ -127,7 +135,7 @@ func convertValue(typeName string, columnType *sql.ColumnType, value any) *v1pb.},}}- zone, offset := raw.Time.Zone()+ zone, offset := raw.Time.In(location).Zone()return &v1pb.RowValue{Kind: &v1pb.RowValue_TimestampTzValue{TimestampTzValue: &v1pb.RowValue_TimestampTZ{diff --git a/backend/plugin/db/redshift/redshift.go b/backend/plugin/db/redshift/redshift.goindex 5b9fee3995..b74a1a43f6 100644--- a/backend/plugin/db/redshift/redshift.go+++ b/backend/plugin/db/redshift/redshift.go@@ -401,12 +401,13 @@ func (d *Driver) QueryConn(ctx context.Context, conn *sql.Conn, statement stringstartTime := time.Now()queryResult, err := func() (*v1pb.QueryResult, error) {if allQuery {+ location := util.PgSessionLocation(conn)rows, err := conn.QueryContext(ctx, statement)if err != nil {return nil, err}defer rows.Close()- r, err := util.RowsToQueryResult(rows, makeValueByTypeName, convertValue, queryContext.MaximumSQLResultSize)+ r, err := util.RowsToQueryResult(rows, makeValueByTypeName, convertValueIn(location), queryContext.MaximumSQLResultSize)if err != nil {return nil, err}diff --git a/backend/plugin/db/util/pgsession.go b/backend/plugin/db/util/pgsession.gonew file mode 100644index 0000000000..a3de194865--- /dev/null+++ b/backend/plugin/db/util/pgsession.go@@ -0,0 +1,58 @@+package util++import (+ "database/sql"+ "sync"+ "time"++ "github.com/jackc/pgx/v5/stdlib"+)++// pgLocations caches locations by PostgreSQL TimeZone setting. time.LoadLocation+// reparses the zoneinfo file on every call for a named zone, which costs microseconds+// and kilobytes; a query result holds one location, often for every cell.+var pgLocations sync.Map++// PgSessionLocation reports the location to render timestamptz values in for the next+// statement on conn: the one named by the session's TimeZone, which the server reports+// on the connection and resends whenever a statement changes it.+//+// Read it once per statement, immediately before running that statement. A statement+// then sees any SET TimeZone that ran before it, and one result set decodes under one+// location, because PostgreSQL reports a change at command completion, after the rows.+//+// PostgreSQL also accepts POSIX specifications such as "+08:00" and+// "EST5EDT,M3.2.0/2,M11.1.0/2", which time.LoadLocation rejects. Those fall back to UTC,+// keeping the instant correct, rather than failing every timestamptz read on the+// instance.+func PgSessionLocation(conn *sql.Conn) *time.Location {+ location := time.UTC+ // A failure here leaves the zone unknown, which UTC already covers.+ _ = conn.Raw(func(driverConn any) error {+ pgxConn, ok := driverConn.(*stdlib.Conn)+ if !ok {+ return nil+ }+ location = pgLocation(pgxConn.Conn().PgConn().ParameterStatus("TimeZone"))+ return nil+ })+ return location+}++func pgLocation(name string) *time.Location {+ if name == "" || name == "UTC" {+ return time.UTC+ }+ if cached, ok := pgLocations.Load(name); ok {+ location, ok := cached.(*time.Location)+ if ok {+ return location+ }+ }+ location, err := time.LoadLocation(name)+ if err != nil {+ location = time.UTC+ }+ pgLocations.Store(name, location)+ return location+}diff --git a/backend/plugin/db/util/pgsession_test.go b/backend/plugin/db/util/pgsession_test.gonew file mode 100644index 0000000000..d1d79ca9b3--- /dev/null+++ b/backend/plugin/db/util/pgsession_test.go@@ -0,0 +1,58 @@+package util++import (+ "testing"+ "time"++ "github.com/stretchr/testify/require"+)++func TestPgLocation(t *testing.T) {+ // A winter instant, so the zones with DST report their standard offset.+ instant := time.Date(2026, 1, 15, 12, 0, 0, 0, time.UTC)++ tests := []struct {+ name string+ setting string+ wantOffset int+ }{+ {name: "UTC", setting: "UTC", wantOffset: 0},+ {name: "unreported setting", setting: "", wantOffset: 0},+ {name: "whole hour", setting: "Asia/Shanghai", wantOffset: 8 * 60 * 60},+ {name: "half hour", setting: "Asia/Kolkata", wantOffset: 5*60*60 + 30*60},+ {name: "three quarter hour", setting: "Pacific/Chatham", wantOffset: 13*60*60 + 45*60},+ {name: "negative", setting: "America/New_York", wantOffset: -5 * 60 * 60},+ {name: "posix name", setting: "EST5EDT", wantOffset: -5 * 60 * 60},+ // PostgreSQL accepts these; time.LoadLocation does not.+ {name: "posix offset falls back to UTC", setting: "+08:00", wantOffset: 0},+ {name: "posix rules fall back to UTC", setting: "EST5EDT,M3.2.0/2,M11.1.0/2", wantOffset: 0},+ {name: "unknown zone falls back to UTC", setting: "Mars/Olympus_Mons", wantOffset: 0},+ }++ for _, test := range tests {+ t.Run(test.name, func(t *testing.T) {+ _, offset := instant.In(pgLocation(test.setting)).Zone()+ require.Equal(t, test.wantOffset, offset)+ })+ }+}++func TestPgLocationCachesByName(t *testing.T) {+ first := pgLocation("Australia/Eucla")+ require.Same(t, first, pgLocation("Australia/Eucla"))++ // A fallback is cached too, so a POSIX setting costs one failed load.+ fallback := pgLocation("-04:30")+ require.Same(t, time.UTC, fallback)+ require.Same(t, fallback, pgLocation("-04:30"))+}++func TestPgLocationAppliesDaylightSavingPerInstant(t *testing.T) {+ location := pgLocation("America/New_York")++ _, winter := time.Date(2026, 1, 15, 12, 0, 0, 0, time.UTC).In(location).Zone()+ _, summer := time.Date(2026, 7, 15, 12, 0, 0, 0, time.UTC).In(location).Zone()++ require.Equal(t, -5*60*60, winter)+ require.Equal(t, -4*60*60, summer)+}diff --git a/backend/store/sample_instance_test.go b/backend/store/sample_instance_test.goindex ba4fd79d11..8b238d8ef6 100644--- a/backend/store/sample_instance_test.go+++ b/backend/store/sample_instance_test.go@@ -88,7 +88,8 @@ func TestSampleInstanceSetupPermanentLifecycle(t *testing.T) {require.True(t, activated)active, err := stores.GetSampleInstanceSetup(ctx, "workspace-a")require.NoError(t, err)- require.Equal(t, activatedAt, *active.ActivatedAt)+ // Compare instants: a timestamptz read carries the process's location, not UTC.+ require.WithinDuration(t, activatedAt, *active.ActivatedAt, 0)require.Nil(t, active.ExpiresAt)count, err := stores.CountSampleInstanceSetupsForCleanup(@@ -184,7 +185,7 @@ func TestSampleInstanceSetupDeletedRowRemainsTombstone(t *testing.T) {deleted, err := stores.GetSampleInstanceSetup(ctx, "workspace-a")require.NoError(t, err)- require.Equal(t, cleanupAt, *deleted.DeletedAt)+ require.WithinDuration(t, cleanupAt, *deleted.DeletedAt, 0)again, created, err := stores.ReserveSampleInstanceSetup(ctx, setup)require.NoError(t, err)require.False(t, created)diff --git a/backend/tests/sample_project_instance_test.go b/backend/tests/sample_project_instance_test.goindex 6b5281a795..42fe32a9cb 100644--- a/backend/tests/sample_project_instance_test.go+++ b/backend/tests/sample_project_instance_test.go@@ -89,7 +89,8 @@ func TestPrepareSampleProjectInstanceLifecycle(t *testing.T) {require.Equal(t, allocation.Database, record.database)require.Equal(t, allocation.Role, record.role)require.False(t, record.createdAt.IsZero())- require.Equal(t, now.Add(7*24*time.Hour), record.expiresAt)+ // Compare instants: a timestamptz read carries the process's location, not UTC.+ require.WithinDuration(t, now.Add(7*24*time.Hour), record.expiresAt, 0)require.Nil(t, record.deletedAt)database, err := fixture.store.GetDatabase(ctx, &store.FindDatabaseMessage{diff --git a/go.mod b/go.modindex 6e130f5b7b..1da3cd398c 100644--- a/go.mod+++ b/go.mod@@ -377,7 +377,7 @@ replace (// Other fixes.github.com/github/gh-ost => github.com/bytebase/gh-ost2 v1.1.7-0.20261102125703-6ff100bb26d5- github.com/jackc/pgx/v5 => github.com/bytebase/pgx/v5 v5.0.0-20261103063743-9142dbf56d46+ github.com/jackc/pgx/v5 => github.com/bytebase/pgx/v5 v5.0.0-20261103142809-663c807705c4github.com/mattn/go-oci8 => github.com/bytebase/go-obo v0.0.0-20231026081615-705a7fffbfd2diff --git a/go.sum b/go.sumindex 648ad9305e..2d21f11f8a 100644--- a/go.sum+++ b/go.sum@@ -263,8 +263,8 @@ github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0 h1:bJ8NSf2K+github.com/bytebase/lsp-protocol v0.0.0-20250324071136-1586d0c10ff0/go.mod h1:BEWMfiZtqshdmulKJcIsWb45MUaEvDO4CROcgi8dfFg=github.com/bytebase/omni v0.0.0-20261102094319-61f070707dc7 h1:3rwQ9dpUCGv19YJJfxUivF9ZwiFFa0QCMLBr/YRP+Jc=github.com/bytebase/omni v0.0.0-20261102094319-61f070707dc7/go.mod h1:9XZE1hqtvr09Oc+DdLu1/yCb8MDcoy/EPdA4AONDHlE=-github.com/bytebase/pgx/v5 v5.0.0-20261103063743-9142dbf56d46 h1:HySG0FEhJefrs8TzW1cGCjReld58q/1YRElukKsKJ6g=-github.com/bytebase/pgx/v5 v5.0.0-20261103063743-9142dbf56d46/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds=+github.com/bytebase/pgx/v5 v5.0.0-20261103142809-663c807705c4 h1:CVgGkwGGV1wLd9Fju0XcY1cac2sf7IKOLA80bWGUwG8=+github.com/bytebase/pgx/v5 v5.0.0-20261103142809-663c807705c4/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds=github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94 h1:6PNsuNmSqCuR8Z616uQLzDIvujWCFsKzeVc3ECmVubk=github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94/go.mod h1:SQliXeA7Dhkt//vS29v3zpbEwoa+zb2Cn5xj5uO4K5U=github.com/bytebase/tidb v0.0.0-20251104040057-d29df9dd1b3b h1:FIMLXCmledlGHw7NzIT2BGVflB2ZNamudc+QtTNITH0=