Add build tag to skip default PG type registration

https://github.com/jackc/pgx/issues/1273#issuecomment-1207338136
pull/1281/head
Jack Christensen 2022-08-13 08:09:44 -05:00
parent 906f709e0c
commit 8256ab147f
4 changed files with 49 additions and 32 deletions

View File

@ -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

View File

@ -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

View File

@ -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]
}

View File

@ -0,0 +1,6 @@
//go:build nopgxregisterdefaulttypes
package pgtype
func registerDefaultPgTypeVariants[T any](m *Map, name string) {
}