ALTER SYSTEM SET max_parallel_workers_per_gather = 0;
SELECT pg_reload_conf();

CREATE EXTENSION postgres_fdw;
CREATE EXTENSION pg_prewarm;


DO $d$
    BEGIN
        EXECUTE $$CREATE SERVER loopback FOREIGN DATA WRAPPER postgres_fdw
            OPTIONS (dbname '$$||current_database()||$$',
                     port '$$||current_setting('port')||$$'
            )$$;
    END;
$d$;

CREATE USER MAPPING FOR CURRENT_USER SERVER loopback;

CREATE TABLE public.t (a INT);
INSERT INTO t SELECT x FROM generate_series(1,1000000) x;
VACUUM FREEZE ANALYZE t;
SELECT pg_prewarm('t');

CREATE FOREIGN TABLE ft (
	a INT
) SERVER loopback  OPTIONS (schema_name 'public', table_name 't');

EXPLAIN (ANALYZE) SELECT COUNT(*) FROM ft;
EXPLAIN (ANALYZE) SELECT COUNT(*) FROM t;

DROP FOREIGN TABLE ft;
DROP TABLE t;
DROP SERVER loopback CASCADE;
ALTER SYSTEM RESET max_parallel_workers_per_gather;
SELECT pg_reload_conf();
