BEGIN TRANSACTION;

CREATE SCHEMA test;

CREATE OR REPLACE FUNCTION test.check_array_size_equal(arr1 anyarray,
														arr2 anyarray)
RETURNS bool AS $$
	
	SELECT COALESCE(array_upper($1, 1), 0) = COALESCE(array_upper($2, 1), 0)

$$ LANGUAGE sql;

CREATE TABLE test.sample_one(
	id serial PRIMARY KEY,
	name text,
	param_names text[]
);

CREATE OR REPLACE FUNCTION test.check_params_array_size(
								id test.sample_one.id%type,
								params text[])
RETURNS bool AS $$

	SELECT (SELECT test.check_array_size_equal(t.param_names, $2)
			FROM test.sample_one AS t
			WHERE id = $1)
		IS TRUE
$$ LANGUAGE sql;

CREATE TABLE test.sample(
	id serial PRIMARY KEY,
	ref_id integer REFERENCES test.sample_one(id)
						ON UPDATE RESTRICT ON DELETE CASCADE,
	params text[],

	CONSTRAINT check_params_array_size
		CHECK (test.check_params_array_size(ref_id, params))
);

INSERT INTO test.sample_one(name) VALUES('test1');
INSERT INTO test.sample_one(name) VALUES('test2');
INSERT INTO test.sample_one(name, param_names) VALUES('test3', '{abc}');

INSERT INTO test.sample(ref_id, params) VALUES(2, '{}');
INSERT INTO test.sample(ref_id, params) VALUES(3, '{123}');
INSERT INTO test.sample(ref_id, params) VALUES(1, '{}');

COMMIT TRANSACTION;