DROP SCHEMA IF EXISTS test_fkey_to_ref_in_tx CASCADE;
CREATE SCHEMA test_fkey_to_ref_in_tx;

CREATE TABLE t1(id int PRIMARY KEY);
CREATE TABLE t2(id int PRIMARY KEY, value_1 int);

ALTER TABLE 
	t2 
ADD CONSTRAINT 
	fkey_delete FOREIGN KEY(value_1) 
REFERENCES 
	t1(id) ON DELETE CASCADE;

INSERT INTO t1 SELECT i FROM generate_series(0, 10) i;
INSERT INTO t2 SELECT i, i % 10  FROM generate_series(0, 100) i;

WITH t1 AS (DELETE FROM t1 RETURNING id) 
	DELETE FROM t2 USING t1 WHERE value_1 = t1.id;

INSERT INTO t1 SELECT i FROM generate_series(0, 10) i;
INSERT INTO t2 SELECT i, i % 10  FROM generate_series(0, 100) i;

DELETE FROM t1 RETURNING id;