create language plpgsql;
\i /path/to/share/contrib/dblink.sql

CREATE OR REPLACE FUNCTION test_clog(howmany int) RETURNS int AS $_$
DECLARE
 i int;
 arr text[];
 dbname text;
BEGIN
 dbname := current_database();
 arr := dblink_get_connections();
 IF arr IS NOT NULL THEN
  PERFORM dblink_disconnect('conn');
 END IF;
 EXECUTE $$SELECT dblink_connect('conn','dbname=$$ || dbname || $$')$$;
 PERFORM dblink_exec('conn', 'DROP TABLE IF EXISTS clogtest');
 PERFORM dblink_exec('conn', 'CREATE TABLE clogtest(id int primary key)');
 PERFORM dblink_exec('conn', 'SET debug_pretty_print TO ON');
 FOR i IN 1..howmany LOOP
  PERFORM dblink_exec('conn', 'insert into clogtest values(' || i || ')', false);
  IF (i % 5000) = 0 THEN
   RAISE WARNING '% iterations performed', i;
   RAISE WARNING 'Last ERROR: %', dblink_error_message('conn');
  END IF;
 END LOOP;
 PERFORM dblink_disconnect('conn');
 RETURN howmany;
END;
$_$ LANGUAGE plpgsql
 SET client_min_messages TO WARNING;

SELECT test_clog(1000000);


