postgresql/contrib
Heikki Linnakangas 3f32804918 Fix int32 overflow in ltree_compare()
The expression (len_diff * 10 * (an + 1)) used as the return value of
ltree_compare() is computed at int32 width.  With LTREE_MAX_LEVELS =
65535, the product can exceed INT32_MAX once an ltree has more than
~14,653 levels, which causes the result to wrap and invert its sign.
That corrupts btree ordering as well as the "magnitude" consumed by
ltree_penalty() for GiST page splits.

To fix, split ltree_compare() into two functions.  The new
ltree_compare_distance() function returns a float, which won't
overflow.  It's used by the ltree_penalty() caller.  All the other
callers only care about the sign of the return value, i.e. which of
the arguments is greater, so change ltree_compare() to not multiply
the result with (10 * (an + 1)), which avoids the overflow for those
callers.

Existing btree or GiST indexes on ltree columns containing values with
more than ~14,653 levels may be corrupt and should be REINDEXed.

Add a regression test based on the reporter's PoC.

Author: Ayush Tiwari <ayushtiwari.slg01@gmail.com>
Reported-by: 王跃林 <violin0613@tju.edu.cn>
Discussion: https://www.postgresql.org/message-id/AI6AnABgKW93Qbx1jVzi84r9.8.1781322625756.Hmail.3020001251%40tju.edu.cn
Backpatch-through: 14
2026-06-16 09:29:47 +03:00
..
amcheck amcheck: Use correct varlena size accessor in bt_normalize_tuple() 2026-06-14 04:04:48 +03:00
auth_delay
auto_explain Pre-beta mechanical code beautification, step 2: run pgperltidy. 2026-05-13 10:37:42 -04:00
basebackup_to_shell
basic_archive Fix dereference in a couple of GUC check hooks 2026-03-24 16:45:39 +01:00
bloom Discount the metapage when estimating number of index pages visited. 2026-03-20 14:50:53 -04:00
bool_plperl
btree_gin
btree_gist Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
citext
cube Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
dblink postgres_fdw, dblink: Validate use_scram_passthrough values 2026-05-28 20:58:08 +09:00
dict_int
dict_xsyn
earthdistance
file_fdw
fuzzystrmatch Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
hstore
hstore_plperl Use palloc_array() in a few more places to avoid overflow 2026-05-11 21:27:55 +03:00
hstore_plpython Use palloc_array() in a few more places to avoid overflow 2026-05-11 21:27:55 +03:00
intagg
intarray Fix missed ReleaseVariableStats() in intarray's _int_matchsel(). 2026-05-25 18:15:49 -04:00
isn Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
jsonb_plperl Harmonize function parameter names for Postgres 19. 2026-04-22 12:47:19 -04:00
jsonb_plpython Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
lo
ltree Fix int32 overflow in ltree_compare() 2026-06-16 09:29:47 +03:00
ltree_plpython
oid2name
pageinspect Adjust some incorrect *GetDatum() macros 2026-04-30 13:10:19 +09:00
passwordcheck
pg_buffercache pg_buffercache: restore rowtype verification in pg_buffercache_pages() 2026-06-10 12:32:59 +09:00
pg_freespacemap
pg_logicalinspect
pg_overexplain Fix a set of typos and grammar issues across the tree 2026-04-21 14:46:22 +09:00
pg_plan_advice pg_plan_advice: DO_NOT_SCAN is a simple tag, not a generic one. 2026-05-29 13:51:09 -04:00
pg_prewarm Pre-beta mechanical code beautification, step 2: run pgperltidy. 2026-05-13 10:37:42 -04:00
pg_stash_advice pg_stash_advice: Reject overlong stash names in dump file. 2026-05-29 13:25:20 -04:00
pg_stat_statements pg_stat_statements: Set PlannedStmt to NULL after nested utility execution 2026-05-13 15:39:44 +09:00
pg_surgery pg_surgery: Fix off-by-one bug with heap offset 2026-06-06 08:16:36 +09:00
pg_trgm Avoid casting void * function arguments 2026-04-23 08:08:57 +02:00
pg_visibility Revert "Allow logical replication snapshots to be database-specific" 2026-05-23 21:33:19 -07:00
pg_walinspect Report detailed errors from XLogFindNextRecord() failures. 2026-03-24 22:33:09 +09:00
pgcrypto Pre-beta mechanical code beautification, step 1: run pgindent. 2026-05-13 10:34:17 -04:00
pgrowlocks Thread flags through begin-scan APIs 2026-03-30 12:27:24 -04:00
pgstattuple
postgres_fdw Fix oversight in commit aa1f93a33. 2026-06-14 16:00:00 +09:00
seg seg: Fix seg_out() to preserve the upper boundary's certainty indicator 2026-06-11 12:33:48 +03:00
sepgsql Harmonize function parameter names for Postgres 19. 2026-04-22 12:47:19 -04:00
spi refint: Remove plan cache. 2026-06-05 12:08:05 -05:00
sslinfo Fix compilation with OpenSSL 4 2026-05-29 21:52:35 +02:00
start-scripts
tablefunc Avoid including vacuum.h in tableam.h and heapam.h. 2026-03-31 12:43:52 -05:00
tcn
test_decoding Disallow direct use of the pgrepack logical decoding plugin 2026-06-09 20:12:55 +02:00
tsm_system_rows
tsm_system_time
unaccent
uuid-ossp
vacuumlo
xml2 xml2: Fix crash with namespace nodes in xpath_nodeset() 2026-06-11 14:29:18 +09:00
contrib-global.mk
Makefile Add pg_stash_advice contrib module. 2026-04-06 07:41:28 -04:00
meson.build Add pg_stash_advice contrib module. 2026-04-06 07:41:28 -04:00
README

The PostgreSQL contrib tree
---------------------------

This subtree contains porting tools, analysis utilities, and plug-in
features that are not part of the core PostgreSQL system, mainly
because they address a limited audience or are too experimental to be
part of the main source tree.  This does not preclude their
usefulness.

User documentation for each module appears in the main SGML
documentation.

When building from the source distribution, these modules are not
built automatically, unless you build the "world" target.  You can
also build and install them all by running "make all" and "make
install" in this directory; or to build and install just one selected
module, do the same in that module's subdirectory.

Some directories supply new user-defined functions, operators, or
types.  To make use of one of these modules, after you have installed
the code you need to register the new SQL objects in the database
system by executing a CREATE EXTENSION command.  In a fresh database,
you can simply do

    CREATE EXTENSION module_name;

See the PostgreSQL documentation for more information about this
procedure.