diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index aa9c8eed5dafabd49429874d973e22abe93d5294..2db67d6fd3d5616944ff0b086aa606af11e9eed4 100644
*** a/src/bin/pg_dump/pg_backup_archiver.c
--- b/src/bin/pg_dump/pg_backup_archiver.c
*************** static void _becomeUser(ArchiveHandle *A
*** 129,134 ****
--- 129,135 ----
  static void _becomeOwner(ArchiveHandle *AH, TocEntry *te);
  static void _selectOutputSchema(ArchiveHandle *AH, const char *schemaName);
  static void _selectTablespace(ArchiveHandle *AH, const char *tablespace);
+ static void writeDependencies(ArchiveHandle *AH, const DumpId *dependencies, int nDeps);
  static void processEncodingEntry(ArchiveHandle *AH, TocEntry *te);
  static void processStdStringsEntry(ArchiveHandle *AH, TocEntry *te);
  static teReqs _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt);
*************** WriteToc(ArchiveHandle *AH)
*** 2144,2150 ****
  	TocEntry   *te;
  	char		workbuf[32];
  	int			tocCount;
- 	int			i;
  
  	/* count entries that will actually be dumped */
  	tocCount = 0;
--- 2145,2150 ----
*************** WriteToc(ArchiveHandle *AH)
*** 2184,2195 ****
  		WriteStr(AH, te->withOids ? "true" : "false");
  
  		/* Dump list of dependencies */
! 		for (i = 0; i < te->nDeps; i++)
! 		{
! 			sprintf(workbuf, "%d", te->dependencies[i]);
! 			WriteStr(AH, workbuf);
! 		}
! 		WriteStr(AH, NULL);		/* Terminate List */
  
  		if (AH->WriteExtraTocPtr)
  			(*AH->WriteExtraTocPtr) (AH, te);
--- 2184,2191 ----
  		WriteStr(AH, te->withOids ? "true" : "false");
  
  		/* Dump list of dependencies */
! 		writeDependencies(AH, te->dependencies, te->nDeps);
! 		WriteStr(AH, NULL);		/* list terminator */
  
  		if (AH->WriteExtraTocPtr)
  			(*AH->WriteExtraTocPtr) (AH, te);
*************** ReadToc(ArchiveHandle *AH)
*** 2353,2358 ****
--- 2349,2400 ----
  	}
  }
  
+ /*
+  * Dump an object's dependencies in a usable form, ie, referencing only
+  * objects that are included in the dump.  We need to make this distinction
+  * since, for example, a view will depend on its _RETURN rule while the
+  * _RETURN rule will depend on other objects --- but the rule will not appear
+  * as a separate object in the dump.  We have to look through the rule to get
+  * a useful representation of the view's dependencies.
+  *
+  * We rely here on the assumption that sortDumpableObjects already broke any
+  * dependency loops, else we might recurse infinitely.
+  */
+ static void
+ writeDependencies(ArchiveHandle *AH, const DumpId *dependencies, int nDeps)
+ {
+ 	char		workbuf[32];
+ 	int			i;
+ 
+ 	for (i = 0; i < nDeps; i++)
+ 	{
+ 		DumpId		depid = dependencies[i];
+ 		TocEntry   *depte = getTocEntryByDumpId(AH, depid);
+ 
+ 		if (depte &&
+ 			(depte->reqs & (REQ_SCHEMA | REQ_DATA | REQ_SPECIAL)) != 0)
+ 		{
+ 			/* Object will be dumped, so just reference it as a dependency */
+ 			/* Dependency IDs are printed as strings for historical reasons */
+ 			sprintf(workbuf, "%d", depid);
+ 			WriteStr(AH, workbuf);
+ 		}
+ 		else
+ 		{
+ 			/*
+ 			 * Object will not be dumped, so recursively consider its deps.
+ 			 * Here we must resort to the underlying DumpableObject data
+ 			 * structures, since often there will be no TocEntry for such
+ 			 * an object.
+ 			 */
+ 			DumpableObject *dobj = findObjectByDumpId(depid);
+ 
+ 			if (dobj)
+ 				writeDependencies(AH, dobj->dependencies, dobj->nDeps);
+ 		}
+ 	}
+ }
+ 
  static void
  processEncodingEntry(ArchiveHandle *AH, TocEntry *te)
  {
