-- Test: join chains that link two integer columns through a float column -- Purpose: x.i = f.f AND f.f = y.j puts both integer columns and the float -- column in one equivalence class whose btree families are -- {float_ops, integer_ops}. The planner derives x.i = y.j and -- costs merge joins on it with the first family, float_ops. 1.0.0 -- had no cross-width integer entries in float_ops, so a chain with -- two different integer widths failed to plan with -- "operator N is not a member of opfamily 1970" (float_ops). -- 1.0.1 adds them. -- Expected: every query returns its count (no ERROR), at default settings -- and with merge joins forced. CREATE EXTENSION IF NOT EXISTS pg_num2int_direct_comp; NOTICE: extension "pg_num2int_direct_comp" already exists, skipping CREATE TEMP TABLE cw_int2 AS SELECT g::int2 AS v FROM generate_series(1, 1000) g; CREATE TEMP TABLE cw_int4 AS SELECT g::int4 AS v FROM generate_series(1, 1000) g; CREATE TEMP TABLE cw_int8 AS SELECT g::int8 AS v FROM generate_series(1, 1000) g; CREATE TEMP TABLE cw_float4 AS SELECT g::float4 AS v FROM generate_series(1, 1000) g; CREATE TEMP TABLE cw_float8 AS SELECT g::float8 AS v FROM generate_series(1, 1000) g; CREATE INDEX ON cw_int2 (v); CREATE INDEX ON cw_int4 (v); CREATE INDEX ON cw_int8 (v); CREATE INDEX ON cw_float4 (v); CREATE INDEX ON cw_float8 (v); ANALYZE cw_int2, cw_int4, cw_int8, cw_float4, cw_float8; -- Runs one query and returns its single value, or the error text, so one -- planner failure does not hide the other combinations. CREATE FUNCTION pg_temp.cw_run(q text) RETURNS text LANGUAGE plpgsql AS $$ DECLARE r text; BEGIN EXECUTE q INTO r; RETURN r; EXCEPTION WHEN OTHERS THEN RETURN 'ERROR: ' || SQLERRM; END $$; CREATE TEMP TABLE cw_cases AS SELECT a, f, b FROM unnest(ARRAY['int2', 'int4', 'int8']) a, unnest(ARRAY['float4', 'float8']) f, unnest(ARRAY['int2', 'int4', 'int8']) b; -- Each query: the 3-way count (expected 1000), and an ORDER BY over the -- class with LIMIT, which needs the merge pathkey (expected 1). CREATE FUNCTION pg_temp.cw_matrix() RETURNS TABLE (a text, f text, b text, join_count text, order_by text) LANGUAGE sql AS $$ SELECT a, f, b, pg_temp.cw_run(format( 'SELECT count(*) FROM cw_%s x JOIN cw_%s m ON x.v = m.v JOIN cw_%s y ON m.v = y.v', a, f, b)), pg_temp.cw_run(format( 'SELECT x.v FROM cw_%s x JOIN cw_%s m ON x.v = m.v JOIN cw_%s y ON m.v = y.v ORDER BY y.v LIMIT 1', a, f, b)) FROM cw_cases ORDER BY a, f, b $$; -- Default planner settings. Expected: no row (every count 1000, first 1). SELECT * FROM pg_temp.cw_matrix() WHERE join_count IS DISTINCT FROM '1000' OR order_by IS DISTINCT FROM '1'; a | f | b | join_count | order_by ---+---+---+------------+---------- (0 rows) -- Merge joins only. Expected: no row. SET enable_hashjoin = off; SET enable_nestloop = off; SELECT * FROM pg_temp.cw_matrix() WHERE join_count IS DISTINCT FROM '1000' OR order_by IS DISTINCT FROM '1'; a | f | b | join_count | order_by ---+---+---+------------+---------- (0 rows) RESET enable_hashjoin; RESET enable_nestloop; -- The checks above ran all 18 combinations (3 x 2 x 3). SELECT count(*) AS cases FROM cw_cases; cases ------- 18 (1 row) -- Btree family completeness. amvalidate() checks the whole family of the -- given operator class. float_ops and numeric_ops are complete; integer_ops -- still lacks numeric x float, which the extension does not provide (README, -- Limitations), so it reports that and returns false. SELECT c.opcname, f.opfname, amvalidate(c.oid) FROM pg_opclass c JOIN pg_opfamily f ON f.oid = c.opcfamily JOIN pg_am m ON m.oid = c.opcmethod WHERE m.amname = 'btree' AND c.opcname IN ('float8_ops', 'int4_ops', 'numeric_ops') ORDER BY f.opfname; INFO: operator family "integer_ops" of access method btree is missing cross-type operator(s) opcname | opfname | amvalidate -------------+-------------+------------ float8_ops | float_ops | t int4_ops | integer_ops | f numeric_ops | numeric_ops | t (3 rows) -- The type pairs integer_ops lacks (no strategy 3 entry). Expected: only -- numeric x float4 and numeric x float8, in both orders. WITH fam AS ( SELECT f.oid FROM pg_opfamily f JOIN pg_am m ON m.oid = f.opfmethod WHERE m.amname = 'btree' AND f.opfname = 'integer_ops'), types AS ( SELECT DISTINCT amoplefttype AS t FROM pg_amop, fam WHERE amopfamily = fam.oid) SELECT l.t::regtype AS lefttype, r.t::regtype AS righttype FROM types l, types r, fam WHERE NOT EXISTS ( SELECT 1 FROM pg_amop a WHERE a.amopfamily = fam.oid AND a.amoplefttype = l.t AND a.amoprighttype = r.t AND a.amopstrategy = 3) ORDER BY l.t::regtype::text, r.t::regtype::text; lefttype | righttype ------------------+------------------ double precision | numeric numeric | double precision numeric | real real | numeric (4 rows)