diff --git a/pgtype/doc.go b/pgtype/doc.go index 7b4ed409..834aa0b6 100644 --- a/pgtype/doc.go +++ b/pgtype/doc.go @@ -139,5 +139,13 @@ implementing NumericScanner and passes that to the Codec. Map.Scan and Map.Encode are convenience methods that wrap Map.PlanScan and Map.PlanEncode. Determining how to scan or encode a particular type may be a time consuming operation. Hence the planning and execution steps of a conversion are internally separated. + +Reducing Compiled Binary Size + +pgx.QueryExecModeExec and pgx.QueryExecModeSimpleProtocol require the default PostgreSQL type to be registered for each +Go type used as a query parameter. By default pgx does this for all supported types and their array variants. If an +application does not use those query execution modes or manually registers the default PostgreSQL type for the types it +uses as query parameters it can use the build tag nopgxregisterdefaulttypes. This omits the default type registration +and reduces the compiled binary size by ~2MB. */ package pgtype diff --git a/pgtype/pgtype.go b/pgtype/pgtype.go index 160f18af..793ec28a 100644 --- a/pgtype/pgtype.go +++ b/pgtype/pgtype.go @@ -410,38 +410,6 @@ func NewMap() *Map { return m } -func registerDefaultPgTypeVariants[T any](m *Map, name string) { - arrayName := "_" + name - - var value T - m.RegisterDefaultPgType(value, name) // T - m.RegisterDefaultPgType(&value, name) // *T - - var sliceT []T - m.RegisterDefaultPgType(sliceT, arrayName) // []T - m.RegisterDefaultPgType(&sliceT, arrayName) // *[]T - - var slicePtrT []*T - m.RegisterDefaultPgType(slicePtrT, arrayName) // []*T - m.RegisterDefaultPgType(&slicePtrT, arrayName) // *[]*T - - var arrayOfT Array[T] - m.RegisterDefaultPgType(arrayOfT, arrayName) // Array[T] - m.RegisterDefaultPgType(&arrayOfT, arrayName) // *Array[T] - - var arrayOfPtrT Array[*T] - m.RegisterDefaultPgType(arrayOfPtrT, arrayName) // Array[*T] - m.RegisterDefaultPgType(&arrayOfPtrT, arrayName) // *Array[*T] - - var flatArrayOfT FlatArray[T] - m.RegisterDefaultPgType(flatArrayOfT, arrayName) // FlatArray[T] - m.RegisterDefaultPgType(&flatArrayOfT, arrayName) // *FlatArray[T] - - var flatArrayOfPtrT FlatArray[*T] - m.RegisterDefaultPgType(flatArrayOfPtrT, arrayName) // FlatArray[*T] - m.RegisterDefaultPgType(&flatArrayOfPtrT, arrayName) // *FlatArray[*T] -} - func (m *Map) RegisterType(t *Type) { m.oidToType[t.OID] = t m.nameToType[t.Name] = t diff --git a/pgtype/register_default_pg_types.go b/pgtype/register_default_pg_types.go new file mode 100644 index 00000000..be1ca4a1 --- /dev/null +++ b/pgtype/register_default_pg_types.go @@ -0,0 +1,35 @@ +//go:build !nopgxregisterdefaulttypes + +package pgtype + +func registerDefaultPgTypeVariants[T any](m *Map, name string) { + arrayName := "_" + name + + var value T + m.RegisterDefaultPgType(value, name) // T + m.RegisterDefaultPgType(&value, name) // *T + + var sliceT []T + m.RegisterDefaultPgType(sliceT, arrayName) // []T + m.RegisterDefaultPgType(&sliceT, arrayName) // *[]T + + var slicePtrT []*T + m.RegisterDefaultPgType(slicePtrT, arrayName) // []*T + m.RegisterDefaultPgType(&slicePtrT, arrayName) // *[]*T + + var arrayOfT Array[T] + m.RegisterDefaultPgType(arrayOfT, arrayName) // Array[T] + m.RegisterDefaultPgType(&arrayOfT, arrayName) // *Array[T] + + var arrayOfPtrT Array[*T] + m.RegisterDefaultPgType(arrayOfPtrT, arrayName) // Array[*T] + m.RegisterDefaultPgType(&arrayOfPtrT, arrayName) // *Array[*T] + + var flatArrayOfT FlatArray[T] + m.RegisterDefaultPgType(flatArrayOfT, arrayName) // FlatArray[T] + m.RegisterDefaultPgType(&flatArrayOfT, arrayName) // *FlatArray[T] + + var flatArrayOfPtrT FlatArray[*T] + m.RegisterDefaultPgType(flatArrayOfPtrT, arrayName) // FlatArray[*T] + m.RegisterDefaultPgType(&flatArrayOfPtrT, arrayName) // *FlatArray[*T] +} diff --git a/pgtype/register_default_pg_types_disabled.go b/pgtype/register_default_pg_types_disabled.go new file mode 100644 index 00000000..56fe7c22 --- /dev/null +++ b/pgtype/register_default_pg_types_disabled.go @@ -0,0 +1,6 @@ +//go:build nopgxregisterdefaulttypes + +package pgtype + +func registerDefaultPgTypeVariants[T any](m *Map, name string) { +}