From 4484831550355ee24ac994b0f262cba7de797d9e Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Fri, 10 Feb 2023 20:21:25 -0600 Subject: [PATCH] Prefer binary format for arrays This improves performance decoding text[]. --- pgtype/array_codec.go | 11 ++++++++++- pgtype/array_codec_test.go | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) 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)) + }) +}