-- A cleanup after AT_PASS_ADD_COL drops wr_idx.  The cleanup after
-- AT_PASS_DROP then tries to process the same OID again.
CREATE TABLE wr_multi_pass(a int, b int);
CREATE FUNCTION wr_multi_pass_image(wr_multi_pass)
RETURNS wr_multi_pass
LANGUAGE sql IMMUTABLE
RETURN $1;
CREATE INDEX wr_multi_pass_idx
ON wr_multi_pass ((wr_multi_pass_image(wr_multi_pass)));
ALTER TABLE wr_multi_pass ADD COLUMN c int, DROP COLUMN b;

-- ERROR: cache lookup failed for index ...


-- A whole-row Var over an RTE_FUNCTION is passed to
-- process_function_rte_ref() as WholeRowAttrNumber, although that function
-- accepts only positive output-column numbers.
CREATE TYPE wr_function_row AS (a int);
CREATE FUNCTION wr_function()
RETURNS SETOF wr_function_row
LANGUAGE sql IMMUTABLE
AS $$ SELECT ROW(1)::wr_function_row $$;
CREATE VIEW wr_function_view AS
SELECT f FROM wr_function() AS f;

-- ERROR: column -32768 of relation "f" does not exist


-- A whole-row partition expression has a reversed self-dependency, so it
-- is not found by RememberWholeRowDependentForRebuilding().  The existing
-- tuple remains in p1 after its partition key changes to 0, while a new
-- tuple with the same key is routed to p0.
CREATE FUNCTION wr_partition_key(record)
RETURNS int
LANGUAGE plpgsql IMMUTABLE
AS $$
BEGIN
    RETURN CASE WHEN $1 IS NOT NULL THEN 1 ELSE 0 END;
END
$$;

CREATE TABLE wr_partitioned(a int)
PARTITION BY RANGE (wr_partition_key(wr_partitioned));
CREATE TABLE wr_partitioned_p0 PARTITION OF wr_partitioned
FOR VALUES FROM (0) TO (1);
CREATE TABLE wr_partitioned_p1 PARTITION OF wr_partitioned
FOR VALUES FROM (1) TO (2);

INSERT INTO wr_partitioned VALUES (1);
ALTER TABLE wr_partitioned ADD COLUMN b int;
INSERT INTO wr_partitioned(a) VALUES (2);

SELECT tableoid::regclass, wr_partition_key(wr_partitioned), wr_partitioned
FROM wr_partitioned
ORDER BY a;

--       tableoid       | wr_partition_key | wr_partitioned
-- ---------------------+------------------+----------------
--  wr_partitioned_p1   |                0 | (1,)
--  wr_partitioned_p0   |                0 | (2,)


-- pg_describe_object() knows the new pseudo-subobject, but the identity
-- functions currently treat it as an ordinary column.
SELECT pg_describe_object('pg_class'::regclass,
                          'wr_multi_pass'::regclass, -32768);
SELECT * FROM pg_identify_object('pg_class'::regclass,
                                 'wr_multi_pass'::regclass, -32768);
SELECT * FROM pg_identify_object_as_address('pg_class'::regclass,
                                            'wr_multi_pass'::regclass,
                                            -32768);
