CREATE TABLE public.test_geom_table(id serial);
SELECT AddGeometryColumn ('public','test_geom_table','geom',4326,'MULTIPOLYGON',2);

CREATE OR REPLACE FUNCTION public.get_random_number(in_start int, in_end int) RETURNS int AS $$
BEGIN
    RETURN trunc(random() * (in_end - in_start) + in_start);
END;
$$ LANGUAGE 'plpgsql' STRICT;

CREATE OR REPLACE FUNCTION public.func_generate_junk_data(in_total bigint) RETURNS void
    LANGUAGE plpgsql
    AS $$
DECLARE
	_i int;
	_geoms geometry[] := ARRAY[
		'0106000020E610000001000000010300000001000000050000000000000084C65EC081EA9A04E7A048400000000084C65EC060E109FC259E484000000000F2C15EC060E109FC259E484000000000F2C15EC081EA9A04E7A048400000000084C65EC081EA9A04E7A04840'::geometry,
		'0106000020E61000000100000001030000000100000005000000349A5E84F5ED5CC0A66CEE0B104A4240349A5E84F5ED5CC0B8DEFD86874B4240F465A13B3CEB5CC0B8DEFD86874B4240F465A13B3CEB5CC0A66CEE0B104A4240349A5E84F5ED5CC0A66CEE0B104A4240'::geometry,
		'0106000020E6100000010000000103000000010000000500000000000000A81634400E50224D5B0C404000000000A816344017212509660640400000000078193440172125096606404000000000781934400E50224D5B0C404000000000A81634400E50224D5B0C4040'::geometry,
		'0106000020E610000001000000010300000001000000050000000000000034343F4056FE9ED0D10C3E400000000034343F409C8CC591BB063E40000000004C493F409C8CC591BB063E40000000004C493F4056FE9ED0D10C3E400000000034343F4056FE9ED0D10C3E40'::geometry,
		'0106000020E6100000010000000103000000010000000500000000000000424440404D4602EB52292F400000000042444040E2FFCC34CE322F4000000000F04A4040E2FFCC34CE322F4000000000F04A40404D4602EB52292F4000000000424440404D4602EB52292F40'::geometry,
		'0106000020E61000000100000001030000000100000005000000000000004C6641402B3B87C019064040000000004C6641403256FE4D9C04404000000000BA6141403256FE4D9C04404000000000BA6141402B3B87C019064040000000004C6641402B3B87C019064040'::geometry,
		'0106000020E61000000100000001030000000100000005000000000000003EBF41408D9AC2BE04F14040000000003EBF4140E26D3AAB94F04040000000005AC14140E26D3AAB94F04040000000005AC141408D9AC2BE04F14040000000003EBF41408D9AC2BE04F14040'::geometry,
		'0106000020E6100000010000000103000000010000000500000000000000D8F74140351C52C923F63F4000000000D8F74140351C52C923F63F4000000000E0FE4140351C52C923F63F4000000000E0FE4140351C52C923F63F4000000000D8F74140351C52C923F63F40'::geometry,
		'0106000020E61000000100000001030000000100000005000000000000008C254240388EB2127DC14040000000008C254240B31535BF6FBF404000000000F4264240B31535BF6FBF404000000000F4264240388EB2127DC14040000000008C254240388EB2127DC14040'::geometry,
		'0106000020E61000000100000001030000000100000005000000F9FFFFFF71284640F3FFFFBF94C14040F9FFFFFF71284640B5F5281C24C440407414AE4753234640B5F5281C24C440407414AE4753234640F3FFFFBF94C14040F9FFFFFF71284640F3FFFFBF94C14040'::geometry,
		'0106000020E6100000010000000103000000010000000500000000000000D45047400F85B71805C3384000000000D4504740AD8444C306AC384000000000A6604740AD8444C306AC384000000000A66047400F85B71805C3384000000000D45047400F85B71805C33840'::geometry,
		'0106000020E610000001000000010300000001000000050000000000000040B5494020FB2E3B6BD641400000000040B54940FD33A17C6BD241400000000004C44940FD33A17C6BD241400000000004C4494020FB2E3B6BD641400000000040B5494020FB2E3B6BD64140'::geometry,
		'0106000020E6100000010000000103000000010000000500000000000080C5305140B7851D9A7847434000000080C53051401D13CC6B3049434000000000EA3151401D13CC6B3049434000000000EA315140B7851D9A7847434000000080C5305140B7851D9A78474340'::geometry,
		'0106000020E61000000100000001030000000100000005000000F9FFFF9FB75C51400C000080C6464140F9FFFF9FB75C5140CFF528DC55494140360AD743285A5140CFF528DC55494140360AD743285A51400C000080C6464140F9FFFF9FB75C51400C000080C6464140'::geometry,
		'0106000020E610000001000000010300000001000000050000000000000017405240C7999190D0DA40400000000017405240C376A913B2D5404000000080A0455240C376A913B2D5404000000080A0455240C7999190D0DA40400000000017405240C7999190D0DA4040'::geometry
	]::geometry[];
BEGIN
	FOR _i IN SELECT * FROM generate_series(1, in_total) LOOP
		INSERT INTO public.test_geom_table (geom) VALUES (_geoms[get_random_number(1, array_upper(_geoms, 1) + 1)]);
	END LOOP;
END;
$$;

SELECT public.func_generate_junk_data(5200000);

CREATE INDEX test_geom_table_geom_idx ON public.test_geom_table USING gist (geom);

SELECT id FROM public.test_geom_table WHERE ST_Intersects(ST_PolygonFromText('POLYGON(( -132.71472222222 51.508611111111, -132.71472222222 18.646111111111, -57.831944444444 18.646111111111, -57.831944444444 51.508611111111, -132.71472222222 51.508611111111 ))', 4326), geom);
