Re: Hash-based MCV matching for large IN-lists

Matheus Alcantara <matheusssilv97@gmail.com>

From: "Matheus Alcantara" <matheusssilv97@gmail.com>
To: "Ilia Evdokimov" <ilya.evdokimov@tantorlabs.com>, "Tatsuya Kawata" <kawatatatsuya0913@gmail.com>, "David Geier" <geidav.pg@gmail.com>
Cc: "Chengpeng Yan" <chengpeng_yan@outlook.com>, "pgsql-hackers@lists.postgresql.org" <pgsql-hackers@lists.postgresql.org>
Date: 2026-02-24T19:32:15Z
Lists: pgsql-hackers
Hi, thanks for working on this!

On Wed Feb 18, 2026 at 9:48 AM -03, Ilia Evdokimov wrote:
> I've fixed all the comments raised above and updated the v5 patch.
>

Here are some comments regarding v5 patch:

On scalararraysel() we have:

+				ReleaseVariableStats(vardata);
+
+				if (s1 >= 0.0)
+					return s1;

I'm wondering if we also should call ReleaseVariableStats() on the early
return?

+					if (!useOr && elem_nulls[i])
+					{
+						pfree(elem_values);
+						pfree(elem_nulls);
+						pfree(elem_const);
+
+						return (Selectivity) 0.0;
+					}

------------------

On scalararray_mcv_hash_match() free_attstatsslot() is called only on
if (have_mcvs && OidIsValid(hashLeft) && OidIsValid(hashRight)),
perhaps it should be moved outside the if condition?

+	if (have_mcvs && OidIsValid(hashLeft) && OidIsValid(hashRight))
+	{
+       ...

+		MCVHashTable_destroy(hashTable);
+		free_attstatsslot(&sslot);
+	}
+
+	return selec;


------------------

typo: "all elements are const"

+ *              array is NULL if all elemnets is const.

------------------

It's worth adding on scalararray_mcv_hash_match() an early return when
num_elems == 0? I imagine that this can happens, e.g "WHERE a =
ANY(array[]::int[]);". In this case the function should still execute
completely?

--
Matheus Alcantara
EDB: https://www.enterprisedb.com



Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Short-circuit row estimation in NOT IN containing NULL consts