Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vdDJY-00FQgh-0P for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Jan 2026 20:06:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vdDJX-00APcA-0C for pgsql-hackers@arkaria.postgresql.org; Tue, 06 Jan 2026 20:05:59 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vdDJW-00APc0-1v for pgsql-hackers@lists.postgresql.org; Tue, 06 Jan 2026 20:05:59 +0000 Received: from mail-qt1-x82b.google.com ([2607:f8b0:4864:20::82b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vdDJV-004aQV-2H for pgsql-hackers@postgresql.org; Tue, 06 Jan 2026 20:05:58 +0000 Received: by mail-qt1-x82b.google.com with SMTP id d75a77b69052e-4eda057f3c0so13539541cf.2 for ; Tue, 06 Jan 2026 12:05:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1767729957; x=1768334757; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=f38VoUmvZMhkQ+lm/yrrrz+CP8ySdBZouniv75JN+rI=; b=CKsKSsXCRMT9M/saEVTxffVh4ZkjtWIkMfFnlFv2R3qnQ9CTFMJyJMmBjI1FUSJH0+ lplIcGbVzwMGV3lt2ttpuYkVPdCXFYWmPDkzYDACLLOHTA1GG8aS1dcZ1b9ykJAfVMUk LBL5aeauwhfAWULFt0FGFudrXDZma18OyuNh32pWtslYDzuDz1OVs2+it5xUqGjxC6w0 gk/DYsZMD0In9svxzWiJrMVaXPNeLQ3ynyATpMdyxw5j5P/h1pLRAspwvBx8Am12FK03 IYzmHp4/ZtzX9137gu3/8TbXb7ms92symeBFBR5WOfQTL2ueNNxvtFlTAoawkwjg6uu6 MYIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1767729957; x=1768334757; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=f38VoUmvZMhkQ+lm/yrrrz+CP8ySdBZouniv75JN+rI=; b=LFJ6w2+kDtvUYfs99OEu6YwSrKN6ai6JJ7yBQY8GHTd79bHup4sikF3UQXf7a6FHbo wmcdQXdQA/3fWuu5sBKXF10Y2qvqHonVmXuTHiJfnreoatVfL1E4kpcEEkuS7exdDTgq o1p80nub99n8Wday38Nb1JAC8mRjeYrD86cITqz8tkIBlwhjbjC8eyuVIUu44IBTN5// rzLiv6q9yw4ZPF3W3e/T5r+Axku8S6rI9wx/YbMC1kcUpRw784tdLIpR62vzApDYn+ne MaMrrW+viAZQwrcnxeyMTSEn3iMeWRgTaYYKbEPr4Mtd+dO1mojFW1gQvklEbbGXDZcU jnTw== X-Forwarded-Encrypted: i=1; AJvYcCXawglJG4h1QSOwsBNnRmeGXVgWT9NF3M2/roNEF3bJsFW3Kc/qGRfSldS6cHXS9OgOdQQ6wrKYgnwESj7t@postgresql.org X-Gm-Message-State: AOJu0YzEz+s/gcbnMg7ycylgJmg+sDADZpBmb1gt0EwmGlPdA5o9WRQH x0tpXAJ/dB0w+quL9RabZvYw34TU0y3neatwmJOWaNkXlO5/xv4Pqa24M6sbp98QrcQCqQ47HI1 Vy4pxyvXcS8CdmbZDtxnpFkyZmj2QhrI= X-Gm-Gg: AY/fxX6ZdgLuDJ6omzdfqGffj1R0wn1EFOtnH+leRZ2tvMQO7RuTd80/lPkzho1E/oX HRCE1CEEU4vdydx34zJtSVK/nWcxN+KLeHe3rTRbFviVqPxLVeKNZcDTEeYRcW5DribbMzsl3R2 5rAVS+N3F3I4cPryiJZbXBt72uF6l8T8BDjhB0N6yNnXCsHOAvF/UnXCQ4AUhKwpvAfKMGX7vfU 99XP/66+J0QtFrCJ4FZ8M7lUiRR+q0L9gK8WyH2e1qZXf9bCLREYiFVzWZuPlPcYhA7GN8ygT4x 72UvF6rgFJGkwkx/tuxR6WqP/M1OaWTb4fmplArXz/a3wWCTFfVu3qCxiWkDv3Trq95IIg== X-Google-Smtp-Source: AGHT+IEU55rZP9q9C0cFlY+ewmGgAGkktJEKnqa6ctfn1K2m9iKSxvCnHR01nKfxO8ubsf57HEj8o1waN3gJRw+sIeY= X-Received: by 2002:ac8:58d5:0:b0:4f1:c66d:4c98 with SMTP id d75a77b69052e-4ffb482f4afmr2252341cf.24.1767729956434; Tue, 06 Jan 2026 12:05:56 -0800 (PST) MIME-Version: 1.0 References: <5d366878-2007-4d31-861e-19294b7a583b@gmail.com> <9ac3931a-180e-4283-a7a8-05eb66099206@iki.fi> In-Reply-To: <9ac3931a-180e-4283-a7a8-05eb66099206@iki.fi> From: Kirill Reshke Date: Wed, 7 Jan 2026 01:05:45 +0500 X-Gm-Features: AQt7F2q0XzAJNbnfFphjD2BeVrZ4PZlkteIIqkVq5Sj1nwYaftB6kH8Afk3kRs4 Message-ID: Subject: Re: Reduce build times of pg_trgm GIN indexes To: Heikki Linnakangas Cc: David Geier , pgsql-hackers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 6 Jan 2026 at 22:00, Heikki Linnakangas wrote: > > On 05/01/2026 17:01, David Geier wrote: > > The script I used for testing is attached. I ran CREATE INDEX three > > times and took the fastest run. I'm getting the following results on my > > i9-13950HX dev laptop in release build: > > > > Data set | Patched (ms) | Master (ms) | Speedup > > --------------------|--------------|--------------|---------- > > movies(plot) | 3,409 | 10,311 | 3.02x > > lineitem(l_comment) | 161,569 | 256,986 | 1.59x > > > > Impressive speedup! > > > The attached patches do the following: > > > > - v1-0001-Inline-ginCompareAttEntries.patch: Inline > > ginCompareAttEntries() which is very frequently called by the GIN code. > > Looks good. > > > - v1-0002-Optimized-comparison-functions.patch: Use FunctionCallInvoke() > > instead of FunctionCall2Coll(). This saves a bunch of per-comparison > > setup code, such as calling InitFunctionCallInfoData(). > > You lose the check for NULL result with this. That's probably still > worth checking. > > > - v1-0003-Use-sort_template.h.patch: Use sort_template.h instead of > > qsort(), to inline calls to the sort comparator. This is an interim step > > that is further improved on by patch 0006. > > ok > > > - v1-0004-Avoid-dedup-and-sort-in-ginExtractEntries.patch > > ginExtractEntries() deduplicates and sorts the entries returned from the > > extract value function. In case of pg_trgm, that is completely redundant > > because the trigrams are already deduplicated and sorted. The current > > version of this patch is just to demonstrate the potential. We need to > > think about what we want here. Ideally, we would require the extraction > > function to provide the entries deduplicated and sorted. Alternatively, > > we could indicate to ginExtractEntries() if the entries are already > > deduplicated and sorted. If we don't want to alter the signature of the > > extract value function, we could e.g. use the MSB of the nentries argument. > > Yeah, this seems wrong as it is. You're assuming that if the extract > function returns nullFlags == NULL, the array is already sorted and deduped. > > > - v1-0005-Make-btint4cmp-branchless.patch: Removes branches from > > btint4cmp(), which is heavily called from the GIN code. This might as > > well have benefit in other parts of the code base. > > Seems reasonable. > > > v1-0006-Use-radix-sort.patch: Replace the sort_template.h-based qsort() > > with radix sort. For the purpose of demonstrating the possible gains, > > I've only replaced the signed variant for now. I've also tried using > > simplehash.h for deduplicating followed by a sort_template.h-based sort. > > But that was slower. > > Ok. > > > v1-0007-Faster-qunique-comparator.patch: qunique() doesn't require a > > full sort comparator (-1 = less, 0 = equal, 1 = greater) but only a > > equal/unequal comparator (e.g. 0 = equal and 1 = unequal). The same > > optimization can be done in plenty of other places in our code base. > > Likely, in most of them the gains are insignificant. > > Makes sense. I'm a little disappointed the compiler won't do that > optimization for us.. > > Perhaps we should introduce a new qunique_eq() function with a different > callback signature: > > /* like qunique(), but the callback function returns true/false rather > than int */ > static inline size_t > qunique_eq(void *array, size_t elements, size_t width, > bool (*equal) (const void *, const void *)) > > > v1-0008-Add-ASCII-fastpath-to-generate_trgm_only.patch: Typically lots > > of text is actually ASCII. Hence, we provide a fast path for this case > > which is exercised if the MSB of the current character is unset. > > This uses pg_ascii_tolower() when for ASCII characters when built with > the IGNORECASE. I don't think that's correct, if the proper collation > would do something more complicated for than what pg_ascii_tolower() does. > > Did you measure how big is the impact from each individual patch? > Patches 1 and 2 seem pretty much ready to be committed, but I wonder if > they make any difference on their own. > > - Heikki > > > Hi! patches 0003, 0004 & 0008 applies with whitespace errors. reshke@yezzey-cbdb-bench:~/pgpure$ git am v1-0003-Use-sort_template.h.patch Applying: Use sort_template.h .git/rebase-apply/patch:66: trailing whitespace. warning: 1 line adds whitespace errors. reshke@yezzey-cbdb-bench:~/pgpure$ git am v1-0004-Avoid-dedup-and-sort-in-ginExtractEntries.patch Applying: Avoid dedup and sort in ginExtractEntries .git/rebase-apply/patch:30: trailing whitespace. { warning: 1 line adds whitespace errors. reshke@yezzey-cbdb-bench:~/pgpure$ git am v1-0008-Add-ASCII-fastpath-to-generate_trgm_only.patch Applying: Add ASCII fastpath to generate_trgm_only() .git/rebase-apply/patch:101: trailing whitespace. else warning: 1 line adds whitespace errors. I did run benchmarks of my VM using your data. v1-0001 solely improves runtime by 4-5%. v2-0002 does not show runtime improvement for me. With parallel GIN build, performance gains are 2-3 % for 2 workers and not noticeable after that. I will try to run some more benchmarks on this. -- Best regards, Kirill Reshke