From 1b4f4bc1f741d730832745a07489c08d05c052ec Mon Sep 17 00:00:00 2001
From: Alexander Nestorov <alexandernst@gmail.com>
Date: Thu, 4 Jun 2026 00:09:08 +0200
Subject: [PATCH v8 4/4] doc: Document cross-type operator support in
 btree_gist

Note that as of btree_gist 1.10 the int2, int4 and int8 operator
classes accept cross-type query operators (the B-tree comparison
operators and the <-> distance operator) against the other two
integer types without an explicit cast, and that out-of-range query
constants are compared by normal integer semantics.

Also document that nearest-neighbor index scans on these operator
classes order rows by a distance computed internally in float8, while
the <-> operator itself returns an exact integer. When an int8 value
or distance exceeds 2^53, that internal distance may be inexact, so
the index scan can return rows in a different order (and, with LIMIT,
a different set of rows) than sorting by <->. This already applied to
same-type int8 searches.
---
 doc/src/sgml/btree-gist.sgml | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/doc/src/sgml/btree-gist.sgml b/doc/src/sgml/btree-gist.sgml
index cc09ec83733..e520106b7b0 100644
--- a/doc/src/sgml/btree-gist.sgml
+++ b/doc/src/sgml/btree-gist.sgml
@@ -49,12 +49,42 @@
   <type>float8</type>, <type>timestamp with time zone</type>,
   <type>timestamp without time zone</type>,
   <type>time without time zone</type>, <type>date</type>, <type>interval</type>,
   <type>oid</type>, and <type>money</type>.
  </para>
 
+ <para>
+  As of version 1.10, the operator classes for <type>int2</type>,
+  <type>int4</type> and <type>int8</type> additionally support cross-type
+  query operators. An index on any one of these integer types can be used
+  with a B-tree-style comparison (<literal>&lt;</literal>,
+  <literal>&lt;=</literal>, <literal>=</literal>, <literal>&gt;=</literal>,
+  <literal>&gt;</literal>, <literal>&lt;&gt;</literal>) or with the
+  distance operator (<literal>&lt;-&gt;</literal>) against a value of any
+  of the other two integer types, without an explicit cast. Query
+  constants outside the indexed column's range are compared according to
+  normal integer comparison semantics, without narrowing the query value
+  to the column type first.
+ </para>
+
+ <para>
+  When a nearest-neighbor search (<literal>ORDER BY</literal> with
+  <literal>&lt;-&gt;</literal>) on the <type>int2</type>, <type>int4</type>
+  or <type>int8</type> operator classes is performed by an index scan, the
+  rows are ordered by a distance that the index computes internally in
+  <type>float8</type>, not by the result of the
+  <literal>&lt;-&gt;</literal> operator, which is an exact integer (and
+  raises an error on overflow). That internal distance may be inexact if
+  the indexed value, the query value, or the distance between them exceeds
+  2<superscript>53</superscript> in absolute value, which can only happen
+  when <type>int8</type> is involved. The index scan may then return rows
+  in a different order than sorting by <literal>&lt;-&gt;</literal> would,
+  and, with <literal>LIMIT</literal>, a different set of rows. This applies
+  equally to same-type and cross-type searches.
+ </para>
+
  <para>
   By default <filename>btree_gist</filename> builds <acronym>GiST</acronym> index with
   <function>sortsupport</function> in <firstterm>sorted</firstterm> mode. This usually results in
   much faster index built speed. It is still possible to revert to buffered built strategy
   by using the <literal>buffering</literal> parameter when creating the index.
  </para>
-- 
2.54.0 (Apple Git-157)

