--
|
-- one_c_temp_memory_catalog_toast
|
--
|
|
SET client_min_messages = warning;
|
|
SET enable_temp_memory_catalog = off;
|
|
CREATE TEMP TABLE tmc_toast_phys_parent(
|
id integer
|
);
|
|
DO $$
|
DECLARE
|
parent_oid oid;
|
toast_nsp text;
|
toast_oid oid;
|
toast_index_oid oid;
|
class_rows bigint;
|
attr_rows bigint;
|
pg_index_rows bigint;
|
depend_rows bigint;
|
BEGIN
|
parent_oid := to_regclass('pg_temp.tmc_toast_phys_parent');
|
|
IF parent_oid IS NULL THEN
|
RAISE EXCEPTION 'tmc_toast_phys_parent is missing';
|
END IF;
|
END;
|
$$;
|
|
SET enable_temp_memory_catalog = on;
|
|
ALTER TABLE pg_temp.tmc_toast_phys_parent
|
ADD COLUMN payload text;
|
|
ALTER TABLE pg_temp.tmc_toast_phys_parent
|
ALTER COLUMN payload SET STORAGE EXTERNAL;
|
|
DO $$
|
DECLARE
|
parent_oid oid;
|
temp_nsp text;
|
toast_nsp text;
|
toast_oid oid;
|
toast_index_oid oid;
|
class_rows bigint;
|
attr_rows bigint;
|
pg_index_rows bigint;
|
depend_rows bigint;
|
BEGIN
|
parent_oid := to_regclass('pg_temp.tmc_toast_phys_parent');
|
|
IF parent_oid IS NULL THEN
|
RAISE EXCEPTION 'tmc_toast_phys_parent disappeared before TOAST checks';
|
END IF;
|
|
SELECT nspname INTO temp_nsp
|
FROM pg_catalog.pg_namespace
|
WHERE oid = pg_my_temp_schema();
|
|
toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
|
|
toast_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', parent_oid)));
|
toast_index_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', parent_oid)));
|
|
IF toast_oid IS NULL OR toast_index_oid IS NULL THEN
|
RAISE EXCEPTION
|
'physical control TOAST objects failed to resolve';
|
END IF;
|
|
SELECT count(*) INTO class_rows
|
FROM pg_catalog.pg_class
|
WHERE oid = toast_oid;
|
|
IF class_rows <> 1 THEN
|
RAISE EXCEPTION 'unexpected pg_class rows for physical TOAST relation: %',
|
class_rows;
|
END IF;
|
|
SELECT count(*) INTO class_rows
|
FROM pg_catalog.pg_class
|
WHERE oid = toast_index_oid;
|
|
IF class_rows <> 1 THEN
|
RAISE EXCEPTION 'unexpected pg_class rows for physical TOAST index: %',
|
class_rows;
|
END IF;
|
|
SELECT count(*) INTO attr_rows
|
FROM pg_catalog.pg_attribute
|
WHERE attrelid = toast_oid
|
AND attnum > 0;
|
|
IF attr_rows <= 0 THEN
|
RAISE EXCEPTION 'unexpected pg_attribute rows for physical TOAST relation: %',
|
attr_rows;
|
END IF;
|
|
SELECT count(*) INTO attr_rows
|
FROM pg_catalog.pg_attribute
|
WHERE attrelid = toast_index_oid
|
AND attnum > 0;
|
|
IF attr_rows <= 0 THEN
|
RAISE EXCEPTION 'unexpected pg_attribute rows for physical TOAST index: %',
|
attr_rows;
|
END IF;
|
|
SELECT count(*) INTO pg_index_rows
|
FROM pg_catalog.pg_index
|
WHERE indexrelid = toast_index_oid;
|
|
IF pg_index_rows <> 1 THEN
|
RAISE EXCEPTION 'unexpected pg_index rows for physical TOAST index: %',
|
pg_index_rows;
|
END IF;
|
|
SELECT count(*) INTO depend_rows
|
FROM pg_catalog.pg_depend
|
WHERE classid = 'pg_class'::regclass
|
AND objid = toast_oid
|
AND refclassid = 'pg_class'::regclass
|
AND refobjid = parent_oid
|
AND deptype = 'i';
|
|
IF depend_rows <> 1 THEN
|
RAISE EXCEPTION 'unexpected physical dependency rows for TOAST relation: %',
|
depend_rows;
|
END IF;
|
END;
|
$$;
|
|
INSERT INTO pg_temp.tmc_toast_phys_parent
|
VALUES (
|
1,
|
'phys:' || repeat('P', 36000) || ':tail'
|
);
|
|
SELECT length(payload) AS phys_payload_len,
|
left(payload, 5) AS phys_prefix,
|
right(payload, 5) AS phys_suffix
|
FROM pg_temp.tmc_toast_phys_parent;
|
|
DROP TABLE pg_temp.tmc_toast_phys_parent;
|
|
SET enable_temp_memory_catalog = on;
|
|
CREATE TEMP TABLE tmc_toast_mem_parent(
|
id integer
|
);
|
|
DO $$
|
DECLARE
|
parent_oid oid;
|
class_rows bigint;
|
BEGIN
|
parent_oid := to_regclass('pg_temp.tmc_toast_mem_parent');
|
|
IF parent_oid IS NULL THEN
|
RAISE EXCEPTION 'tmc_toast_mem_parent is missing';
|
END IF;
|
|
SELECT count(*) INTO class_rows
|
FROM pg_catalog.pg_class
|
WHERE oid = parent_oid;
|
|
IF class_rows <> 0 THEN
|
RAISE EXCEPTION
|
'unexpected physical pg_class rowcount for tmc_toast_mem_parent: got %, expected 0',
|
class_rows;
|
END IF;
|
END;
|
$$;
|
|
SET enable_temp_memory_catalog = off;
|
|
CREATE TEMP TABLE tmc_toast_state(
|
parent_oid oid,
|
toast_oid oid,
|
toast_index_oid oid
|
);
|
|
ALTER TABLE pg_temp.tmc_toast_mem_parent
|
ADD COLUMN payload text;
|
|
ALTER TABLE pg_temp.tmc_toast_mem_parent
|
ALTER COLUMN payload SET STORAGE EXTERNAL;
|
|
SELECT to_regclass('pg_temp.tmc_toast_mem_parent') IS NOT NULL AS mem_parent_visible;
|
|
DO $$
|
DECLARE
|
parent_oid oid;
|
temp_nsp text;
|
toast_nsp text;
|
toast_oid oid;
|
toast_index_oid oid;
|
index_def text;
|
class_rows bigint;
|
attr_rows bigint;
|
pg_index_rows bigint;
|
depend_rows bigint;
|
BEGIN
|
parent_oid := to_regclass('pg_temp.tmc_toast_mem_parent');
|
|
IF parent_oid IS NULL THEN
|
RAISE EXCEPTION 'tmc_toast_mem_parent disappeared before TOAST checks';
|
END IF;
|
|
SELECT nspname INTO temp_nsp
|
FROM pg_catalog.pg_namespace
|
WHERE oid = pg_my_temp_schema();
|
|
toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
|
|
toast_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', parent_oid)));
|
toast_index_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', parent_oid)));
|
|
IF toast_oid IS NULL OR toast_index_oid IS NULL THEN
|
RAISE EXCEPTION 'memory-backed TOAST objects failed to resolve';
|
END IF;
|
|
index_def := pg_get_indexdef(toast_index_oid);
|
IF index_def IS NULL
|
OR position('UNIQUE INDEX ' IN index_def) = 0
|
OR position(' USING btree (chunk_id, chunk_seq)' IN index_def) = 0 THEN
|
RAISE EXCEPTION 'unexpected TOAST index definition visibility';
|
END IF;
|
|
SELECT count(*) INTO class_rows
|
FROM pg_catalog.pg_class
|
WHERE oid = toast_oid;
|
|
SELECT count(*) INTO attr_rows
|
FROM pg_catalog.pg_attribute
|
WHERE attrelid = toast_oid
|
AND attnum > 0;
|
|
SELECT count(*) INTO pg_index_rows
|
FROM pg_catalog.pg_index
|
WHERE indexrelid = toast_index_oid;
|
|
SELECT count(*) INTO depend_rows
|
FROM pg_catalog.pg_depend
|
WHERE classid = 'pg_class'::regclass
|
AND objid = toast_oid
|
AND refclassid = 'pg_class'::regclass
|
AND refobjid = parent_oid
|
AND deptype = 'i';
|
|
IF class_rows <> 0 OR attr_rows <> 0 OR pg_index_rows <> 0 OR depend_rows <> 0 THEN
|
RAISE EXCEPTION
|
'unexpected physical spill for memory TOAST relation: class %, attr %, pg_index %, depend %',
|
class_rows, attr_rows, pg_index_rows, depend_rows;
|
END IF;
|
|
SELECT count(*) INTO class_rows
|
FROM pg_catalog.pg_class
|
WHERE oid = toast_index_oid;
|
|
SELECT count(*) INTO attr_rows
|
FROM pg_catalog.pg_attribute
|
WHERE attrelid = toast_index_oid
|
AND attnum > 0;
|
|
IF class_rows <> 0 OR attr_rows <> 0 THEN
|
RAISE EXCEPTION
|
'unexpected physical spill for memory TOAST index: class %, attr %',
|
class_rows, attr_rows;
|
END IF;
|
|
INSERT INTO pg_temp.tmc_toast_state
|
VALUES (parent_oid, toast_oid, toast_index_oid);
|
|
SELECT count(*) INTO class_rows
|
FROM pg_temp.tmc_toast_state AS s
|
WHERE s.parent_oid IS NOT NULL
|
AND s.toast_oid IS NOT NULL
|
AND s.toast_index_oid IS NOT NULL;
|
|
IF class_rows <> 1 THEN
|
RAISE EXCEPTION 'known-good TOAST state capture failed';
|
END IF;
|
END;
|
$$;
|
|
INSERT INTO pg_temp.tmc_toast_mem_parent
|
VALUES (
|
1,
|
'mem:' || repeat('M', 36000) || ':tail'
|
);
|
|
SELECT length(payload) AS mem_payload_len,
|
left(payload, 4) AS mem_prefix,
|
right(payload, 5) AS mem_suffix
|
FROM pg_temp.tmc_toast_mem_parent;
|
|
UPDATE pg_temp.tmc_toast_mem_parent
|
SET payload = 'upd:' || repeat('U', 37000) || ':done'
|
WHERE id = 1;
|
|
SELECT length(payload) AS mem_updated_payload_len,
|
left(payload, 4) AS mem_updated_prefix,
|
right(payload, 5) AS mem_updated_suffix
|
FROM pg_temp.tmc_toast_mem_parent;
|
|
SET enable_temp_memory_catalog = on;
|
|
CREATE TEMP TABLE tmc_toast_rb_parent(
|
id integer
|
);
|
|
INSERT INTO pg_temp.tmc_toast_rb_parent VALUES (1);
|
|
SET enable_temp_memory_catalog = off;
|
|
BEGIN;
|
SAVEPOINT tmc_toast_rb_s;
|
|
ALTER TABLE pg_temp.tmc_toast_rb_parent
|
ADD COLUMN payload text;
|
|
DO $$
|
DECLARE
|
parent_oid oid;
|
temp_nsp text;
|
toast_nsp text;
|
toast_oid oid;
|
toast_index_oid oid;
|
BEGIN
|
parent_oid := to_regclass('pg_temp.tmc_toast_rb_parent');
|
|
IF parent_oid IS NULL THEN
|
RAISE EXCEPTION 'tmc_toast_rb_parent is missing inside rollback test';
|
END IF;
|
|
SELECT nspname INTO temp_nsp
|
FROM pg_catalog.pg_namespace
|
WHERE oid = pg_my_temp_schema();
|
|
toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
|
|
toast_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', parent_oid)));
|
toast_index_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', parent_oid)));
|
|
IF toast_oid IS NULL OR toast_index_oid IS NULL THEN
|
RAISE EXCEPTION 'rollback TOAST objects failed to resolve inside savepoint';
|
END IF;
|
END;
|
$$;
|
|
SELECT to_regclass('pg_temp.tmc_toast_rb_parent') IS NOT NULL AS rb_parent_visible_inside;
|
|
ROLLBACK TO SAVEPOINT tmc_toast_rb_s;
|
RELEASE SAVEPOINT tmc_toast_rb_s;
|
COMMIT;
|
|
DO $$
|
DECLARE
|
saved_parent_oid oid;
|
saved_toast_oid oid;
|
saved_toast_index_oid oid;
|
parent_oid oid;
|
temp_nsp text;
|
toast_nsp text;
|
toast_oid oid;
|
toast_index_oid oid;
|
logical_columns text[];
|
class_rows bigint;
|
payload_len integer;
|
payload_prefix text;
|
payload_suffix text;
|
BEGIN
|
parent_oid := to_regclass('pg_temp.tmc_toast_rb_parent');
|
|
IF parent_oid IS NULL THEN
|
RAISE EXCEPTION 'tmc_toast_rb_parent is missing after rollback';
|
END IF;
|
|
SELECT array_agg(keys.key ORDER BY keys.key)
|
INTO logical_columns
|
FROM pg_temp.tmc_toast_rb_parent AS t
|
CROSS JOIN LATERAL
|
jsonb_object_keys(to_jsonb(t)) AS keys(key);
|
|
IF logical_columns IS DISTINCT FROM ARRAY['id']::text[] THEN
|
RAISE EXCEPTION 'unexpected rollback logical columns: %',
|
logical_columns;
|
END IF;
|
|
SELECT nspname INTO temp_nsp
|
FROM pg_catalog.pg_namespace
|
WHERE oid = pg_my_temp_schema();
|
|
toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
|
|
toast_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', parent_oid)));
|
toast_index_oid := to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', parent_oid)));
|
|
IF toast_oid IS NOT NULL OR toast_index_oid IS NOT NULL THEN
|
RAISE EXCEPTION 'rolled back TOAST objects still resolve';
|
END IF;
|
|
SELECT count(*) INTO class_rows
|
FROM pg_catalog.pg_class
|
WHERE relname = format('pg_toast_%s', parent_oid);
|
|
IF class_rows <> 0 THEN
|
RAISE EXCEPTION 'rolled back TOAST relation spilled physically: %',
|
class_rows;
|
END IF;
|
|
SELECT count(*) INTO class_rows
|
FROM pg_catalog.pg_class
|
WHERE relname = format('pg_toast_%s_index', parent_oid);
|
|
IF class_rows <> 0 THEN
|
RAISE EXCEPTION 'rolled back TOAST index spilled physically: %',
|
class_rows;
|
END IF;
|
|
SELECT count(*),
|
min(s.parent_oid),
|
min(s.toast_oid),
|
min(s.toast_index_oid)
|
INTO class_rows,
|
saved_parent_oid,
|
saved_toast_oid,
|
saved_toast_index_oid
|
FROM pg_temp.tmc_toast_state AS s
|
WHERE s.parent_oid IS NOT NULL
|
AND s.toast_oid IS NOT NULL
|
AND s.toast_index_oid IS NOT NULL;
|
|
IF class_rows <> 1 THEN
|
RAISE EXCEPTION 'committed memory TOAST state row is missing';
|
END IF;
|
|
toast_oid := to_regclass(
|
format('%I.%I', toast_nsp, format('pg_toast_%s', saved_parent_oid)));
|
toast_index_oid := to_regclass(
|
format('%I.%I', toast_nsp, format('pg_toast_%s_index', saved_parent_oid)));
|
|
IF toast_oid IS DISTINCT FROM saved_toast_oid THEN
|
RAISE EXCEPTION
|
'committed memory TOAST relation was damaged by unrelated subtransaction rollback';
|
END IF;
|
|
IF toast_index_oid IS DISTINCT FROM saved_toast_index_oid THEN
|
RAISE EXCEPTION
|
'committed memory TOAST index was damaged by unrelated subtransaction rollback';
|
END IF;
|
|
SELECT length(payload),
|
left(payload, 4),
|
right(payload, 5)
|
INTO payload_len,
|
payload_prefix,
|
payload_suffix
|
FROM pg_temp.tmc_toast_mem_parent
|
WHERE id = 1;
|
|
IF payload_len <> 37009
|
OR payload_prefix <> 'upd:'
|
OR payload_suffix <> ':done' THEN
|
RAISE EXCEPTION
|
'committed oversized TOAST payload became unusable after unrelated rollback';
|
END IF;
|
END;
|
$$;
|
|
SET enable_temp_memory_catalog = off;
|
|
DROP TABLE pg_temp.tmc_toast_mem_parent;
|
|
DO $$
|
DECLARE
|
saved_parent_oid oid;
|
saved_toast_oid oid;
|
saved_toast_index_oid oid;
|
temp_nsp text;
|
toast_nsp text;
|
toast_class_rows bigint;
|
toast_attr_rows bigint;
|
index_class_rows bigint;
|
index_attr_rows bigint;
|
pg_index_rows bigint;
|
BEGIN
|
SELECT count(*),
|
min(s.parent_oid),
|
min(s.toast_oid),
|
min(s.toast_index_oid)
|
INTO toast_class_rows,
|
saved_parent_oid,
|
saved_toast_oid,
|
saved_toast_index_oid
|
FROM pg_temp.tmc_toast_state AS s
|
WHERE s.parent_oid IS NOT NULL
|
AND s.toast_oid IS NOT NULL
|
AND s.toast_index_oid IS NOT NULL;
|
|
IF toast_class_rows <> 1 THEN
|
RAISE EXCEPTION 'saved TOAST cleanup state is missing';
|
END IF;
|
|
IF to_regclass('pg_temp.tmc_toast_mem_parent') IS NOT NULL THEN
|
RAISE EXCEPTION 'tmc_toast_mem_parent still resolves after drop';
|
END IF;
|
|
SELECT nspname INTO temp_nsp
|
FROM pg_catalog.pg_namespace
|
WHERE oid = pg_my_temp_schema();
|
|
toast_nsp := replace(temp_nsp, 'pg_temp_', 'pg_toast_temp_');
|
|
IF to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s', saved_parent_oid))) IS NOT NULL
|
OR to_regclass(format('%I.%I', toast_nsp, format('pg_toast_%s_index', saved_parent_oid))) IS NOT NULL THEN
|
RAISE EXCEPTION 'dropped memory TOAST objects still resolve';
|
END IF;
|
|
SELECT count(*) INTO toast_class_rows
|
FROM pg_catalog.pg_class
|
WHERE oid = saved_toast_oid;
|
|
SELECT count(*) INTO toast_attr_rows
|
FROM pg_catalog.pg_attribute
|
WHERE attrelid = saved_toast_oid
|
AND attnum > 0;
|
|
SELECT count(*) INTO index_class_rows
|
FROM pg_catalog.pg_class
|
WHERE oid = saved_toast_index_oid;
|
|
SELECT count(*) INTO index_attr_rows
|
FROM pg_catalog.pg_attribute
|
WHERE attrelid = saved_toast_index_oid
|
AND attnum > 0;
|
|
SELECT count(*) INTO pg_index_rows
|
FROM pg_catalog.pg_index
|
WHERE indexrelid = saved_toast_index_oid;
|
|
IF toast_class_rows <> 0
|
OR toast_attr_rows <> 0
|
OR index_class_rows <> 0
|
OR index_attr_rows <> 0
|
OR pg_index_rows <> 0 THEN
|
RAISE EXCEPTION
|
'dropped memory TOAST metadata spilled physically: toast class %, toast attr %, index class %, index attr %, pg_index %',
|
toast_class_rows, toast_attr_rows, index_class_rows, index_attr_rows, pg_index_rows;
|
END IF;
|
END;
|
$$;
|
|
SELECT to_regclass('pg_temp.tmc_toast_mem_parent') IS NULL AS mem_parent_dropped;
|
|
DROP TABLE pg_temp.tmc_toast_rb_parent;
|
DROP TABLE pg_temp.tmc_toast_state;
|
|
RESET enable_temp_memory_catalog;
|