summaryrefslogtreecommitdiffstats
path: root/doc/src/sgml/archive-modules.sgml
blob: 7064307d9e6081475c39e9f2a295ff5e64cb2fbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!-- doc/src/sgml/archive-modules.sgml -->

<chapter id="archive-modules">
 <title>Archive Modules</title>
 <indexterm zone="archive-modules">
  <primary>Archive Modules</primary>
 </indexterm>

 <para>
  PostgreSQL provides infrastructure to create custom modules for continuous
  archiving (see <xref linkend="continuous-archiving"/>).  While archiving via
  a shell command (i.e., <xref linkend="guc-archive-command"/>) is much
  simpler, a custom archive module will often be considerably more robust and
  performant.
 </para>

 <para>
  When a custom <xref linkend="guc-archive-library"/> is configured, PostgreSQL
  will submit completed WAL files to the module, and the server will avoid
  recycling or removing these WAL files until the module indicates that the files
  were successfully archived.  It is ultimately up to the module to decide what
  to do with each WAL file, but many recommendations are listed at
  <xref linkend="backup-archiving-wal"/>.
 </para>

 <para>
  Archiving modules must at least consist of an initialization function (see
  <xref linkend="archive-module-init"/>) and the required callbacks (see
  <xref linkend="archive-module-callbacks"/>).  However, archive modules are
  also permitted to do much more (e.g., declare GUCs and register background
  workers).
 </para>

 <para>
  The <filename>contrib/basic_archive</filename> module contains a working
  example, which demonstrates some useful techniques.
 </para>

 <sect1 id="archive-module-init">
  <title>Initialization Functions</title>
  <indexterm zone="archive-module-init">
   <primary>_PG_archive_module_init</primary>
  </indexterm>
  <para>
   An archive library is loaded by dynamically loading a shared library with the
   <xref linkend="guc-archive-library"/>'s name as the library base name.  The
   normal library search path is used to locate the library.  To provide the
   required archive module callbacks and to indicate that the library is
   actually an archive module, it needs to provide a function named
   <function>_PG_archive_module_init</function>.  The result of the function
   must be a pointer to a struct of type
   <structname>ArchiveModuleCallbacks</structname>, which contains everything
   that the core code needs to know to make use of the archive module.  The
   return value needs to be of server lifetime, which is typically achieved by
   defining it as a <literal>static const</literal> variable in global scope.

<programlisting>
typedef struct ArchiveModuleCallbacks
{
    ArchiveStartupCB startup_cb;
    ArchiveCheckConfiguredCB check_configured_cb;
    ArchiveFileCB archive_file_cb;
    ArchiveShutdownCB shutdown_cb;
} ArchiveModuleCallbacks;
typedef const ArchiveModuleCallbacks *(*ArchiveModuleInit) (void);
</programlisting>

   Only the <function>archive_file_cb</function> callback is required.  The
   others are optional.
  </para>
 </sect1>

 <sect1 id="archive-module-callbacks">
  <title>Archive Module Callbacks</title>
  <para>
   The archive callbacks define the actual archiving behavior of the module.
   The server will call them as required to process each individual WAL file.
  </para>

  <sect2 id="archive-module-startup">
   <title>Startup Callback</title>
   <para>
    The <function>startup_cb</function> callback is called shortly after the
    module is loaded.  This callback can be used to perform any additional
    initialization required.  If the archive module has any state, it can use
    <structfield>state->private_data</structfield> to store it.

<programlisting>
typedef void (*ArchiveStartupCB) (ArchiveModuleState *state);
</programlisting>
   </para>
  </sect2>

  <sect2 id="archive-module-check">
   <title>Check Callback</title>
   <para>
    The <function>check_configured_cb</function> callback is called to determine
    whether the module is fully configured and ready to accept WAL files (e.g.,
    its configuration parameters are set to valid values).  If no
    <function>check_configured_cb</function> is defined, the server always
    assumes the module is configured.

<programlisting>
typedef bool (*ArchiveCheckConfiguredCB) (ArchiveModuleState *state);
</programlisting>

    If <literal>true</literal> is returned, the server will proceed with
    archiving the file by calling the <function>archive_file_cb</function>
    callback.  If <literal>false</literal> is returned, archiving will not
    proceed, and the archiver will emit the following message to the server log:
<screen>
WARNING:  archive_mode enabled, yet archiving is not configured
</screen>
    In the latter case, the server will periodically call this function, and
    archiving will proceed only when it returns <literal>true</literal>.
   </para>
  </sect2>

  <sect2 id="archive-module-archive">
   <title>Archive Callback</title>
   <para>
    The <function>archive_file_cb</function> callback is called to archive a
    single WAL file.

<programlisting>
typedef bool (*ArchiveFileCB) (ArchiveModuleState *state, const char *file, const char *path);
</programlisting>

    If <literal>true</literal> is returned, the server proceeds as if the file
    was successfully archived, which may include recycling or removing the
    original WAL file.  If <literal>false</literal> is returned, the server will
    keep the original WAL file and retry archiving later.
    <replaceable>file</replaceable> will contain just the file name of the WAL
    file to archive, while <replaceable>path</replaceable> contains the full
    path of the WAL file (including the file name).
   </para>
  </sect2>

  <sect2 id="archive-module-shutdown">
   <title>Shutdown Callback</title>
   <para>
    The <function>shutdown_cb</function> callback is called when the archiver
    process exits (e.g., after an error) or the value of
    <xref linkend="guc-archive-library"/> changes.  If no
    <function>shutdown_cb</function> is defined, no special action is taken in
    these situations.  If the archive module has any state, this callback should
    free it to avoid leaks.

<programlisting>
typedef void (*ArchiveShutdownCB) (ArchiveModuleState *state);
</programlisting>
   </para>
  </sect2>
 </sect1>
</chapter>