1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/perform.sgml,v 1.63 2007/02/01 19:10:24 momjian Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/perform.sgml,v 1.64 2007/03/29 00:15:36 tgl Exp $ -->
2
2
3
3
<chapter id="performance-tips">
4
4
<title>Performance Tips</title>
@@ -801,7 +801,7 @@ SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
801
801
<command>EXECUTE</command> as many times as required. This avoids
802
802
some of the overhead of repeatedly parsing and planning
803
803
<command>INSERT</command>. Different interfaces provide this facility
804
- in different ways; look for Prepared Statements in the interface
804
+ in different ways; look for <quote>prepared statements</> in the interface
805
805
documentation.
806
806
</para>
807
807
@@ -815,14 +815,12 @@ SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
815
815
<para>
816
816
<command>COPY</command> is fastest when used within the same
817
817
transaction as an earlier <command>CREATE TABLE</command> or
818
- <command>TRUNCATE</command> command. In those cases, no WAL
819
- needs to be written because in case of an error, the files
820
- containing the newly loaded data will be removed automatically.
821
- <command>CREATE TABLE AS SELECT</command> is also optimized
822
- to avoid writing WAL. <command>COPY</command> and
823
- <command>CREATE TABLE AS SELECT</command> will write WAL
824
- when <xref linkend="guc-archive-command"> is set and will not
825
- therefore be optimized in that case.
818
+ <command>TRUNCATE</command> command. In such cases no WAL
819
+ needs to be written, because in case of an error, the files
820
+ containing the newly loaded data will be removed anyway.
821
+ However, this consideration does not apply when
822
+ <xref linkend="guc-archive-command"> is set, as all commands
823
+ must write WAL in that case.
826
824
</para>
827
825
828
826
</sect2>
@@ -897,23 +895,51 @@ SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
897
895
<title>Turn off <varname>archive_command</varname></title>
898
896
899
897
<para>
900
- When loading large amounts of data you might want to unset the
901
- <xref linkend="guc-archive-command"> before loading. It might be
902
- faster to take a new base backup once the load has completed
903
- than to allow a large archive to accumulate.
898
+ When loading large amounts of data into an installation that uses
899
+ WAL archiving, you might want to disable archiving (unset the
900
+ <xref linkend="guc-archive-command"> configuration variable)
901
+ while loading. It might be
902
+ faster to take a new base backup after the load has completed
903
+ than to process a large amount of incremental WAL data.
904
904
</para>
905
905
906
906
<para>
907
- This is particularly important advice because certain commands
908
- will perform more slowly when <varname>archive_command</varname>
909
- is set, as a result of their needing to write large amounts of WAL.
907
+ Aside from avoiding the time for the archiver to process the WAL data,
908
+ doing this will actually make certain commands faster, because they
909
+ are designed not to write WAL at all if <varname>archive_command</varname>
910
+ is unset. (They can guarantee crash safety more cheaply by doing an
911
+ <function>fsync</> at the end than by writing WAL.)
910
912
This applies to the following commands:
911
- <command>CREATE TABLE AS SELECT</command>,
912
- <command>CREATE INDEX</command> and also <command>COPY</command>, when
913
- it is executed in the same transaction as a prior
914
- <command>CREATE TABLE</command> or <command>TRUNCATE</command> command.
913
+ <itemizedlist>
914
+ <listitem>
915
+ <para>
916
+ <command>CREATE TABLE AS SELECT</command>
917
+ </para>
918
+ </listitem>
919
+ <listitem>
920
+ <para>
921
+ <command>CREATE INDEX</command> (and variants such as
922
+ <command>ALTER TABLE ADD PRIMARY KEY</command>)
923
+ </para>
924
+ </listitem>
925
+ <listitem>
926
+ <para>
927
+ <command>ALTER TABLE SET TABLESPACE</command>
928
+ </para>
929
+ </listitem>
930
+ <listitem>
931
+ <para>
932
+ <command>CLUSTER</command>
933
+ </para>
934
+ </listitem>
935
+ <listitem>
936
+ <para>
937
+ <command>COPY FROM</command>, when the target table has been
938
+ created or truncated earlier in the same transaction
939
+ </para>
940
+ </listitem>
941
+ </itemizedlist>
915
942
</para>
916
-
917
943
</sect2>
918
944
919
945
<sect2 id="populate-analyze">
@@ -950,15 +976,43 @@ SELECT * FROM x, y, a, b, c WHERE something AND somethingelse;
950
976
By default, <application>pg_dump</> uses <command>COPY</>, and when
951
977
it is generating a complete schema-and-data dump, it is careful to
952
978
load data before creating indexes and foreign keys. So in this case
953
- the first several guidelines are handled automatically. What is left
954
- for you to do is to set appropriate (i.e., larger than normal) values
955
- for <varname>maintenance_work_mem</varname> and
956
- <varname>checkpoint_segments</varname>, as well as unsetting
957
- <varname>archive_command</varname> before loading the dump script,
958
- and then to run <command>ANALYZE</> afterwards and resetting
959
- <varname>archive_command</varname> if required. All of the
960
- parameters can be reset once the load has completed without needing
961
- to restart the server, as described in <xref linkend="config-setting">.
979
+ several guidelines are handled automatically. What is left
980
+ for you to do is to:
981
+ <itemizedlist>
982
+ <listitem>
983
+ <para>
984
+ Set appropriate (i.e., larger than normal) values for
985
+ <varname>maintenance_work_mem</varname> and
986
+ <varname>checkpoint_segments</varname>.
987
+ </para>
988
+ </listitem>
989
+ <listitem>
990
+ <para>
991
+ If using WAL archiving, consider disabling it during the restore.
992
+ To do that, unset <varname>archive_command</varname> before loading the
993
+ dump script, and afterwards restore <varname>archive_command</varname>
994
+ and take a fresh base backup.
995
+ </para>
996
+ </listitem>
997
+ <listitem>
998
+ <para>
999
+ Consider whether the whole dump should be restored as a single
1000
+ transaction. To do that, pass the <option>-1</> or
1001
+ <option>--single-transaction</> command-line option to
1002
+ <application>psql</> or <application>pg_restore</>. When using this
1003
+ mode, even the smallest of errors will rollback the entire restore,
1004
+ possibly discarding many hours of processing. Depending on how
1005
+ interrelated the data is, that might seem preferable to manual cleanup,
1006
+ or not. <command>COPY</> commands will run fastest if you use a single
1007
+ transaction and have WAL archiving turned off.
1008
+ </para>
1009
+ </listitem>
1010
+ <listitem>
1011
+ <para>
1012
+ Run <command>ANALYZE</> afterwards.
1013
+ </para>
1014
+ </listitem>
1015
+ </itemizedlist>
962
1016
</para>
963
1017
964
1018
<para>
0 commit comments