select 'id' in every table psql Each table in my database (see schema) has a primary key called id. 

I would like to find all records in all tables where id is in the range [1,1000]. How can I write such a query?

I've tried to write a for loop following the PostgreSQL docs 

FOR T IN (
  SELECT TABLE_NAME FROM information_schema. TABLES
  WHERE table_schema = 'public'
    )
LOOP
    SELECT id, T FROM T WHERE ID BETWEEN 1 AND 1000;
  RETURN NEXT T;
END LOOP ;


but get "Syntax error at FOR".
You can do this all in one statement pretty easily.

SELECT string_agg(statement, E'\nUNION ALL ') || ';'
FROM (
  SELECT FORMAT(
    $$SELECT '%s' AS table_catalog, '%s' AS table_schema, '%s' AS table_name, id FROM %I.%I.%I WHERE %I BETWEEN 1 AND 1000$$,
    table_catalog,
    table_schema,
    table_name,
    table_catalog,
    table_schema,
    table_name,
    column_name
  )
  FROM information_schema.columns
  WHERE
    table_catalog = CURRENT_CATALOG
    AND table_schema = 'public'
    AND column_name = 'id'
) AS t(statement);


That will generate a single statement. In psql, just run \gexec after that.