-- -- one_c_temp_memory_catalog_index -- SET client_min_messages = warning; SET enable_temp_memory_catalog = off; CREATE TEMP TABLE tmc_index_phys_parent( id integer, payload integer ); INSERT INTO pg_temp.tmc_index_phys_parent SELECT g, g * 10 FROM generate_series(1, 8) AS g; SET enable_temp_memory_catalog = on; CREATE INDEX tmc_index_phys_idx ON pg_temp.tmc_index_phys_parent USING btree (id); DO $$ DECLARE index_oid oid; class_rows bigint; attr_rows bigint; pg_index_rows bigint; BEGIN index_oid := to_regclass('pg_temp.tmc_index_phys_idx'); IF index_oid IS NULL THEN RAISE EXCEPTION 'tmc_index_phys_idx is missing'; END IF; SELECT count(*) INTO class_rows FROM pg_catalog.pg_class WHERE oid = index_oid; SELECT count(*) INTO attr_rows FROM pg_catalog.pg_attribute WHERE attrelid = index_oid AND attnum > 0; SELECT count(*) INTO pg_index_rows FROM pg_catalog.pg_index WHERE indexrelid = index_oid; IF class_rows <> 1 OR attr_rows <= 0 OR pg_index_rows <> 1 THEN RAISE EXCEPTION 'unexpected physical control rows for tmc_index_phys_idx: class %, attr %, pg_index %', class_rows, attr_rows, pg_index_rows; END IF; END; $$; DROP INDEX pg_temp.tmc_index_phys_idx; DROP TABLE pg_temp.tmc_index_phys_parent; SET enable_temp_memory_catalog = on; CREATE TEMP TABLE tmc_index_mem_parent( id integer, payload integer ); INSERT INTO pg_temp.tmc_index_mem_parent SELECT g, g * 100 FROM generate_series(1, 16) AS g; DO $$ DECLARE table_oid oid; class_rows bigint; BEGIN table_oid := to_regclass('pg_temp.tmc_index_mem_parent'); IF table_oid IS NULL THEN RAISE EXCEPTION 'tmc_index_mem_parent is missing'; END IF; SELECT count(*) INTO class_rows FROM pg_catalog.pg_class WHERE oid = table_oid; IF class_rows <> 0 THEN RAISE EXCEPTION 'unexpected physical pg_class rowcount for tmc_index_mem_parent: got %, expected 0', class_rows; END IF; END; $$; SET enable_temp_memory_catalog = off; CREATE INDEX tmc_index_mem_idx ON pg_temp.tmc_index_mem_parent USING btree (id); SELECT to_regclass('pg_temp.tmc_index_mem_idx') IS NOT NULL AS mem_idx_visible; DO $$ DECLARE index_oid oid; index_def text; plan json; node_type text; index_name text; BEGIN index_oid := to_regclass('pg_temp.tmc_index_mem_idx'); IF index_oid IS NULL THEN RAISE EXCEPTION 'tmc_index_mem_idx is missing'; END IF; index_def := pg_get_indexdef(index_oid); IF position('CREATE INDEX tmc_index_mem_idx ON ' IN index_def) <> 1 OR position(' USING btree (id)' IN index_def) = 0 THEN RAISE EXCEPTION 'unexpected index definition for tmc_index_mem_idx: %', index_def; END IF; PERFORM set_config('enable_seqscan', 'off', true); PERFORM set_config('enable_bitmapscan', 'off', true); EXECUTE 'EXPLAIN (FORMAT JSON) SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE id = 7' INTO plan; node_type := plan->0->'Plan'->>'Node Type'; index_name := plan->0->'Plan'->>'Index Name'; IF node_type NOT IN ('Index Scan', 'Index Only Scan') THEN RAISE EXCEPTION 'unexpected node type for tmc_index_mem_idx plan: %', node_type; END IF; IF index_name <> 'tmc_index_mem_idx' THEN RAISE EXCEPTION 'unexpected index name for tmc_index_mem_idx plan: %', index_name; END IF; END; $$; SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE id = 7; RESET enable_seqscan; RESET enable_bitmapscan; DO $$ DECLARE index_oid oid; class_rows bigint; attr_rows bigint; pg_index_rows bigint; depend_rows bigint; BEGIN index_oid := to_regclass('pg_temp.tmc_index_mem_idx'); IF index_oid IS NULL THEN RAISE EXCEPTION 'tmc_index_mem_idx disappeared before spill checks'; END IF; SELECT count(*) INTO class_rows FROM pg_catalog.pg_class WHERE oid = index_oid; SELECT count(*) INTO attr_rows FROM pg_catalog.pg_attribute WHERE attrelid = index_oid AND attnum > 0; SELECT count(*) INTO pg_index_rows FROM pg_catalog.pg_index WHERE indexrelid = index_oid; SELECT count(*) INTO depend_rows FROM pg_catalog.pg_depend WHERE classid = 'pg_class'::regclass AND objid = index_oid; IF class_rows <> 0 OR attr_rows <> 0 OR pg_index_rows <> 0 OR depend_rows <> 0 THEN RAISE EXCEPTION 'unexpected physical spill for tmc_index_mem_idx: class %, attr %, pg_index %, depend %', class_rows, attr_rows, pg_index_rows, depend_rows; END IF; END; $$; CREATE INDEX tmc_index_mem_multi ON pg_temp.tmc_index_mem_parent USING btree (id, payload); SELECT to_regtype('pg_temp.tmc_index_mem_multi') IS NOT NULL AS mem_multi_type_visible; DO $$ DECLARE index_oid oid; type_oid oid; type_rows bigint; class_rows bigint; attr_rows bigint; pg_index_rows bigint; BEGIN index_oid := to_regclass('pg_temp.tmc_index_mem_multi'); type_oid := to_regtype('pg_temp.tmc_index_mem_multi'); IF index_oid IS NULL THEN RAISE EXCEPTION 'tmc_index_mem_multi is missing'; END IF; IF type_oid IS NULL THEN RAISE EXCEPTION 'tmc_index_mem_multi type is missing'; END IF; SELECT count(*) INTO type_rows FROM pg_catalog.pg_type WHERE oid = type_oid; SELECT count(*) INTO class_rows FROM pg_catalog.pg_class WHERE oid = index_oid; SELECT count(*) INTO attr_rows FROM pg_catalog.pg_attribute WHERE attrelid = index_oid AND attnum > 0; SELECT count(*) INTO pg_index_rows FROM pg_catalog.pg_index WHERE indexrelid = index_oid; IF type_rows <> 0 OR class_rows <> 0 OR attr_rows <> 0 OR pg_index_rows <> 0 THEN RAISE EXCEPTION 'unexpected physical spill for tmc_index_mem_multi: type %, class %, attr %, pg_index %', type_rows, class_rows, attr_rows, pg_index_rows; END IF; END; $$; BEGIN; SAVEPOINT tmc_index_create_s; CREATE INDEX tmc_index_rb_idx ON pg_temp.tmc_index_mem_parent USING btree (payload); SELECT to_regclass('pg_temp.tmc_index_rb_idx') IS NOT NULL AS rb_created; ROLLBACK TO SAVEPOINT tmc_index_create_s; RELEASE SAVEPOINT tmc_index_create_s; COMMIT; SELECT to_regclass('pg_temp.tmc_index_rb_idx') IS NULL AS rb_missing_after_rollback; DO $$ DECLARE class_rows bigint; attr_rows bigint; pg_index_rows bigint; depend_rows bigint; BEGIN SELECT count(*) INTO class_rows FROM pg_catalog.pg_class WHERE relname = 'tmc_index_rb_idx'; SELECT count(*) INTO attr_rows FROM pg_catalog.pg_attribute WHERE attrelid IN ( SELECT oid FROM pg_catalog.pg_class WHERE relname = 'tmc_index_rb_idx' ) AND attnum > 0; SELECT count(*) INTO pg_index_rows FROM pg_catalog.pg_index WHERE indexrelid IN ( SELECT oid FROM pg_catalog.pg_class WHERE relname = 'tmc_index_rb_idx' ); SELECT count(*) INTO depend_rows FROM pg_catalog.pg_depend WHERE classid = 'pg_class'::regclass AND objid IN ( SELECT oid FROM pg_catalog.pg_class WHERE relname = 'tmc_index_rb_idx' ); IF class_rows <> 0 OR attr_rows <> 0 OR pg_index_rows <> 0 OR depend_rows <> 0 THEN RAISE EXCEPTION 'unexpected physical spill for rolled back tmc_index_rb_idx: class %, attr %, pg_index %, depend %', class_rows, attr_rows, pg_index_rows, depend_rows; END IF; END; $$; CREATE INDEX tmc_index_drop_rb_idx ON pg_temp.tmc_index_mem_parent USING btree (payload); BEGIN; SAVEPOINT tmc_index_drop_s; DROP INDEX pg_temp.tmc_index_drop_rb_idx; SELECT to_regclass('pg_temp.tmc_index_drop_rb_idx') IS NULL AS drop_missing_inside; ROLLBACK TO SAVEPOINT tmc_index_drop_s; RELEASE SAVEPOINT tmc_index_drop_s; COMMIT; SELECT to_regclass('pg_temp.tmc_index_drop_rb_idx') IS NOT NULL AS drop_restored; DO $$ DECLARE index_oid oid; index_def text; plan json; node_type text; index_name text; BEGIN index_oid := to_regclass('pg_temp.tmc_index_drop_rb_idx'); IF index_oid IS NULL THEN RAISE EXCEPTION 'tmc_index_drop_rb_idx is missing after rollback'; END IF; index_def := pg_get_indexdef(index_oid); IF position('CREATE INDEX tmc_index_drop_rb_idx ON ' IN index_def) <> 1 OR position(' USING btree (payload)' IN index_def) = 0 THEN RAISE EXCEPTION 'unexpected index definition for tmc_index_drop_rb_idx: %', index_def; END IF; PERFORM set_config('enable_seqscan', 'off', true); PERFORM set_config('enable_bitmapscan', 'off', true); EXECUTE 'EXPLAIN (FORMAT JSON) SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE payload = 800' INTO plan; node_type := plan->0->'Plan'->>'Node Type'; index_name := plan->0->'Plan'->>'Index Name'; IF node_type NOT IN ('Index Scan', 'Index Only Scan') THEN RAISE EXCEPTION 'unexpected node type for tmc_index_drop_rb_idx plan: %', node_type; END IF; IF index_name <> 'tmc_index_drop_rb_idx' THEN RAISE EXCEPTION 'unexpected index name for tmc_index_drop_rb_idx plan: %', index_name; END IF; END; $$; SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE payload = 800; RESET enable_seqscan; RESET enable_bitmapscan; ALTER INDEX pg_temp.tmc_index_drop_rb_idx RENAME TO tmc_index_drop_rb_idx_renamed; SELECT to_regclass('pg_temp.tmc_index_drop_rb_idx') IS NULL AS old_drop_name_missing; SELECT to_regclass('pg_temp.tmc_index_drop_rb_idx_renamed') IS NOT NULL AS new_drop_name_present; DO $$ DECLARE index_oid oid; index_def text; plan json; node_type text; index_name text; BEGIN index_oid := to_regclass('pg_temp.tmc_index_drop_rb_idx_renamed'); IF index_oid IS NULL THEN RAISE EXCEPTION 'tmc_index_drop_rb_idx_renamed is missing'; END IF; index_def := pg_get_indexdef(index_oid); IF position('CREATE INDEX tmc_index_drop_rb_idx_renamed ON ' IN index_def) <> 1 OR position(' USING btree (payload)' IN index_def) = 0 THEN RAISE EXCEPTION 'unexpected index definition for tmc_index_drop_rb_idx_renamed: %', index_def; END IF; PERFORM set_config('enable_seqscan', 'off', true); PERFORM set_config('enable_bitmapscan', 'off', true); EXECUTE 'EXPLAIN (FORMAT JSON) SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE payload = 800' INTO plan; node_type := plan->0->'Plan'->>'Node Type'; index_name := plan->0->'Plan'->>'Index Name'; IF node_type NOT IN ('Index Scan', 'Index Only Scan') THEN RAISE EXCEPTION 'unexpected node type for tmc_index_drop_rb_idx_renamed plan: %', node_type; END IF; IF index_name <> 'tmc_index_drop_rb_idx_renamed' THEN RAISE EXCEPTION 'unexpected index name for tmc_index_drop_rb_idx_renamed plan: %', index_name; END IF; END; $$; SELECT payload FROM pg_temp.tmc_index_mem_parent WHERE payload = 800; RESET enable_seqscan; RESET enable_bitmapscan; DROP INDEX pg_temp.tmc_index_mem_idx; DROP INDEX pg_temp.tmc_index_mem_multi; DROP INDEX pg_temp.tmc_index_drop_rb_idx_renamed; SELECT to_regclass('pg_temp.tmc_index_mem_idx') IS NULL AND to_regclass('pg_temp.tmc_index_mem_multi') IS NULL AND to_regclass('pg_temp.tmc_index_drop_rb_idx_renamed') IS NULL AS all_indexes_dropped; DROP TABLE pg_temp.tmc_index_mem_parent; RESET enable_temp_memory_catalog;