Actions to perform to reproduce the issue and test patch pg_dump.c-extension-FK.patch.

testdb=# CREATE EXTENSION test_fk_in_ext ;
CREATE EXTENSION
testdb=# \d
              List of relations
 Schema |       Name        | Type  | Owner  
--------+-------------------+-------+--------
 public | a_test_fk_in_ext1 | table | gilles
 public | b_test_fk_in_ext1 | table | gilles
(2 rows)

testdb=# INSERT INTO a_test_fk_in_ext1 VALUES (1), (2);
ERROR:  insert or update on table "a_test_fk_in_ext1" violates foreign key constraint "a_test_fk_in_ext1_id_fkey"
DETAIL:  Key (id)=(1) is not present in table "b_test_fk_in_ext1".
testdb=# INSERT INTO b_test_fk_in_ext1 VALUES (1), (2);
INSERT 0 2
testdb=# INSERT INTO a_test_fk_in_ext1 VALUES (1), (2);
INSERT 0 2
testdb=# 

Dump/restore with current version of pg_dump:
---------------------------------------------

pg_dump -s -h localhost testdb

	CREATE EXTENSION IF NOT EXISTS test_fk_in_ext WITH SCHEMA public;
	COMMENT ON EXTENSION test_fk_in_ext IS 'An extension to test dump-order table with FK';


pg_dump -h localhost testdb

	CREATE EXTENSION IF NOT EXISTS test_fk_in_ext WITH SCHEMA public;
	COMMENT ON EXTENSION test_fk_in_ext IS 'An extension to test dump-order table with FK';

	COPY a_test_fk_in_ext1 (id) FROM stdin;
	1
	2
	\.

	COPY b_test_fk_in_ext1 (id) FROM stdin;
	1
	2
	\.

Same with --data-only : pg_dump -a -h localhost testdb

	COPY a_test_fk_in_ext1 (id) FROM stdin;
	1
	2
	\.

	COPY b_test_fk_in_ext1 (id) FROM stdin;
	1
	2
	\.

This will fail when restoring because b_test_fk_in_ext1 should be loaded before a_test_fk_in_ext1
to respect the Foreign Key order.

psql -h localhost testdb2 < testdump.txt 
	SET
	SET
	SET
	SET
	SET
	SET
	CREATE EXTENSION
	COMMENT
	CREATE EXTENSION
	COMMENT
	SET
	ERROR:  insert or update on table "a_test_fk_in_ext1" violates foreign key constraint "a_test_fk_in_ext1_id_fkey"
	DETAIL:  Key (id)=(1) is not present in table "b_test_fk_in_ext1".
	REVOKE
	REVOKE
	GRANT
	GRANT


Dump/restore with pg_dump patched:
---------------------------------

/usr/local/postgresql/bin/pg_dump -s -h localhost testdb

	CREATE EXTENSION IF NOT EXISTS test_fk_in_ext WITH SCHEMA public;
	COMMENT ON EXTENSION test_fk_in_ext IS 'An extension to test dump-order table with FK';

Same as previous test with schema only export. Now try with data only:

/usr/local/postgresql/bin/pg_dump -a -h localhost testdb

	COPY b_test_fk_in_ext1 (id) FROM stdin;
	1
	2
	\.

	COPY a_test_fk_in_ext1 (id) FROM stdin;
	1
	2
	\.

Tables are well ordered following the foreign key. Then full export:

/usr/local/postgresql/bin/pg_dump -h localhost testdb

	CREATE EXTENSION IF NOT EXISTS test_fk_in_ext WITH SCHEMA public;
	COMMENT ON EXTENSION test_fk_in_ext IS 'An extension to test dump-order table with FK';

	COPY b_test_fk_in_ext1 (id) FROM stdin;
	1
	2
	\.

	COPY a_test_fk_in_ext1 (id) FROM stdin;
	1
	2
	\.

Restore it to verify:

psql -h localhost testdb2 < testdump.txt 

	SET
	SET
	SET
	SET
	SET
	SET
	CREATE EXTENSION
	COMMENT
	CREATE EXTENSION
	COMMENT
	SET
	REVOKE
	REVOKE
	GRANT
	GRANT


With custom format wa have the same behavior.


Dump/restore with a pg_dump patched:
------------------------------------

pg_restore -h localhost -v -Fc -d testdb2 dump.bin
	pg_restore: connecting to database for restore
	pg_restore: creating SCHEMA public
	pg_restore: creating COMMENT SCHEMA public
	pg_restore: creating EXTENSION plpgsql
	pg_restore: creating COMMENT EXTENSION plpgsql
	pg_restore: creating EXTENSION test_fk_in_ext
	pg_restore: creating COMMENT EXTENSION test_fk_in_ext
	pg_restore: processing data for table "b_test_fk_in_ext1"
	pg_restore: processing data for table "a_test_fk_in_ext1"
	pg_restore: setting owner and privileges for DATABASE testdb
	pg_restore: setting owner and privileges for SCHEMA public
	pg_restore: setting owner and privileges for COMMENT SCHEMA public
	pg_restore: setting owner and privileges for ACL public
	pg_restore: setting owner and privileges for EXTENSION plpgsql
	pg_restore: setting owner and privileges for COMMENT EXTENSION plpgsql
	pg_restore: setting owner and privileges for EXTENSION test_fk_in_ext
	pg_restore: setting owner and privileges for COMMENT EXTENSION test_fk_in_ext
	pg_restore: setting owner and privileges for TABLE DATA b_test_fk_in_ext1
	pg_restore: setting owner and privileges for TABLE DATA a_test_fk_in_ext1

and with current pg_dump:

pg_restore -h localhost -v -Fc -d testdb2 dump.bin
	pg_restore: connecting to database for restore
	pg_restore: creating SCHEMA public
	pg_restore: creating COMMENT SCHEMA public
	pg_restore: creating EXTENSION plpgsql
	pg_restore: creating COMMENT EXTENSION plpgsql
	pg_restore: creating EXTENSION test_fk_in_ext
	pg_restore: creating COMMENT EXTENSION test_fk_in_ext
	pg_restore: processing data for table "a_test_fk_in_ext1"
	pg_restore: [archiver (db)] Error while PROCESSING TOC:
	pg_restore: [archiver (db)] Error from TOC entry 1877; 0 157059661 TABLE DATA a_test_fk_in_ext1 gilles
	pg_restore: [archiver (db)] COPY failed for table "a_test_fk_in_ext1": ERROR:  insert or update on table "a_test_fk_in_ext1" violates foreign key constraint "a_test_fk_in_ext1_id_fkey"
	DETAIL:  Key (id)=(1) is not present in table "b_test_fk_in_ext1".
	pg_restore: processing data for table "b_test_fk_in_ext1"
	pg_restore: setting owner and privileges for DATABASE testdb
	pg_restore: setting owner and privileges for SCHEMA public
	pg_restore: setting owner and privileges for COMMENT SCHEMA public
	pg_restore: setting owner and privileges for ACL public
	pg_restore: setting owner and privileges for EXTENSION plpgsql
	pg_restore: setting owner and privileges for COMMENT EXTENSION plpgsql
	pg_restore: setting owner and privileges for EXTENSION test_fk_in_ext
	pg_restore: setting owner and privileges for COMMENT EXTENSION test_fk_in_ext
	pg_restore: setting owner and privileges for TABLE DATA a_test_fk_in_ext1
	pg_restore: setting owner and privileges for TABLE DATA b_test_fk_in_ext1
	WARNING: errors ignored on restore: 1

