diff --git a/pgtype/array_codec.go b/pgtype/array_codec.go index d7ccf0aa..c1863b32 100644 --- a/pgtype/array_codec.go +++ b/pgtype/array_codec.go @@ -47,7 +47,16 @@ func (c *ArrayCodec) FormatSupported(format int16) bool { } func (c *ArrayCodec) PreferredFormat() int16 { - return c.ElementType.Codec.PreferredFormat() + // The binary format should always be preferred for arrays if it is supported. Usually, this will happen automatically + // because most types that support binary prefer it. However, text, json, and jsonb support binary but prefer the text + // format. This is because it is simpler for jsonb and PostgreSQL can be significantly faster using the text format + // for text-like data types than binary. However, arrays appear to always be faster in binary. + // + // https://www.postgresql.org/message-id/CAMovtNoHFod2jMAKQjjxv209PCTJx5Kc66anwWvX0mEiaXwgmA%40mail.gmail.com + if c.ElementType.Codec.FormatSupported(BinaryFormatCode) { + return BinaryFormatCode + } + return TextFormatCode } func (c *ArrayCodec) PlanEncode(m *Map, oid uint32, format int16, value any) EncodePlan { diff --git a/pgtype/array_codec_test.go b/pgtype/array_codec_test.go index 797a5768..86d03b57 100644 --- a/pgtype/array_codec_test.go +++ b/pgtype/array_codec_test.go @@ -342,3 +342,11 @@ func TestArrayCodecDecodeTextArrayWithTextOfNULL(t *testing.T) { } }) } + +func TestArrayCodecDecodeTextArrayPrefersBinaryFormat(t *testing.T) { + defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) { + sd, err := conn.Prepare(ctx, "", `select '{"foo", "NULL", " NULL "}'::text[]`) + require.NoError(t, err) + require.Equal(t, int16(1), conn.TypeMap().FormatCodeForOID(sd.Fields[0].DataTypeOID)) + }) +}