Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit38d4ce6

Browse files
committed
Flesh out the background worker documentation.
Make it more clear that bgw_main is usually not what you want. Put thebackground worker flags in a variablelist rather than having them aspart of a paragraph. Explain important limits on how bgw_main_arg canbe used.Craig Ringer, substantially revised by me.
1 parentc7f0b28 commit38d4ce6

File tree

1 file changed

+72
-26
lines changed

1 file changed

+72
-26
lines changed

‎doc/src/sgml/bgworker.sgml

Lines changed: 72 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,39 @@ typedef struct BackgroundWorker
7070

7171
<para>
7272
<structfield>bgw_flags</> is a bitwise-or'd bit mask indicating the
73-
capabilities that the module wants. Possible values are
74-
<literal>BGWORKER_SHMEM_ACCESS</literal> (requesting shared memory access)
75-
and <literal>BGWORKER_BACKEND_DATABASE_CONNECTION</literal> (requesting the
76-
ability to establish a database connection, through which it can later run
77-
transactions and queries). A background worker using
78-
<literal>BGWORKER_BACKEND_DATABASE_CONNECTION</literal> to connect to
79-
a database must also attach shared memory using
80-
<literal>BGWORKER_SHMEM_ACCESS</literal>, or worker start-up will fail.
73+
capabilities that the module wants. Possible values are:
74+
<variablelist>
75+
76+
<varlistentry>
77+
<term><literal>BGWORKER_SHMEM_ACCESS</literal></term>
78+
<listitem>
79+
<para>
80+
<indexterm><primary>BGWORKER_SHMEM_ACCESS</primary></indexterm>
81+
Requests shared memory access. Workers without shared memory access
82+
cannot access any of <productname>PostgreSQL's</productname> shared
83+
data structures, such as heavyweight or lightweight locks, shared
84+
buffers, or any custom data structures which the worker itself may
85+
wish to create and use.
86+
</para>
87+
</listitem>
88+
</varlistentry>
89+
90+
<varlistentry>
91+
<term><literal>BGWORKER_BACKEND_DATABASE_CONNECTION</literal></term>
92+
<listitem>
93+
<para>
94+
<indexterm><primary>BGWORKER_BACKEND_DATABASE_CONNECTION</primary></indexterm>
95+
Requests the ability to establish a database connection through which it
96+
can later run transactions and queries. A background worker using
97+
<literal>BGWORKER_BACKEND_DATABASE_CONNECTION</literal> to connect to a
98+
database must also attach shared memory using
99+
<literal>BGWORKER_SHMEM_ACCESS</literal>, or worker start-up will fail.
100+
</para>
101+
</listitem>
102+
</varlistentry>
103+
104+
</variablelist>
105+
81106
</para>
82107

83108
<para>
@@ -106,34 +131,55 @@ typedef struct BackgroundWorker
106131

107132
<para>
108133
<structfield>bgw_main</structfield> is a pointer to the function to run when
109-
the process is started. This function must take a single argument of type
110-
<type>Datum</> and return <type>void</>.
111-
<structfield>bgw_main_arg</structfield> will be passed to it as its only
112-
argument. Note that the global variable <literal>MyBgworkerEntry</literal>
113-
points to a copy of the <structname>BackgroundWorker</structname> structure
114-
passed at registration time. <structfield>bgw_main</structfield> may be
115-
NULL; in that case, <structfield>bgw_library_name</structfield> and
116-
<structfield>bgw_function_name</structfield> will be used to determine
117-
the entry point. This is useful for background workers launched after
118-
postmaster startup, where the postmaster does not have the requisite
119-
library loaded.
134+
the process is started. This field can only safely be used to launch
135+
functions within the core server, because shared libraries may be loaded
136+
at different starting addresses in different backend processes. This will
137+
happen on all platforms when the library is loaded using any mechanism
138+
other than <xref linkend="guc-shared-preload-libraries">. Even when that
139+
mechanism is used, address space layout variations will still occur on
140+
Windows, and when <literal>EXEC_BACKEND</> is used. Therefore, most users
141+
of this API should set this field to NULL. If it is non-NULL, it takes
142+
precedence over <structfield>bgw_library_name</> and
143+
<structfield>bgw_function_name</>.
120144
</para>
121145

122146
<para>
123147
<structfield>bgw_library_name</structfield> is the name of a library in
124148
which the initial entry point for the background worker should be sought.
125-
It is ignored unless <structfield>bgw_main</structfield> is NULL.
126-
But if <structfield>bgw_main</structfield> is NULL, then the named library
127-
will be dynamically loaded by the worker process and
128-
<structfield>bgw_function_name</structfield> will be used to identify
129-
the function to be called.
149+
The named library will be dynamically loaded by the worker process and
150+
<structfield>bgw_function_name</structfield> will be used to identify the
151+
function to be called. If loading a function from the core code,
152+
<structfield>bgw_main</> should be set instead.
130153
</para>
131154

132155
<para>
133156
<structfield>bgw_function_name</structfield> is the name of a function in
134157
a dynamically loaded library which should be used as the initial entry point
135-
for a new background worker. It is ignored unless
136-
<structfield>bgw_main</structfield> is NULL.
158+
for a new background worker.
159+
</para>
160+
161+
<para>
162+
<structfield>bgw_main_arg</structfield> is the <type>Datum</> argument
163+
to the background worker main function. Regardless of whether that
164+
function is specified via <structfield>bgw_main</> or via the combination
165+
of <function>bgw_library_name</> and <function>bgw_function_name</>,
166+
this main function should take a single argument of type <type>Datum</>
167+
and return <type>void</>. <structfield>bgw_main_arg</structfield> will be
168+
passed as the argument. In addition, the global variable
169+
<literal>MyBgworkerEntry</literal>
170+
points to a copy of the <structname>BackgroundWorker</structname> structure
171+
passed at registration time; the worker may find it helpful to examine
172+
this structure.
173+
</para>
174+
175+
<para>
176+
On Windows (and anywhere else where <literal>EXEC_BACKEND</literal> is
177+
defined) or in dynamic background workers it is not safe to pass a
178+
<type>Datum</> by reference, only by value. If an argument is required, it
179+
is safest to pass an int32 or other small value and use that as an index
180+
into an array allocated in shared memory. If a value like a <type>cstring</>
181+
or <type>text</type> is passed then the pointer won't be valid from the
182+
new background worker process.
137183
</para>
138184

139185
<para>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp