/* * Copyright (c) 2026 dataStone Inc. * * SPDX-License-Identifier: MIT * See LICENSE file for full license text. */ -- pg_num2int_direct_comp--1.0.0--1.0.1.sql -- -- Completes the btree float_ops and integer_ops families, so that a join -- chaining two integer widths through a float column plans without error. -- -- 1.0.0 put the int x float "=" operators in two btree families, float_ops -- and integer_ops. A query such as -- ta.i2 = tb.f8 AND tb.f8 = tc.i4 -- puts i2, f8 and i4 in one equivalence class whose families are -- {float_ops, integer_ops}. The planner derives i2 = i4 (found in -- integer_ops) and costs a merge join on it with the class's first family, -- float_ops, which had no int2 x int4 entry: planning failed with -- "operator 532 is not a member of opfamily 1970". -- -- float_ops gains the six cross-width integer pairs, and integer_ops gains -- float4 x float8. All are core operators and comparison functions, so -- nothing the extension owns depends on them, and the cleanup trigger -- below must DROP each one (see "Hardcoded list" in the 1.0.0 script). -- After this, btree float_ops passes amvalidate(); integer_ops still lacks -- numeric x float, which the extension does not provide (see README, -- Limitations). -- -- There is no full 1.0.1 install script: CREATE EXTENSION runs the 1.0.0 -- script and then this one, so each entry and its DROP have one source. -- complain if script is sourced in psql, rather than via ALTER EXTENSION \echo Use "ALTER EXTENSION pg_num2int_direct_comp UPDATE TO '1.0.1'" to load this file. \quit -- int x int cross-width comparisons in float_ops (int x int x float chains) ALTER OPERATOR FAMILY float_ops USING btree ADD FUNCTION 1 (int2, int4) btint24cmp(int2, int4), OPERATOR 1 < (int2, int4), OPERATOR 2 <= (int2, int4), OPERATOR 3 = (int2, int4), OPERATOR 4 >= (int2, int4), OPERATOR 5 > (int2, int4), FUNCTION 1 (int4, int2) btint42cmp(int4, int2), OPERATOR 1 < (int4, int2), OPERATOR 2 <= (int4, int2), OPERATOR 3 = (int4, int2), OPERATOR 4 >= (int4, int2), OPERATOR 5 > (int4, int2), FUNCTION 1 (int2, int8) btint28cmp(int2, int8), OPERATOR 1 < (int2, int8), OPERATOR 2 <= (int2, int8), OPERATOR 3 = (int2, int8), OPERATOR 4 >= (int2, int8), OPERATOR 5 > (int2, int8), FUNCTION 1 (int8, int2) btint82cmp(int8, int2), OPERATOR 1 < (int8, int2), OPERATOR 2 <= (int8, int2), OPERATOR 3 = (int8, int2), OPERATOR 4 >= (int8, int2), OPERATOR 5 > (int8, int2), FUNCTION 1 (int4, int8) btint48cmp(int4, int8), OPERATOR 1 < (int4, int8), OPERATOR 2 <= (int4, int8), OPERATOR 3 = (int4, int8), OPERATOR 4 >= (int4, int8), OPERATOR 5 > (int4, int8), FUNCTION 1 (int8, int4) btint84cmp(int8, int4), OPERATOR 1 < (int8, int4), OPERATOR 2 <= (int8, int4), OPERATOR 3 = (int8, int4), OPERATOR 4 >= (int8, int4), OPERATOR 5 > (int8, int4); -- float4 x float8 comparisons in integer_ops (completes the family's float -- members; no plan depended on them, since float_ops already has them and -- is searched first) ALTER OPERATOR FAMILY integer_ops USING btree ADD FUNCTION 1 (float4, float8) btfloat48cmp(float4, float8), OPERATOR 1 < (float4, float8), OPERATOR 2 <= (float4, float8), OPERATOR 3 = (float4, float8), OPERATOR 4 >= (float4, float8), OPERATOR 5 > (float4, float8), FUNCTION 1 (float8, float4) btfloat84cmp(float8, float4), OPERATOR 1 < (float8, float4), OPERATOR 2 <= (float8, float4), OPERATOR 3 = (float8, float4), OPERATOR 4 >= (float8, float4), OPERATOR 5 > (float8, float4); -- The cleanup trigger's list grows by the 48 entries above. CREATE OR REPLACE FUNCTION pg_num2int_direct_comp_cleanup() RETURNS event_trigger LANGUAGE plpgsql AS $func$ DECLARE ops text[] := ARRAY[ -- Btree numeric_ops: int×int operators and functions 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int2, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int2, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int2, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int2, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int2, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int2, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int4, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int4, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int4, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int4, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int4, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int4, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int8, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int8, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int8, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int8, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int8, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int8, int8)', -- Cross-integer comparisons in numeric_ops btree 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int2, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int2, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int2, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int2, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int2, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int2, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int4, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int4, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int4, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int4, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int4, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int4, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int2, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int2, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int2, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int2, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int2, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int2, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int8, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int8, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int8, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int8, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int8, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int8, int2)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int4, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int4, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int4, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int4, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int4, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int4, int8)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 1 (int8, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 2 (int8, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 3 (int8, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 4 (int8, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP OPERATOR 5 (int8, int4)', 'ALTER OPERATOR FAMILY numeric_ops USING btree DROP FUNCTION 1 (int8, int4)', -- Btree integer_ops: numeric×numeric operators 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 1 (numeric, numeric)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 2 (numeric, numeric)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 3 (numeric, numeric)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 4 (numeric, numeric)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 5 (numeric, numeric)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP FUNCTION 1 (numeric, numeric)', -- Btree float_ops: native integer operators (added to complete the family) 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int2, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int2, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int2, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int2, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int2, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int2, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int4, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int4, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int4, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int4, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int4, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int4, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int8, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int8, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int8, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int8, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int8, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int8, int8)', -- Btree integer_ops: float×float same-type operators (NEW in v2.0) 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 1 (float4, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 2 (float4, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 3 (float4, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 4 (float4, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 5 (float4, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP FUNCTION 1 (float4, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 1 (float8, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 2 (float8, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 3 (float8, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 4 (float8, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 5 (float8, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP FUNCTION 1 (float8, float8)', -- Btree float_ops: cross-width integer operators (1.0.1) 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int2, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int2, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int2, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int2, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int2, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int2, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int4, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int4, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int4, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int4, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int4, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int4, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int2, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int2, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int2, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int2, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int2, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int2, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int8, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int8, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int8, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int8, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int8, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int8, int2)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int4, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int4, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int4, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int4, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int4, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int4, int8)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 1 (int8, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 2 (int8, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 3 (int8, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 4 (int8, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP OPERATOR 5 (int8, int4)', 'ALTER OPERATOR FAMILY float_ops USING btree DROP FUNCTION 1 (int8, int4)', -- Btree integer_ops: float4 x float8 operators (1.0.1) 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 1 (float4, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 2 (float4, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 3 (float4, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 4 (float4, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 5 (float4, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP FUNCTION 1 (float4, float8)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 1 (float8, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 2 (float8, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 3 (float8, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 4 (float8, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP OPERATOR 5 (float8, float4)', 'ALTER OPERATOR FAMILY integer_ops USING btree DROP FUNCTION 1 (float8, float4)' ]; stmt text; BEGIN -- Only process DROP EXTENSION for this specific extension. -- Word boundaries (\m and \M) prevent prefix matches against extensions -- whose name starts with ours (e.g. pg_num2int_direct_comp_foo). IF NOT (current_query() ~* '\mDROP\s+EXTENSION\M.*\mpg_num2int_direct_comp\M') THEN RETURN; END IF; -- Execute each cleanup statement, swallowing only "doesn't exist" errors -- (the entry may not have been added on a partial install). Permission, -- lock-conflict, and internal errors are allowed to propagate so they -- surface instead of silently leaving orphan catalog rows. FOREACH stmt IN ARRAY ops LOOP BEGIN EXECUTE stmt; EXCEPTION WHEN undefined_object THEN NULL; END; END LOOP; RAISE NOTICE 'pg_num2int_direct_comp: cleaned up operator family entries'; END; $func$;