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 1wVZbx-0023Ym-0m for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Jun 2026 18:49:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wVZbw-00Dtri-0P for pgsql-hackers@arkaria.postgresql.org; Fri, 05 Jun 2026 18:49:40 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wVZbv-00Dtra-2I for pgsql-hackers@lists.postgresql.org; Fri, 05 Jun 2026 18:49:39 +0000 Received: from forwardcorp1d.mail.yandex.net ([2a02:6b8:c41:1300:1:45:d181:df01]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1wVZbt-00000001TMS-18Ts for pgsql-hackers@postgresql.org; Fri, 05 Jun 2026 18:49:39 +0000 Received: from mail-nwsmtp-smtp-corp-main-68.klg.yp-c.yandex.net (mail-nwsmtp-smtp-corp-main-68.klg.yp-c.yandex.net [IPv6:2a02:6b8:c42:94a9:0:640:a3fa:0]) by forwardcorp1d.mail.yandex.net (Yandex) with ESMTPS id A777580713; Fri, 05 Jun 2026 21:49:35 +0300 (MSK) Received: from smtpclient.apple (unknown [2a02:6bf:8080:160::1:37]) by mail-nwsmtp-smtp-corp-main-68.klg.yp-c.yandex.net (smtpcorp) with ESMTPSA id YnnL0P3XP8c0-43R9txra; Fri, 05 Jun 2026 21:49:35 +0300 X-Yandex-Fwd: 1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1780685375; bh=WGYizgPcTGyyme+ihSkYvHP9a6l5AuWUz3L8DThSeJQ=; h=Message-Id:To:Date:References:Cc:In-Reply-To:From:Subject; b=T/3Whw4SqL/5sHB+QRrTg4kGeWkqkl0P2YJhmOkC+ejbxzzQLBN+lw05hVRdpc2Pd kknQ7bX/Lvjdbz7ezSKjyL7CKuXey6CgTL2hGri6eCK5QWNyKqxkq+VjJ8oBe5vQe7 84D6khQLE+Noj6wJZkzGMDpcLDE0YznV0LXgriAs= Authentication-Results: mail-nwsmtp-smtp-corp-main-68.klg.yp-c.yandex.net; dkim=pass header.i=@yandex-team.ru Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.600.51.1.1\)) Subject: Re: [PATCH] btree_gist: add cross-type integer operator support for GiST From: Andrey Borodin In-Reply-To: <36b4f67d-5975-452c-a6b8-b6407f0924ee@Spark> Date: Fri, 5 Jun 2026 23:49:24 +0500 Cc: pgsql-hackers mailing list Content-Transfer-Encoding: 7bit Message-Id: <4B4B0998-7B43-4893-9603-0AF212036690@yandex-team.ru> References: <36b4f67d-5975-452c-a6b8-b6407f0924ee@Spark> To: Alexander Nestorov X-Mailer: Apple Mail (2.3864.600.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi Alexander! Thanks for working on this - this looks like useful feature and btree_gist users will appreciate it. > When a query provides a value whose type is compatible but different > from the column type, the planner cannot use the index for that column. > [...] The workaround is to write an explicit cast in every query: > WHERE camera_id = 1189::int8 > This is fragile as ORMs, application parameter binding, and even hand- > written SQL queries produce values whose types do not exactly match > the column type. This bites particularly hard with composite indexes that mix a PostGIS column with a plain identifier - a layer number, tenant id, etc.: CREATE INDEX ON t USING gist (layer_id, geom); ... WHERE geom && :bbox AND layer_id = 42; Today the integer side forces a cast to be index-usable, which seems ugly and easy to forget, and the ORM argument above only makes it worse. So I think this is worth pursuing. A few things that seem important to me (though discussion may well prove some of them minor): 1. GiST consistent() is CPU-bound (it runs for every key on every visited page), so the bar here should be strictly zero speed regression for existing same-type users. Note sk_subtype is the operator's right operand type, so for ordinary same-type scans it is the native type, not InvalidOid - meaning the same-type path now also goes through the cross-type dispatch and scans the subtype table. Please add some fast pathm and back it with a microbenchmark on a same-type workload. 2. On the "general foundation" framing: > Other btree_gist opclasses (float4/float8, date, timestamp, ...) and > even range-type GiST opfamilies in core can adopt the same pattern I'd be cautious here. The scalar int64/fabs() dispatch shape does not obviously fit range types (not scalar at all), and timestamp/date bring their own questions (infinity, etc.). It may be better to solve types as they actually come up and generalize the scaffolding as load on it grows, rather than commit to a universal framework upfront. 3. The cross-type knowledge lives in two hand-maintained places - the pg_amop entries in SQL and the C dispatch tables: > I propose [...] to dispatch cross-type queries directly inside the > existing consistent and distance functions and use the existing > subtype OID argument. They are only reconciled at query time via an elog(), not at amvalidate/CREATE time. (Version skew between the .so and the catalog isn't the worry - the new extension version ships with the new major.) The concern is plain authoring drift: adding an amop without the matching C entry, or vice versa, passes validation and only fails on a live query. It would be nicer if we could assert that every cross-type amop in the family has a corresponding dispatch entry (in amvalidate?). 4. KNN: the distance callbacks compute fabs() in float8, so for int8 values beyond 2^53 the lower-bound distance loses precision. This matches the existing same-type int8 KNN, so it's not a regression, but since you're widening the mix it's worth calling out as a known limit. The missing int_crosstype test files (referenced from REGRESS/meson but not in the patch) break make check, but I take it that's just the not-yet-included test round. Did you register your patch on the commitfest? [0] Thank you! Best regards, Andrey Borodin. [0] https://commitfest.postgresql.org/59/