|
| 1 | +<!-- doc/src/sgml/pgtransfer.sgml --> |
| 2 | + |
| 3 | +<sect1 id="pgtransfer" xreflabel="pg_transfer"> |
| 4 | + <title>pg_transfer</title> |
| 5 | + |
| 6 | + <indexterm zone="pgtransfer"> |
| 7 | + <primary>pg_transfer</primary> |
| 8 | + </indexterm> |
| 9 | + |
| 10 | + <para> |
| 11 | + The <filename>pg_transfer</filename> extension enables quick transfer of |
| 12 | + tables between <productname>&productname;</productname> instances. |
| 13 | + </para> |
| 14 | + |
| 15 | + <sect2> |
| 16 | + <title>Description</title> |
| 17 | + |
| 18 | + <para> |
| 19 | + Some applications experience problems loading huge amount of data |
| 20 | + into a database, for example when consolidating data from regional servers |
| 21 | + into a central one. Usual method is to copy schema and data by using |
| 22 | + <application>pg_dump</application> and <application>pg_restore</application>. |
| 23 | + In this case the receiving server's workload is higher then |
| 24 | + that of sending server. Data are loaded by <command>INSERT</command> or |
| 25 | + <command>COPY</command> commands, which creates significant impact |
| 26 | + on a disk subsystem. Creation of indexes and analysis of tables are |
| 27 | + performed in a target database after data are loaded. |
| 28 | + </para> |
| 29 | + |
| 30 | + <para> |
| 31 | + The <filename>pg_transfer</filename> extension allows to prepare a table |
| 32 | + (i. e. creation of indexes and analysis) out of target server and ensures fast load |
| 33 | + of read-only data. The extension contains additional functions for |
| 34 | + <application>pg_dump</application> and <application>pg_restore</application> utilities. |
| 35 | + </para> |
| 36 | + </sect2> |
| 37 | + |
| 38 | + <sect2> |
| 39 | + <title>Installation</title> |
| 40 | + |
| 41 | + <para>To install the extension execute the following SQL command: |
| 42 | + <programlisting> |
| 43 | + CREATE EXTENSION pg_transfer; |
| 44 | + </programlisting> |
| 45 | + </para> |
| 46 | + </sect2> |
| 47 | + |
| 48 | + <sect2> |
| 49 | + <title>Usage</title> |
| 50 | + |
| 51 | + <para> |
| 52 | + Before table can be transferred, it must be marked as read-only. |
| 53 | + |
| 54 | + <programlisting> |
| 55 | + ALTER TABLE <replaceable>table_name</replaceable> SET CONSTANT; |
| 56 | + </programlisting> |
| 57 | + </para> |
| 58 | + |
| 59 | + <para> |
| 60 | + After that <command>VACUUM (ANALYZE)</command> should be executed to get rid of |
| 61 | + dead tuples and refresh statistics. |
| 62 | + |
| 63 | + <programlisting> |
| 64 | + VACUUM (ANALYZE) <replaceable>table_name</replaceable>; |
| 65 | + </programlisting> |
| 66 | + </para> |
| 67 | + |
| 68 | + <para> |
| 69 | + Data transfer is performed in two stages. |
| 70 | + First, logical dump of data schema is taken on auxiliary database server |
| 71 | + and restored on target server. Second, data on auxiliary server are |
| 72 | + prepared for transfer, using some information about the restored schema, |
| 73 | + and the transfer itself is performed. |
| 74 | + </para> |
| 75 | + |
| 76 | + <para> |
| 77 | + When source and target databases are located in the same file system, |
| 78 | + <option>--copy-mode-transfer</option> option must be specified at least once |
| 79 | + (for either <application>pg_dump</application> or |
| 80 | + <application>pg_restore</application>) to get an independent copy of data. |
| 81 | + When restoring data on primary server, <option>--generate-wal</option> option |
| 82 | + must be specified for <application>pg_restore</application> for changes |
| 83 | + to be replicated to standby server. |
| 84 | + </para> |
| 85 | + |
| 86 | + <note> |
| 87 | + <para> |
| 88 | + Architecture of both servers and configuration of |
| 89 | + <productname>&productname;</productname> must guarantee binary-compatible |
| 90 | + file formats. Checks are performed during data restoring for |
| 91 | + coincidence of alignment, page and segment sizes etc. |
| 92 | + </para> |
| 93 | + </note> |
| 94 | + |
| 95 | + <sect3> |
| 96 | + <title>Stage 1</title> |
| 97 | + |
| 98 | + <para> |
| 99 | + <programlisting> |
| 100 | + pg_dump <replaceable>database</replaceable> -t <replaceable>table_name</replaceable> --schema-only -f <replaceable>transfer_dir</replaceable>/archive.out |
| 101 | + pg_restore -d <replaceable>database</replaceable> --schema-only <replaceable>transfer_dir</replaceable>/archive.out |
| 102 | + </programlisting> |
| 103 | + </para> |
| 104 | + |
| 105 | + <para> |
| 106 | + After the schema is restored, TOAST table's identifier must be determined. |
| 107 | + <programlisting> |
| 108 | + psql <replaceable>target_database</replaceable> -c select reltoastrelid from pg_class where relname='<replaceable>table_name</replaceable>' |
| 109 | + </programlisting> |
| 110 | + </para> |
| 111 | + </sect3> |
| 112 | + |
| 113 | + <sect3> |
| 114 | + <title>Stage 2</title> |
| 115 | + |
| 116 | + <para> |
| 117 | + The <filename>pg_transfer</filename> extension must be installed in both databases. |
| 118 | + </para> |
| 119 | + |
| 120 | + <para> |
| 121 | + Using the TOAST table's identifier from the previous stage (<replaceable>reltoastid</replaceable>), |
| 122 | + prepare the table for transfer and force data flush to disk. |
| 123 | + <programlisting> |
| 124 | + psql -d <replaceable>database</replaceable> -c select pg_transfer_freeze('<replaceable>table_name</replaceable>'::regclass::oid, <replaceable>reltoastrelid</replaceable>::oid); |
| 125 | + </programlisting> |
| 126 | + </para> |
| 127 | + |
| 128 | + </sect3> |
| 129 | + |
| 130 | + <sect3> |
| 131 | + <title>Data transfer</title> |
| 132 | + <para> |
| 133 | + Preparation is completed. Now the data can be transferred into separate directory using <application>pg_dump</application> utility. |
| 134 | + <programlisting> |
| 135 | + pg_dump <replaceable>database</replaceable> -Fc -t <replaceable>table_name</replaceable> --copy-mode-transfer --transfer-dir <replaceable>transfer_dir</replaceable>/ -f <replaceable>transfer_dir</replaceable>/archive.out |
| 136 | + </programlisting> |
| 137 | + </para> |
| 138 | + |
| 139 | + <para> |
| 140 | + And finally data can be restored in target database. |
| 141 | + <programlisting> |
| 142 | + pg_restore -d <replaceable>target_database</replaceable> --data-only --transfer-dir <replaceable>transfer_dir</replaceable>/ <replaceable>transfer_dir</replaceable>/archive.out |
| 143 | + </programlisting> |
| 144 | + </para> |
| 145 | + </sect3> |
| 146 | + </sect2> |
| 147 | + |
| 148 | + <sect2> |
| 149 | + <title>Compatibility</title> |
| 150 | + |
| 151 | + <para> |
| 152 | + The extension is compatible with <productname>Postgres Pro</productname> 9.6 |
| 153 | + or newer on Unix-like systems. |
| 154 | + </para> |
| 155 | + </sect2> |
| 156 | + |
| 157 | +</sect1> |