-- Extension Lifecycle Tests -- Verifies the extension can be installed correctly and that DROP/CREATE -- cycles work properly. This test requires superuser privileges. -- -- Tests: -- 1. OID stability: hardcoded PostgreSQL OIDs match the system catalog -- 2. Extension cleanup: event trigger removes operator family entries on DROP -- 3. Syscache invalidation: OID cache is refreshed after DROP/CREATE -- 4. Catalog snapshot: in a database made from template0, DROP EXTENSION -- restores pg_amop/pg_amproc exactly, a reinstall matches the first -- install, and every upgrade path matches a fresh install -- ============================================================================ -- OID Stability Tests -- ============================================================================ -- Verifies that hardcoded PostgreSQL built-in OIDs in pg_num2int_direct_comp.h -- match the actual system catalog values. These OIDs should be stable across -- all PostgreSQL versions, but this test provides early warning if they change. -- Cast function OIDs (NUM2INT_CAST_NUMERIC_INT* in header) SELECT CASE WHEN oid = 1783 THEN 'PASS' ELSE 'FAIL: expected 1783, got ' || oid END AS int2_numeric_cast FROM pg_proc WHERE proname = 'int2' AND pronargs = 1 AND proargtypes[0] = 'numeric'::regtype; int2_numeric_cast ------------------- PASS (1 row) SELECT CASE WHEN oid = 1744 THEN 'PASS' ELSE 'FAIL: expected 1744, got ' || oid END AS int4_numeric_cast FROM pg_proc WHERE proname = 'int4' AND pronargs = 1 AND proargtypes[0] = 'numeric'::regtype; int4_numeric_cast ------------------- PASS (1 row) SELECT CASE WHEN oid = 1779 THEN 'PASS' ELSE 'FAIL: expected 1779, got ' || oid END AS int8_numeric_cast FROM pg_proc WHERE proname = 'int8' AND pronargs = 1 AND proargtypes[0] = 'numeric'::regtype; int8_numeric_cast ------------------- PASS (1 row) -- Integer comparison operator OIDs (NUM2INT_INT*_OID in header) -- Numeric function OIDs (NUM2INT_NUMERIC_* in header) WITH oid_checks AS ( SELECT 1783 AS expected, oid AS actual, 'int2(numeric)' AS name FROM pg_proc WHERE proname = 'int2' AND pronargs = 1 AND proargtypes[0] = 'numeric'::regtype UNION ALL SELECT 1744, oid, 'int4(numeric)' FROM pg_proc WHERE proname = 'int4' AND pronargs = 1 AND proargtypes[0] = 'numeric'::regtype UNION ALL SELECT 1779, oid, 'int8(numeric)' FROM pg_proc WHERE proname = 'int8' AND pronargs = 1 AND proargtypes[0] = 'numeric'::regtype UNION ALL SELECT 1710, oid, 'trunc(numeric)' FROM pg_proc WHERE proname = 'trunc' AND pronargs = 1 AND proargtypes[0] = 'numeric'::regtype UNION ALL SELECT 1712, oid, 'floor(numeric)' FROM pg_proc WHERE proname = 'floor' AND pronargs = 1 AND proargtypes[0] = 'numeric'::regtype UNION ALL SELECT 1718, oid, 'numeric_eq(numeric,numeric)' FROM pg_proc WHERE proname = 'numeric_eq' AND pronargs = 2 AND proargtypes[0] = 'numeric'::regtype AND proargtypes[1] = 'numeric'::regtype UNION ALL SELECT 94, oid, 'int2 = int2' FROM pg_operator WHERE oprname = '=' AND oprleft = 'int2'::regtype AND oprright = 'int2'::regtype UNION ALL SELECT 95, oid, 'int2 < int2' FROM pg_operator WHERE oprname = '<' AND oprleft = 'int2'::regtype AND oprright = 'int2'::regtype UNION ALL SELECT 520, oid, 'int2 > int2' FROM pg_operator WHERE oprname = '>' AND oprleft = 'int2'::regtype AND oprright = 'int2'::regtype UNION ALL SELECT 522, oid, 'int2 <= int2' FROM pg_operator WHERE oprname = '<=' AND oprleft = 'int2'::regtype AND oprright = 'int2'::regtype UNION ALL SELECT 524, oid, 'int2 >= int2' FROM pg_operator WHERE oprname = '>=' AND oprleft = 'int2'::regtype AND oprright = 'int2'::regtype UNION ALL SELECT 519, oid, 'int2 <> int2' FROM pg_operator WHERE oprname = '<>' AND oprleft = 'int2'::regtype AND oprright = 'int2'::regtype UNION ALL SELECT 96, oid, 'int4 = int4' FROM pg_operator WHERE oprname = '=' AND oprleft = 'int4'::regtype AND oprright = 'int4'::regtype UNION ALL SELECT 97, oid, 'int4 < int4' FROM pg_operator WHERE oprname = '<' AND oprleft = 'int4'::regtype AND oprright = 'int4'::regtype UNION ALL SELECT 521, oid, 'int4 > int4' FROM pg_operator WHERE oprname = '>' AND oprleft = 'int4'::regtype AND oprright = 'int4'::regtype UNION ALL SELECT 523, oid, 'int4 <= int4' FROM pg_operator WHERE oprname = '<=' AND oprleft = 'int4'::regtype AND oprright = 'int4'::regtype UNION ALL SELECT 525, oid, 'int4 >= int4' FROM pg_operator WHERE oprname = '>=' AND oprleft = 'int4'::regtype AND oprright = 'int4'::regtype UNION ALL SELECT 518, oid, 'int4 <> int4' FROM pg_operator WHERE oprname = '<>' AND oprleft = 'int4'::regtype AND oprright = 'int4'::regtype UNION ALL SELECT 410, oid, 'int8 = int8' FROM pg_operator WHERE oprname = '=' AND oprleft = 'int8'::regtype AND oprright = 'int8'::regtype UNION ALL SELECT 412, oid, 'int8 < int8' FROM pg_operator WHERE oprname = '<' AND oprleft = 'int8'::regtype AND oprright = 'int8'::regtype UNION ALL SELECT 413, oid, 'int8 > int8' FROM pg_operator WHERE oprname = '>' AND oprleft = 'int8'::regtype AND oprright = 'int8'::regtype UNION ALL SELECT 414, oid, 'int8 <= int8' FROM pg_operator WHERE oprname = '<=' AND oprleft = 'int8'::regtype AND oprright = 'int8'::regtype UNION ALL SELECT 415, oid, 'int8 >= int8' FROM pg_operator WHERE oprname = '>=' AND oprleft = 'int8'::regtype AND oprright = 'int8'::regtype UNION ALL SELECT 411, oid, 'int8 <> int8' FROM pg_operator WHERE oprname = '<>' AND oprleft = 'int8'::regtype AND oprright = 'int8'::regtype ) SELECT COUNT(*) FILTER (WHERE expected != actual) AS oid_mismatches, CASE WHEN COUNT(*) FILTER (WHERE expected != actual) = 0 THEN 'All hardcoded OIDs match system catalog' ELSE 'WARNING: OID mismatches detected!' END AS status FROM oid_checks; oid_mismatches | status ----------------+----------------------------------------- 0 | All hardcoded OIDs match system catalog (1 row) -- ============================================================================ -- Internal Numeric Format Validation -- ============================================================================ -- These tests verify that our internal Numeric format macros (NUM2INT_NUMERIC_*) -- correctly interpret PostgreSQL's Numeric storage format. If PostgreSQL ever -- changes the internal Numeric format, these tests should catch it. -- -- Tests cover: NaN/Inf detection, sign extraction, weight calculation, -- integral vs fractional detection, and digit array interpretation. -- Special Values: NaN and Infinity (tests NUM2INT_NUMERIC_IS_NAN/PINF/NINF) SELECT 'NaN = 0'::text AS test, 'NaN'::numeric = 0::int4 AS result; test | result ---------+-------- NaN = 0 | f (1 row) SELECT 'NaN <> 0'::text AS test, 'NaN'::numeric <> 0::int4 AS result; test | result ----------+-------- NaN <> 0 | t (1 row) SELECT '+Inf > max_int8'::text AS test, 'Infinity'::numeric > 9223372036854775807::int8 AS result; test | result -----------------+-------- +Inf > max_int8 | t (1 row) SELECT '-Inf < min_int8'::text AS test, '-Infinity'::numeric < (-9223372036854775808)::int8 AS result; test | result -----------------+-------- -Inf < min_int8 | t (1 row) -- Zero handling (ndigits=0 in Numeric format) SELECT '0 = 0'::text AS test, 0::numeric = 0::int4 AS result; test | result -------+-------- 0 = 0 | t (1 row) SELECT '0.0 = 0'::text AS test, 0.0::numeric = 0::int4 AS result; test | result ---------+-------- 0.0 = 0 | t (1 row) -- Sign detection (NUM2INT_NUMERIC_SIGN) SELECT '100 > 0'::text AS test, 100::numeric > 0::int4 AS result; test | result ---------+-------- 100 > 0 | t (1 row) SELECT '-100 < 0'::text AS test, (-100)::numeric < 0::int4 AS result; test | result ----------+-------- -100 < 0 | t (1 row) -- Weight and digit array (NUM2INT_NUMERIC_WEIGHT, NUM2INT_NUMERIC_NDIGITS) SELECT '9999 = 9999'::text AS test, 9999::numeric = 9999::int4 AS result; -- single base-10000 digit test | result -------------+-------- 9999 = 9999 | t (1 row) SELECT '10000 = 10000'::text AS test, 10000::numeric = 10000::int4 AS result; -- weight=1 test | result ---------------+-------- 10000 = 10000 | t (1 row) SELECT 'max_int8 exact'::text AS test, 9223372036854775807::numeric = 9223372036854775807::int8 AS result; test | result ----------------+-------- max_int8 exact | t (1 row) SELECT 'min_int8 exact'::text AS test, (-9223372036854775808)::numeric = (-9223372036854775808)::int8 AS result; test | result ----------------+-------- min_int8 exact | t (1 row) -- Fractional detection (ndigits > weight + 1) SELECT '0.5 = 0'::text AS test, 0.5::numeric = 0::int4 AS result; -- false test | result ---------+-------- 0.5 = 0 | f (1 row) SELECT '100.5 > 100'::text AS test, 100.5::numeric > 100::int4 AS result; -- true test | result -------------+-------- 100.5 > 100 | t (1 row) SELECT '-100.5 > -101'::text AS test, (-100.5)::numeric > (-101)::int4 AS result; -- true (floor test) test | result ---------------+-------- -100.5 > -101 | t (1 row) SELECT '-0.5 > -1'::text AS test, (-0.5)::numeric > (-1)::int4 AS result; -- true test | result -----------+-------- -0.5 > -1 | t (1 row) -- Huge values beyond int64 range (sign-based comparison) SELECT 'huge_pos > max_int8'::text AS test, 99999999999999999999::numeric > 9223372036854775807::int8 AS result; test | result ---------------------+-------- huge_pos > max_int8 | t (1 row) SELECT 'huge_neg < min_int8'::text AS test, (-99999999999999999999)::numeric < (-9223372036854775808)::int8 AS result; test | result ---------------------+-------- huge_neg < min_int8 | t (1 row) -- Trailing zeros (storage normalization) SELECT '100.00 = 100'::text AS test, 100.00::numeric = 100::int4 AS result; test | result --------------+-------- 100.00 = 100 | t (1 row) -- ============================================================================ -- Extension Cleanup Tests -- ============================================================================ -- The extension cleanup event trigger fires on DROP EXTENSION and removes -- operators/functions added to built-in operator families. Without this -- cleanup, CREATE EXTENSION would fail with "operator already exists". -- Start with extension loaded (from setup) SELECT extname FROM pg_extension WHERE extname = 'pg_num2int_direct_comp'; extname ------------------------ pg_num2int_direct_comp (1 row) -- Verify event trigger exists and is owned by extension SELECT evtname, evtenabled FROM pg_event_trigger WHERE evtname = 'pg_num2int_direct_comp_drop_trigger'; evtname | evtenabled -------------------------------------+------------ pg_num2int_direct_comp_drop_trigger | O (1 row) -- Verify the trigger is part of the extension SELECT pg_describe_object(d.classid, d.objid, d.objsubid) AS object_desc FROM pg_depend d JOIN pg_extension e ON e.oid = d.refobjid WHERE d.refclassid = 'pg_extension'::regclass AND e.extname = 'pg_num2int_direct_comp' AND d.classid = 'pg_event_trigger'::regclass; object_desc --------------------------------------------------- event trigger pg_num2int_direct_comp_drop_trigger (1 row) -- Test basic functionality before DROP SELECT 10::numeric = 10::int4 AS before_drop_test; before_drop_test ------------------ t (1 row) -- DROP and recreate extension DROP EXTENSION pg_num2int_direct_comp; NOTICE: pg_num2int_direct_comp: cleaned up operator family entries -- Post-DROP cleanup of the built-in operator families is checked against a -- pristine catalog snapshot in "Catalog Snapshot Tests" at the end of this -- file, for every access method and family. -- Recreate extension CREATE EXTENSION pg_num2int_direct_comp; -- Test functionality after reinstall SELECT 10::numeric = 10::int4 AS after_reinstall_test; after_reinstall_test ---------------------- t (1 row) -- Test int×float operators after reinstall (new test for btree family integration) SELECT 10::int4 = 10.0::float4 AS int4_float4_reinstall_test; int4_float4_reinstall_test ---------------------------- t (1 row) SELECT 10::int4 = 10.0::float8 AS int4_float8_reinstall_test; int4_float8_reinstall_test ---------------------------- t (1 row) -- Verify btree family registrations were recreated correctly SELECT COUNT(*) AS recreated_float_ops FROM pg_amop ao JOIN pg_opfamily of ON ao.amopfamily = of.oid JOIN pg_am am ON of.opfmethod = am.oid WHERE of.opfname = 'float_ops' AND am.amname = 'btree' AND ((ao.amoplefttype IN ('int2'::regtype, 'int4'::regtype, 'int8'::regtype) AND ao.amoprighttype IN ('float4'::regtype, 'float8'::regtype)) OR (ao.amoplefttype IN ('float4'::regtype, 'float8'::regtype) AND ao.amoprighttype IN ('int2'::regtype, 'int4'::regtype, 'int8'::regtype))); recreated_float_ops --------------------- 60 (1 row) SELECT COUNT(*) AS recreated_integer_ops FROM pg_amop ao JOIN pg_opfamily of ON ao.amopfamily = of.oid JOIN pg_am am ON of.opfmethod = am.oid WHERE of.opfname = 'integer_ops' AND am.amname = 'btree' AND ((ao.amoplefttype IN ('int2'::regtype, 'int4'::regtype, 'int8'::regtype) AND ao.amoprighttype IN ('float4'::regtype, 'float8'::regtype)) OR (ao.amoplefttype IN ('float4'::regtype, 'float8'::regtype) AND ao.amoprighttype IN ('int2'::regtype, 'int4'::regtype, 'int8'::regtype))); recreated_integer_ops ----------------------- 60 (1 row) -- Verify event trigger was recreated SELECT evtname FROM pg_event_trigger WHERE evtname = 'pg_num2int_direct_comp_drop_trigger'; evtname ------------------------------------- pg_num2int_direct_comp_drop_trigger (1 row) -- Second DROP/CREATE cycle DROP EXTENSION pg_num2int_direct_comp; NOTICE: pg_num2int_direct_comp: cleaned up operator family entries CREATE EXTENSION pg_num2int_direct_comp; SELECT 10::numeric = 10::int4 AS second_cycle_test; second_cycle_test ------------------- t (1 row) -- ============================================================================ -- Test syscache callback: OID cache must be invalidated after DROP/CREATE -- Without proper cache invalidation, support function won't recognize new -- operator OIDs and index optimization will fail (shows '42'::numeric instead of 42) -- ============================================================================ -- First, use the cross-type operator to populate the OID cache CREATE TEMPORARY TABLE lifecycle_test (id INT4 PRIMARY KEY); INSERT INTO lifecycle_test VALUES (42); ANALYZE lifecycle_test; -- Use a prepared statement to trigger the support function PREPARE lifecycle_find(numeric) AS SELECT * FROM lifecycle_test WHERE id = $1; EXPLAIN (COSTS OFF) EXECUTE lifecycle_find(42); QUERY PLAN ---------------------------- Seq Scan on lifecycle_test Filter: (id = 42) (2 rows) -- Should show: Filter: (id = 42) -- NOT '42'::numeric DEALLOCATE lifecycle_find; -- DROP and CREATE to get new operator OIDs DROP EXTENSION pg_num2int_direct_comp; NOTICE: pg_num2int_direct_comp: cleaned up operator family entries CREATE EXTENSION pg_num2int_direct_comp; -- Test that support function works with NEW operator OIDs -- (this fails if syscache callback doesn't invalidate the cache) PREPARE lifecycle_find2(numeric) AS SELECT * FROM lifecycle_test WHERE id = $1; EXPLAIN (COSTS OFF) EXECUTE lifecycle_find2(42); QUERY PLAN ---------------------------- Seq Scan on lifecycle_test Filter: (id = 42) (2 rows) -- Should show: Filter: (id = 42) -- NOT '42'::numeric DEALLOCATE lifecycle_find2; DROP TABLE lifecycle_test; -- ============================================================================ -- Hash Function Compatibility Tests -- ============================================================================ -- Verifies that our optimized hash functions (which avoid palloc) produce -- identical results to PostgreSQL's hash_numeric. This ensures hash joins -- work correctly when comparing integers to numerics. -- Test hash_int8_as_numeric matches hash_numeric for various values SELECT bool_and(hash_int8_as_numeric(val) = hash_numeric(val::numeric)) AS hash_match FROM (VALUES (0::int8), (1), (-1), (12345), (-12345), (10000), (100000000), (9999999900000001), (9223372036854775807), (-9223372036854775808) ) AS t(val); hash_match ------------ t (1 row) -- Test hash_int8_as_numeric_extended matches hash_numeric_extended SELECT bool_and(hash_int8_as_numeric_extended(val, 12345::int8) = hash_numeric_extended(val::numeric, 12345::int8)) AS extended_hash_match FROM (VALUES (0::int8), (1), (-1), (12345), (-12345), (10000), (100000000), (9999999900000001), (9223372036854775807), (-9223372036854775808) ) AS t(val); extended_hash_match --------------------- t (1 row) -- Final verification SELECT 'Extension lifecycle test passed' AS result; result --------------------------------- Extension lifecycle test passed (1 row) -- ============================================================================ -- GUC Configuration Tests -- ============================================================================ -- Test that the GUC parameter works correctly for enabling/disabling optimizations -- The GUC is defined when the library loads, and SHOW and the reserved-prefix -- check below depend on that. Load it explicitly so this block does not rely -- on earlier statements in the file having used an operator (README: run -- LOAD first if you need the parameter visible earlier). LOAD 'pg_num2int_direct_comp'; -- Check GUC exists and has correct default value (on) SHOW pg_num2int_direct_comp.enable_support_functions; pg_num2int_direct_comp.enable_support_functions ------------------------------------------------- on (1 row) -- Test setting the GUC to different values SET pg_num2int_direct_comp.enable_support_functions = off; SHOW pg_num2int_direct_comp.enable_support_functions; pg_num2int_direct_comp.enable_support_functions ------------------------------------------------- off (1 row) SET pg_num2int_direct_comp.enable_support_functions = on; SHOW pg_num2int_direct_comp.enable_support_functions; pg_num2int_direct_comp.enable_support_functions ------------------------------------------------- on (1 row) -- The prefix is reserved: a misspelled name must be rejected, not silently -- accepted as a placeholder that does nothing SET pg_num2int_direct_comp.enable_support_function = off; ERROR: invalid configuration parameter name "pg_num2int_direct_comp.enable_support_function" DETAIL: "pg_num2int_direct_comp" is a reserved prefix. -- Create test table for optimization verification CREATE TEMPORARY TABLE guc_test (val int8); INSERT INTO guc_test VALUES (9007199254740992); -- With optimizations disabled the plan keeps the cross-type comparison -- (float8 constant compared to the bigint column by the extension operator) SET pg_num2int_direct_comp.enable_support_functions = off; EXPLAIN (VERBOSE, COSTS off) SELECT * FROM guc_test WHERE 9007199254740993.0::float8 = val; QUERY PLAN ---------------------------------------------------------------------- Seq Scan on pg_temp.guc_test Output: val Filter: ('9.007199254740992e+15'::double precision = guc_test.val) (3 rows) -- With optimizations enabled the constant is rewritten to bigint and the -- native int8 = int8 operator is used SET pg_num2int_direct_comp.enable_support_functions = on; EXPLAIN (VERBOSE, COSTS off) SELECT * FROM guc_test WHERE 9007199254740993.0::float8 = val; QUERY PLAN ------------------------------------------------------- Seq Scan on pg_temp.guc_test Output: val Filter: (guc_test.val = '9007199254740992'::bigint) (3 rows) -- Query results must be the same in both modes -- (9007199254740993.0::float8 rounds to 2^53, which equals the stored value) SET pg_num2int_direct_comp.enable_support_functions = off; SELECT count(*) AS disabled_count FROM guc_test WHERE 9007199254740993.0::float8 = val; disabled_count ---------------- 1 (1 row) SET pg_num2int_direct_comp.enable_support_functions = on; SELECT count(*) AS enabled_count FROM guc_test WHERE 9007199254740993.0::float8 = val; enabled_count --------------- 1 (1 row) -- Cleanup RESET pg_num2int_direct_comp.enable_support_functions; DROP TABLE guc_test; -- ============================================================================ -- Catalog Snapshot Tests -- ============================================================================ -- The extension adds entries to built-in operator families (btree and hash -- numeric_ops, integer_ops, float_ops). pg_depend does not track them, so the -- event trigger's hand-written DROP list is the only thing that removes them. -- Instead of naming the entries it expects to be gone, this section diffs the -- whole pg_amop/pg_amproc catalog against a snapshot taken before the -- extension was ever installed, so a new ADD without a matching DROP, or a -- DROP that removes a core entry, shows up without editing this test. -- -- contrib_regression already has the extension (earlier tests create it), so -- the baseline comes from a fresh database made from template0. Everything -- below runs in that database, in one session. SELECT current_database() AS regress_db \gset -- Quiet, so a database left behind by an aborted run doesn't change the output SET client_min_messages = warning; DROP DATABASE IF EXISTS n2i_lifecycle; RESET client_min_messages; CREATE DATABASE n2i_lifecycle TEMPLATE template0; \c n2i_lifecycle -- One row per catalog fact, keyed by names rather than OIDs so that a -- reinstall (new OIDs) compares equal: -- amop / amproc: every operator family entry, all access methods -- member: every object the extension owns -- oper / func: planner-relevant properties of the extension's operators -- and functions (an upgrade that forgets MERGES, a support -- function or a volatility marking shows up here) CREATE TEMP VIEW n2i_catalog AS SELECT 'amop'::text AS cat, am.amname::text AS am, fn.nspname || '.' || f.opfname AS family, a.amoplefttype::regtype::text AS lt, a.amoprighttype::regtype::text AS rt, a.amopstrategy::int AS num, a.amoppurpose::text AS purpose, a.amopopr::regoperator::text AS member, coalesce(sf.opfname::text, '') AS extra FROM pg_amop a JOIN pg_opfamily f ON f.oid = a.amopfamily JOIN pg_namespace fn ON fn.oid = f.opfnamespace JOIN pg_am am ON am.oid = f.opfmethod LEFT JOIN pg_opfamily sf ON sf.oid = a.amopsortfamily UNION ALL SELECT 'amproc', am.amname, fn.nspname || '.' || f.opfname, p.amproclefttype::regtype::text, p.amprocrighttype::regtype::text, p.amprocnum::int, '', p.amproc::regprocedure::text, '' FROM pg_amproc p JOIN pg_opfamily f ON f.oid = p.amprocfamily JOIN pg_namespace fn ON fn.oid = f.opfnamespace JOIN pg_am am ON am.oid = f.opfmethod UNION ALL SELECT 'member', '', '', '', '', 0, '', pg_describe_object(d.classid, d.objid, d.objsubid), '' FROM pg_depend d JOIN pg_extension e ON e.oid = d.refobjid WHERE d.refclassid = 'pg_extension'::regclass AND d.deptype = 'e' AND e.extname = 'pg_num2int_direct_comp' UNION ALL SELECT 'oper', '', '', '', '', 0, '', o.oid::regoperator::text, format('code=%s merges=%s hashes=%s com=%s neg=%s rest=%s join=%s', o.oprcode, o.oprcanmerge, o.oprcanhash, o.oprcom::regoperator, o.oprnegate::regoperator, o.oprrest, o.oprjoin) FROM pg_operator o JOIN pg_depend d ON d.classid = 'pg_operator'::regclass AND d.objid = o.oid JOIN pg_extension e ON e.oid = d.refobjid WHERE d.refclassid = 'pg_extension'::regclass AND d.deptype = 'e' AND e.extname = 'pg_num2int_direct_comp' UNION ALL SELECT 'func', '', '', '', '', 0, '', pr.oid::regprocedure::text, format('vol=%s strict=%s par=%s leak=%s support=%s ret=%s src=%s bin=%s', pr.provolatile, pr.proisstrict, pr.proparallel, pr.proleakproof, pr.prosupport, pr.prorettype::regtype, md5(pr.prosrc), pr.probin) FROM pg_proc pr JOIN pg_depend d ON d.classid = 'pg_proc'::regclass AND d.objid = pr.oid JOIN pg_extension e ON e.oid = d.refobjid WHERE d.refclassid = 'pg_extension'::regclass AND d.deptype = 'e' AND e.extname = 'pg_num2int_direct_comp'; -- Rows in a but not b, and in b but not a, labelled CREATE FUNCTION pg_temp.n2i_diff(a text, b text) RETURNS TABLE (side text, cat text, am text, family text, lt text, rt text, num int, purpose text, member text, extra text) LANGUAGE plpgsql AS $$ BEGIN RETURN QUERY EXECUTE format( 'SELECT %1$L, * FROM (SELECT * FROM %1$s EXCEPT ALL SELECT * FROM %2$s) x UNION ALL SELECT %2$L, * FROM (SELECT * FROM %2$s EXCEPT ALL SELECT * FROM %1$s) y ORDER BY 1, 2, 3, 4, 5, 6, 7, 8', a, b); END $$; -- Baseline: the stock catalog CREATE TEMP TABLE snap_base AS SELECT * FROM n2i_catalog; -- Install. The footprint is what the extension adds; it depends only on the -- install script, not on the PostgreSQL version, and a check that found -- nothing to compare would show up here as missing rows. CREATE EXTENSION pg_num2int_direct_comp; CREATE TEMP TABLE snap_install AS SELECT * FROM n2i_catalog; SELECT cat, am, family, count(*) AS added FROM (SELECT * FROM snap_install EXCEPT ALL SELECT * FROM snap_base) x GROUP BY 1, 2, 3 ORDER BY 1, 2, 3; cat | am | family | added --------+-------+------------------------+------- amop | btree | pg_catalog.float_ops | 105 amop | btree | pg_catalog.integer_ops | 115 amop | btree | pg_catalog.numeric_ops | 75 amop | hash | pg_catalog.float_ops | 12 amop | hash | pg_catalog.numeric_ops | 6 amproc | btree | pg_catalog.float_ops | 21 amproc | btree | pg_catalog.integer_ops | 23 amproc | btree | pg_catalog.numeric_ops | 15 amproc | hash | pg_catalog.float_ops | 6 amproc | hash | pg_catalog.numeric_ops | 6 func | | | 146 member | | | 255 oper | | | 108 (13 rows) -- CREATE EXTENSION must not remove anything (expected: none) SELECT * FROM (SELECT * FROM snap_base EXCEPT ALL SELECT * FROM snap_install) x; cat | am | family | lt | rt | num | purpose | member | extra -----+----+--------+----+----+-----+---------+--------+------- (0 rows) -- DROP must restore the stock catalog exactly. A row on the snap_base side -- means the cleanup removed a core entry; a row on the n2i_catalog side is a -- leftover (an ADD without a matching DROP). Expected: none. DROP EXTENSION pg_num2int_direct_comp; NOTICE: pg_num2int_direct_comp: cleaned up operator family entries SELECT * FROM pg_temp.n2i_diff('n2i_catalog', 'snap_base'); side | cat | am | family | lt | rt | num | purpose | member | extra ------+-----+----+--------+----+----+-----+---------+--------+------- (0 rows) -- Reinstall must match the first install, and DROP must clean up again. -- Expected: none, twice. CREATE EXTENSION pg_num2int_direct_comp; SELECT * FROM pg_temp.n2i_diff('n2i_catalog', 'snap_install'); side | cat | am | family | lt | rt | num | purpose | member | extra ------+-----+----+--------+----+----+-----+---------+--------+------- (0 rows) DROP EXTENSION pg_num2int_direct_comp; NOTICE: pg_num2int_direct_comp: cleaned up operator family entries SELECT * FROM pg_temp.n2i_diff('n2i_catalog', 'snap_base'); side | cat | am | family | lt | rt | num | purpose | member | extra ------+-----+----+--------+----+----+-----+---------+--------+------- (0 rows) -- Upgrade paths: for every installable version with an update path to the -- default version, install it, ALTER EXTENSION UPDATE, and compare with the -- fresh install; then DROP and compare with the baseline. The statements -- run one at a time through \gexec, so the cleanup trigger sees a plain -- top-level DROP EXTENSION. The list of source versions is printed, so a -- new upgrade script changes this output. 1.0.1 has no full install script, -- so a fresh install also runs 1.0.0 plus the update, and the comparison -- with snap_install cannot fail until a version ships one; the DROP after -- the update still checks the update script's cleanup list. CREATE TEMP TABLE upgrade_paths AS SELECT p.source FROM pg_extension_update_paths('pg_num2int_direct_comp') p JOIN pg_available_extensions a ON a.name = 'pg_num2int_direct_comp' WHERE p.target = a.default_version AND p.path IS NOT NULL; SELECT source AS upgrade_from FROM upgrade_paths ORDER BY 1; upgrade_from -------------- 1.0.0 (1 row) CREATE TEMP TABLE upgrade_diffs (source text, side text, cat text, am text, family text, lt text, rt text, num int, purpose text, member text, extra text); SELECT stmt FROM upgrade_paths, LATERAL (VALUES (1, format('CREATE EXTENSION pg_num2int_direct_comp VERSION %L', source)), (2, 'ALTER EXTENSION pg_num2int_direct_comp UPDATE'), (3, format($q$INSERT INTO upgrade_diffs SELECT %L, * FROM pg_temp.n2i_diff('n2i_catalog', 'snap_install')$q$, source)), (4, 'DROP EXTENSION pg_num2int_direct_comp'), (5, format($q$INSERT INTO upgrade_diffs SELECT %L, * FROM pg_temp.n2i_diff('n2i_catalog', 'snap_base')$q$, source)) ) s(n, stmt) ORDER BY source, n \gexec CREATE EXTENSION pg_num2int_direct_comp VERSION '1.0.0' ALTER EXTENSION pg_num2int_direct_comp UPDATE INSERT INTO upgrade_diffs SELECT '1.0.0', * FROM pg_temp.n2i_diff('n2i_catalog', 'snap_install') DROP EXTENSION pg_num2int_direct_comp NOTICE: pg_num2int_direct_comp: cleaned up operator family entries INSERT INTO upgrade_diffs SELECT '1.0.0', * FROM pg_temp.n2i_diff('n2i_catalog', 'snap_base') -- Expected: none SELECT * FROM upgrade_diffs ORDER BY 1, 2, 3, 4, 5, 6, 7, 8; source | side | cat | am | family | lt | rt | num | purpose | member | extra --------+------+-----+----+--------+----+----+-----+---------+--------+------- (0 rows) \c :regress_db DROP DATABASE n2i_lifecycle;