From bccc5623f39a40a7ba3f63b3dcaf902259ad485c Mon Sep 17 00:00:00 2001
From: Craig Ringer <craig@2ndquadrant.com>
Date: Wed, 1 Apr 2015 10:46:29 +0800
Subject: [PATCH] pg_restore -t should select views, matviews, and foreign
 tables

Currently pg_restore's '-t' option selects only tables, not other
relations. It should be able to match anything that behaves like
a relation in the relation namespace, anything that's interchangable
with a table, including:

* Normal relations
* Views
* Materialized views
* Foreign tables

Sequences are not matched. They're in the relation namespace, but
only as an implementation detail. A separate option to selectively
dump sequences should be added so that there's no BC break if
they later become non-class objects.

Indexes are also not matched; again, a different option should be
added for them.

TOAST tables aren't matched, they're implementation detail.

See:
  http://www.postgresql.org/message-id/CAMsr+YGJ50TvTVK4Dbp66gAjeOC0KaP6KXFEHAOM+neQmHvoQA@mail.gmail.com
---
 doc/src/sgml/ref/pg_dump.sgml        |  2 +-
 doc/src/sgml/ref/pg_restore.sgml     | 25 ++++++++++++++++++++++---
 src/bin/pg_dump/pg_backup_archiver.c |  6 +++++-
 3 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/doc/src/sgml/ref/pg_dump.sgml b/doc/src/sgml/ref/pg_dump.sgml
index a6e7b08..7f7da9e 100644
--- a/doc/src/sgml/ref/pg_dump.sgml
+++ b/doc/src/sgml/ref/pg_dump.sgml
@@ -501,7 +501,7 @@ PostgreSQL documentation
       <term><option>--table=<replaceable class="parameter">table</replaceable></option></term>
       <listitem>
        <para>
-        Dump only tables (or views or sequences or foreign tables) matching
+        Dump only tables (or views, sequences, foreign tables or materialized views) matching
         <replaceable class="parameter">table</replaceable>.  Multiple tables
         can be selected by writing multiple <option>-t</> switches.  Also, the
         <replaceable class="parameter">table</replaceable> parameter is
diff --git a/doc/src/sgml/ref/pg_restore.sgml b/doc/src/sgml/ref/pg_restore.sgml
index 9f8dc00..9119e3e 100644
--- a/doc/src/sgml/ref/pg_restore.sgml
+++ b/doc/src/sgml/ref/pg_restore.sgml
@@ -405,9 +405,28 @@
       <term><option>--table=<replaceable class="parameter">table</replaceable></option></term>
       <listitem>
        <para>
-        Restore definition and/or data of named table only. Multiple tables
-        may be specified with multiple <option>-t</> switches. This can be
-        combined with the <option>-n</option> option to specify a schema.
+        Restore definition and/or data of the named table (or other relation)
+        only. This flag matches views, materialized views and foreign tables as
+        well as ordinary tables. Multiple relations may be specified with
+        multiple <option>-t</> switches. This can be combined with the
+        <option>-n</option> option to specify a schema.
+        <note>
+         <para>
+          When <literal>-t</literal> is specified,
+          <application>pg_restore</application> makes no attempt to restore any
+          other database objects that the selected table(s) might depend upon.
+          Therefore, there is no guarantee that the results of a specific-table
+          restore into a clean database will succeed.
+         </para>
+        </note>
+        <note>
+         <para>
+          This flag is not entirely compatible with versions prior to
+          PostgreSQL 9.5. In 9.4 and below this flag matched only tables. It
+          also behaves differently to the flag with the same name in
+          <application>pg_dump</application>.
+         </para>
+        </note>
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/bin/pg_dump/pg_backup_archiver.c b/src/bin/pg_dump/pg_backup_archiver.c
index ca427de..0f34ac7 100644
--- a/src/bin/pg_dump/pg_backup_archiver.c
+++ b/src/bin/pg_dump/pg_backup_archiver.c
@@ -2663,7 +2663,11 @@ _tocEntryRequired(TocEntry *te, teSection curSection, RestoreOptions *ropt)
 	if (ropt->selTypes)
 	{
 		if (strcmp(te->desc, "TABLE") == 0 ||
-			strcmp(te->desc, "TABLE DATA") == 0)
+			strcmp(te->desc, "TABLE DATA") == 0 ||
+			strcmp(te->desc, "VIEW") == 0 ||
+			strcmp(te->desc, "FOREIGN TABLE") == 0 ||
+			strcmp(te->desc, "MATERIALIZED VIEW") == 0 ||
+			strcmp(te->desc, "MATERIALIZED VIEW DATA") == 0)
 		{
 			if (!ropt->selTable)
 				return 0;
-- 
2.1.0

