GiST intarray build speedup: two-arg fast path

From: Kirill Reshke <reshkekirill(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: GiST intarray build speedup: two-arg fast path
Date: 2026-10-30 10:23:22
Message-ID: CALdSSPj7DROqyFL+jtzX23d5jwj4cPsOHBUzoaRcspVH8H1Yrg@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi.

While profiling index builds for different AMs, I noticed that GiST
index for inarray
speds much time in in g_int_union and qsort inside inner_int_union for
some of benchmark cases.This simple diff gives 1.2-1.5x speedup:

diff --git a/contrib/intarray/_int_gist.c b/contrib/intarray/_int_gist.c
index 98711ac54e2..966409e9582 100644
--- a/contrib/intarray/_int_gist.c
+++ b/contrib/intarray/_int_gist.c
@@ -130,6 +130,14 @@ g_int_union(PG_FUNCTION_ARGS)
ArrayType *res;
int totlen = 0;

+ if (entryvec->n == 2)
+ {
+ res = inner_int_union(GETENTRY(entryvec, 0),
+
GETENTRY(entryvec, 1));
+ *size = VARSIZE(res);
+ PG_RETURN_POINTER(res);
+ }
+
for (i = 0; i < entryvec->n; i++)
{
ArrayType *ent = GETENTRY(entryvec, i);

Simple case where this helps (SQL was generated by my fuzzer)

CREATE EXTENSION IF NOT EXISTS intarray;
CREATE TABLE t AS
SELECT i id,
ARRAY(SELECT DISTINCT ((hashint4(i*43+j)::bigint)%10000)::int
FROM generate_series(1,30) j) a
FROM generate_series(1,5000) i;

CREATE INDEX ON t USING gist(a gist__int_ops);

index create time 46s->26s.
I did run some other benchmarks and it didn't show any regressions.
Off-list Andrey x4m proposed to try this for n = 3 4 5 etc, which also
shows some speedup (i. don't have full bench result yet).

WDYT?

--
Best regards,
Kirill Reshke

Browse pgsql-hackers by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2026-10-30 10:33:55 RE: Fix "unexpected logical decoding status change" error; from concurrent logical decoding activation
Previous Message Nitin Jadhav 2026-10-30 10:13:44 Re: Reporting WAL replay progress during pre-consistency standby reovery