From 7682e15797413d7fd05803c1c2028ccbc9b3b3e8 Mon Sep 17 00:00:00 2001 From: Matvey Gubanikhin Date: Thu, 19 Nov 2026 13:52:39 +0300 Subject: [PATCH 1/2] Add ability to specify page size when creating database. --- conn_test.go | 48 +++++++++++++++++++++++++++++++++++++++++++ fbconn/config.go | 23 +++++++++++++++++++++ fbconn/config_test.go | 35 +++++++++++++++++++++++++++++++ fbconn/fbconn.go | 7 +++++++ 4 files changed, 113 insertions(+) diff --git a/conn_test.go b/conn_test.go index 60cf4eda..01d37ada 100644 --- a/conn_test.go +++ b/conn_test.go @@ -79,6 +79,54 @@ func TestCreateDatabase(t *testing.T) { assert.Equal(t, int32(1), result) } +func TestCreateDatabaseWithPageSize(t *testing.T) { + t.Parallel() + + for _, pageSize := range []int{8192, 16384} { + t.Run(fmt.Sprintf("%d", pageSize), func(t *testing.T) { + t.Parallel() + + ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) + defer cancel() + + config := newCreateDatabaseTestConfig(t, "page-size.fdb") + config.PageSize = pageSize + + conn, err := fbx.CreateDatabaseConfig(ctx, config) + require.NoError(t, err) + defer closeConn(t, conn) + + var actualPageSize int + err = conn.QueryRow(ctx, "select mon$page_size from mon$database").Scan(&actualPageSize) + require.NoError(t, err) + assert.Equal(t, pageSize, actualPageSize) + }) + } +} + +func TestCreateDatabaseWithInvalidPageSize(t *testing.T) { + t.Parallel() + + for _, pageSize := range []int{-1, 12345, 65536} { + t.Run(fmt.Sprintf("%d", pageSize), func(t *testing.T) { + t.Parallel() + + ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second) + defer cancel() + + config := newCreateDatabaseTestConfig(t, "invalid-page-size.fdb") + config.PageSize = pageSize + + conn, err := fbx.CreateDatabaseConfig(ctx, config) + require.ErrorContains(t, err, "only the following values are accepted") + require.Nil(t, conn) + + _, err = os.Stat(config.Database) + require.ErrorIs(t, err, os.ErrNotExist) + }) + } +} + func TestDropDatabase(t *testing.T) { t.Parallel() diff --git a/fbconn/config.go b/fbconn/config.go index fd37e524..a8eae1dd 100644 --- a/fbconn/config.go +++ b/fbconn/config.go @@ -8,6 +8,7 @@ import ( "math" "net" "os" + "slices" "strconv" "strings" "time" @@ -56,6 +57,10 @@ type Config struct { MaxBlobCacheSize int FetchBatchSize int + // PageSize specifies the requested database page size in bytes. + // Zero leaves the page size unspecified. + PageSize int + // ConstructorOptions allows to pass callbacks for setting parameters to authentication plugins. ConstructorOptions map[string][]auth.ConstructorOption @@ -361,6 +366,16 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con UseStreamBlobs: convertToBool(settings["use_stream_blobs"], true), FetchBatchSize: DefaultFetchBatchSize, } + if s, ok := settings["page_size"]; ok { + value, err := parsePositiveIntSetting(s) + if err != nil { + return nil, &ParseConfigError{ConnString: connString, msg: "invalid page_size", err: err} + } + if !isValidPageSize(value) { + return nil, &ParseConfigError{ConnString: connString, msg: "invalid page_size", err: errInvalidPageSize} + } + config.PageSize = value + } if s, ok := settings["max_inline_blob_size"]; ok { value, err := parsePositiveIntSetting(s) if err != nil { @@ -434,6 +449,7 @@ func ParseConfigWithOptions(connString string, options ParseConfigOptions) (*Con "max_blob_cache_size": {}, "fetch_batch_size": {}, "target_session_attrs": {}, + "page_size": {}, } for k, v := range settings { @@ -790,3 +806,10 @@ func convertToBool(s string, defaultValue bool) bool { } return v } + +var validPageSizeValues = []int{4096, 8192, 16384, 32768} +var errInvalidPageSize = fmt.Errorf("only the following values are accepted: %v", validPageSizeValues) + +func isValidPageSize(size int) bool { + return slices.Contains(validPageSizeValues, size) +} diff --git a/fbconn/config_test.go b/fbconn/config_test.go index 99227ff2..137e4f97 100644 --- a/fbconn/config_test.go +++ b/fbconn/config_test.go @@ -522,6 +522,41 @@ func TestParseConfigWireCompression(t *testing.T) { } } +func TestParseConfigPageSize(t *testing.T) { + t.Parallel() + + tests := []struct { + value string + want int + wantErr bool + }{ + {value: "4096", want: 4096}, + {value: "8192", want: 8192}, + {value: "16384", want: 16384}, + {value: "32768", want: 32768}, + {value: "12345", wantErr: true}, + {value: "invalid", wantErr: true}, + } + + for _, tt := range tests { + for _, connString := range []string{ + "page_size=" + tt.value, + "firebird://localhost/testdb?page_size=" + tt.value, + } { + t.Run(connString, func(t *testing.T) { + config, err := fbconn.ParseConfig(connString) + if tt.wantErr { + require.Error(t, err) + return + } + + require.NoError(t, err) + assert.Equal(t, tt.want, config.PageSize) + }) + } + } +} + func TestParseConfigKVTrailingWhitespace(t *testing.T) { config, err := fbconn.ParseConfig("host=localhost port=3050 database=testdb \t\n") require.NoError(t, err) diff --git a/fbconn/fbconn.go b/fbconn/fbconn.go index 3c0d2e2a..275b1690 100644 --- a/fbconn/fbconn.go +++ b/fbconn/fbconn.go @@ -889,6 +889,13 @@ connectLoop: dpb.PutInt32(fbproto.Isc_dpb_sql_dialect, 3) if config.CreateDatabase { dpb.PutInt32(fbproto.Isc_dpb_set_db_sql_dialect, 3) + if !isValidPageSize(config.PageSize) && config.PageSize != 0 { + _ = fbConn.conn.Close() + return nil, newPerDialConnectError("database creation error", errInvalidPageSize) + } + if config.PageSize > 0 { + dpb.PutInt32(fbproto.Isc_dpb_page_size, int32(config.PageSize)) + } } dpb.PutString(fbproto.Isc_dpb_lc_ctype, config.Encoding) if config.User != "" { From 5c87fab9a3adadaab07aa84b993bc2de9a955308 Mon Sep 17 00:00:00 2001 From: Matvey Gubanikhin Date: Fri, 20 Nov 2026 09:30:21 +0300 Subject: [PATCH 2/2] Move page size validation to connectConfig. --- fbconn/fbconn.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/fbconn/fbconn.go b/fbconn/fbconn.go index 275b1690..f4c4a077 100644 --- a/fbconn/fbconn.go +++ b/fbconn/fbconn.go @@ -250,6 +250,12 @@ func connectConfig(ctx context.Context, config *Config, target connectTarget) (* var allErrors []error + if config.CreateDatabase && + !isValidPageSize(config.PageSize) && + config.PageSize != 0 { + return nil, errInvalidPageSize + } + connectConfigs, errs := buildConnectOneConfigs(ctx, config) if len(errs) > 0 { allErrors = append(allErrors, errs...) @@ -889,10 +895,6 @@ connectLoop: dpb.PutInt32(fbproto.Isc_dpb_sql_dialect, 3) if config.CreateDatabase { dpb.PutInt32(fbproto.Isc_dpb_set_db_sql_dialect, 3) - if !isValidPageSize(config.PageSize) && config.PageSize != 0 { - _ = fbConn.conn.Close() - return nil, newPerDialConnectError("database creation error", errInvalidPageSize) - } if config.PageSize > 0 { dpb.PutInt32(fbproto.Isc_dpb_page_size, int32(config.PageSize)) }