CREATE OR REPLACE FUNCTION array_to_set(anyarray) RETURNS SETOF anyelement
	AS $$
BEGIN
	CASE array_ndims($1)
		WHEN 1 THEN
			RETURN QUERY
				SELECT $1[a1] FROM
					generate_subscripts($1,1) a1;
		WHEN 2 THEN
			RETURN QUERY
				SELECT $1[a1][a2] FROM
					generate_subscripts($1,1) a1,
					generate_subscripts($1,2) a2;
		WHEN 3 THEN
			RETURN QUERY
				SELECT $1[a1][a2][a3] FROM
					generate_subscripts($1,1) a1,
					generate_subscripts($1,2) a2,
					generate_subscripts($1,3) a3;
		WHEN 4 THEN
			RETURN QUERY
				SELECT $1[a1][a2][a3][a4] FROM
					generate_subscripts($1,1) a1,
					generate_subscripts($1,2) a2,
					generate_subscripts($1,3) a3,
					generate_subscripts($1,4) a4;
		WHEN 5 THEN
			RETURN QUERY
				SELECT $1[a1][a2][a3][a4][a5] FROM
					generate_subscripts($1,1) a1,
					generate_subscripts($1,2) a2,
					generate_subscripts($1,3) a3,
					generate_subscripts($1,4) a4,
					generate_subscripts($1,5) a5;
		WHEN 6 THEN
			RETURN QUERY
				SELECT $1[a1][a2][a3][a4][a5][a6] FROM
					generate_subscripts($1,1) a1,
					generate_subscripts($1,2) a2,
					generate_subscripts($1,3) a3,
					generate_subscripts($1,4) a4,
					generate_subscripts($1,5) a5,
					generate_subscripts($1,6) a6;
	END CASE;
END
$$ LANGUAGE plpgsql;
