Skip to content

Commit be1e1a3

Browse files
author
Commitfest Bot
committed
[CF 51/5118] v5 - SendProcSignal(), SetLatch() → SendInterrupt()
This commit was automatically generated by a robot at cfbot.cputube.org. It is based on patches submitted to the PostgreSQL mailing lists and registered in the PostgreSQL Commitfest application. This branch will be overwritten each time a new patch version is posted to the email thread, and also periodically to check for bitrot caused by changes on the master branch. Commitfest entry: https://commitfest.postgresql.org/51/5118 Patch(es): https://www.postgresql.org/message-id/4ddab51d-e932-40c5-b6fa-18c52db0e082@iki.fi Author(s): Heikki Linnakangas, Thomas Munro
2 parents ebf2ab4 + cac1de6 commit be1e1a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1583
-1514
lines changed

contrib/pg_prewarm/autoprewarm.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
#include "storage/dsm.h"
3838
#include "storage/dsm_registry.h"
3939
#include "storage/fd.h"
40+
#include "storage/interrupt.h"
4041
#include "storage/ipc.h"
41-
#include "storage/latch.h"
4242
#include "storage/lwlock.h"
4343
#include "storage/procsignal.h"
4444
#include "storage/smgr.h"
@@ -216,10 +216,10 @@ autoprewarm_main(Datum main_arg)
216216
if (autoprewarm_interval <= 0)
217217
{
218218
/* We're only dumping at shutdown, so just wait forever. */
219-
(void) WaitLatch(MyLatch,
220-
WL_LATCH_SET | WL_EXIT_ON_PM_DEATH,
221-
-1L,
222-
PG_WAIT_EXTENSION);
219+
(void) WaitInterrupt(1 << INTERRUPT_GENERAL,
220+
WL_INTERRUPT | WL_EXIT_ON_PM_DEATH,
221+
-1L,
222+
PG_WAIT_EXTENSION);
223223
}
224224
else
225225
{
@@ -243,14 +243,14 @@ autoprewarm_main(Datum main_arg)
243243
}
244244

245245
/* Sleep until the next dump time. */
246-
(void) WaitLatch(MyLatch,
247-
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
248-
delay_in_ms,
249-
PG_WAIT_EXTENSION);
246+
(void) WaitInterrupt(1 << INTERRUPT_GENERAL,
247+
WL_INTERRUPT | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
248+
delay_in_ms,
249+
PG_WAIT_EXTENSION);
250250
}
251251

252252
/* Reset the latch, loop. */
253-
ResetLatch(MyLatch);
253+
ClearInterrupt(INTERRUPT_GENERAL);
254254
}
255255

256256
/*
@@ -817,9 +817,6 @@ apw_start_leader_worker(void)
817817
return;
818818
}
819819

820-
/* must set notify PID to wait for startup */
821-
worker.bgw_notify_pid = MyProcPid;
822-
823820
if (!RegisterDynamicBackgroundWorker(&worker, &handle))
824821
ereport(ERROR,
825822
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),
@@ -853,9 +850,6 @@ apw_start_database_worker(void)
853850
strcpy(worker.bgw_name, "autoprewarm worker");
854851
strcpy(worker.bgw_type, "autoprewarm worker");
855852

856-
/* must set notify PID to wait for shutdown */
857-
worker.bgw_notify_pid = MyProcPid;
858-
859853
if (!RegisterDynamicBackgroundWorker(&worker, &handle))
860854
ereport(ERROR,
861855
(errcode(ERRCODE_INSUFFICIENT_RESOURCES),

contrib/postgres_fdw/connection.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "miscadmin.h"
2727
#include "pgstat.h"
2828
#include "postgres_fdw.h"
29-
#include "storage/latch.h"
29+
#include "storage/interrupt.h"
3030
#include "utils/builtins.h"
3131
#include "utils/hsearch.h"
3232
#include "utils/inval.h"
@@ -740,8 +740,8 @@ do_sql_command_end(PGconn *conn, const char *sql, bool consume_input)
740740
/*
741741
* If requested, consume whatever data is available from the socket. (Note
742742
* that if all data is available, this allows pgfdw_get_result to call
743-
* PQgetResult without forcing the overhead of WaitLatchOrSocket, which
744-
* would be large compared to the overhead of PQconsumeInput.)
743+
* PQgetResult without forcing the overhead of WaitInterruptOrSocket,
744+
* which would be large compared to the overhead of PQconsumeInput.)
745745
*/
746746
if (consume_input && !PQconsumeInput(conn))
747747
pgfdw_report_error(ERROR, NULL, conn, false, sql);
@@ -1384,7 +1384,7 @@ pgfdw_cancel_query_end(PGconn *conn, TimestampTz endtime,
13841384
/*
13851385
* If requested, consume whatever data is available from the socket. (Note
13861386
* that if all data is available, this allows pgfdw_get_cleanup_result to
1387-
* call PQgetResult without forcing the overhead of WaitLatchOrSocket,
1387+
* call PQgetResult without forcing the overhead of WaitInterruptOrSocket,
13881388
* which would be large compared to the overhead of PQconsumeInput.)
13891389
*/
13901390
if (consume_input && !PQconsumeInput(conn))
@@ -1479,7 +1479,7 @@ pgfdw_exec_cleanup_query_end(PGconn *conn, const char *query,
14791479
/*
14801480
* If requested, consume whatever data is available from the socket. (Note
14811481
* that if all data is available, this allows pgfdw_get_cleanup_result to
1482-
* call PQgetResult without forcing the overhead of WaitLatchOrSocket,
1482+
* call PQgetResult without forcing the overhead of WaitInterruptOrSocket,
14831483
* which would be large compared to the overhead of PQconsumeInput.)
14841484
*/
14851485
if (consume_input && !PQconsumeInput(conn))
@@ -1592,12 +1592,12 @@ pgfdw_get_cleanup_result(PGconn *conn, TimestampTz endtime,
15921592
pgfdw_we_cleanup_result = WaitEventExtensionNew("PostgresFdwCleanupResult");
15931593

15941594
/* Sleep until there's something to do */
1595-
wc = WaitLatchOrSocket(MyLatch,
1596-
WL_LATCH_SET | WL_SOCKET_READABLE |
1597-
WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
1598-
PQsocket(conn),
1599-
cur_timeout, pgfdw_we_cleanup_result);
1600-
ResetLatch(MyLatch);
1595+
wc = WaitInterruptOrSocket(1 << INTERRUPT_GENERAL,
1596+
WL_INTERRUPT | WL_SOCKET_READABLE |
1597+
WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
1598+
PQsocket(conn),
1599+
cur_timeout, pgfdw_we_cleanup_result);
1600+
ClearInterrupt(INTERRUPT_GENERAL);
16011601

16021602
CHECK_FOR_INTERRUPTS();
16031603

contrib/postgres_fdw/postgres_fdw.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include "optimizer/tlist.h"
3939
#include "parser/parsetree.h"
4040
#include "postgres_fdw.h"
41-
#include "storage/latch.h"
41+
#include "storage/waiteventset.h"
4242
#include "utils/builtins.h"
4343
#include "utils/float.h"
4444
#include "utils/guc.h"
@@ -7354,7 +7354,7 @@ postgresForeignAsyncConfigureWait(AsyncRequest *areq)
73547354
Assert(pendingAreq == areq);
73557355

73567356
AddWaitEventToSet(set, WL_SOCKET_READABLE, PQsocket(fsstate->conn),
7357-
NULL, areq);
7357+
0, areq);
73587358
}
73597359

73607360
/*

doc/src/sgml/bgworker.sgml

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ typedef struct BackgroundWorker
6363
char bgw_function_name[BGW_MAXLEN];
6464
Datum bgw_main_arg;
6565
char bgw_extra[BGW_EXTRALEN];
66-
pid_t bgw_notify_pid;
66+
pid_t bgw_notify_pid; /* not used */
6767
} BackgroundWorker;
6868
</programlisting>
6969
</para>
@@ -108,6 +108,18 @@ typedef struct BackgroundWorker
108108
</listitem>
109109
</varlistentry>
110110

111+
<varlistentry>
112+
<term><literal>BGWORKER_NO_NOTIFY</literal></term>
113+
<listitem>
114+
<para>
115+
<indexterm><primary>BGWORKER_NO_NOTIFY</primary></indexterm>
116+
Normally, the backend that registers a dynamic worker will be notified
117+
with <literal>INTERRUPT_GENERAL</literal> when the workers state changes, which allows the
118+
caller to wait for the worker to start and shut down. That can be
119+
suppressed by setting this flag.
120+
</para>
121+
</listitem>
122+
</varlistentry>
111123
</variablelist>
112124

113125
</para>
@@ -181,12 +193,8 @@ typedef struct BackgroundWorker
181193
</para>
182194

183195
<para>
184-
<structfield>bgw_notify_pid</structfield> is the PID of a PostgreSQL
185-
backend process to which the postmaster should send <literal>SIGUSR1</literal>
186-
when the process is started or exits. It should be 0 for workers registered
187-
at postmaster startup time, or when the backend registering the worker does
188-
not wish to wait for the worker to start up. Otherwise, it should be
189-
initialized to <literal>MyProcPid</literal>.
196+
<structfield>bgw_notify_pid</structfield> is not used and may be removed
197+
in a future release.
190198
</para>
191199

192200
<para>Once running, the process can connect to a database by calling
@@ -227,7 +235,7 @@ typedef struct BackgroundWorker
227235
reinitializes the cluster due to a backend failure. Backends which need
228236
to suspend execution only temporarily should use an interruptible sleep
229237
rather than exiting; this can be achieved by calling
230-
<function>WaitLatch()</function>. Make sure the
238+
<function>WaitInterrupt()</function>. Make sure the
231239
<literal>WL_POSTMASTER_DEATH</literal> flag is set when calling that function, and
232240
verify the return code for a prompt exit in the emergency case that
233241
<command>postgres</command> itself has terminated.
@@ -258,10 +266,7 @@ typedef struct BackgroundWorker
258266

259267
<para>
260268
In some cases, a process which registers a background worker may wish to
261-
wait for the worker to start up. This can be accomplished by initializing
262-
<structfield>bgw_notify_pid</structfield> to <literal>MyProcPid</literal> and
263-
then passing the <type>BackgroundWorkerHandle *</type> obtained at
264-
registration time to
269+
wait for the worker to start up. This can be accomplished with the
265270
<function>WaitForBackgroundWorkerStartup(<parameter>BackgroundWorkerHandle
266271
*handle</parameter>, <parameter>pid_t *</parameter>)</function> function.
267272
This function will block until the postmaster has attempted to start the

doc/src/sgml/sources.sgml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -989,19 +989,19 @@ MemoryContextSwitchTo(MemoryContext context)
989989
call async-signal safe functions (as defined in POSIX) and access
990990
variables of type <literal>volatile sig_atomic_t</literal>. A few
991991
functions in <command>postgres</command> are also deemed signal safe, importantly
992-
<function>SetLatch()</function>.
992+
<function>RaiseInterrupt()</function>.
993993
</para>
994994
<para>
995995
In most cases signal handlers should do nothing more than note
996996
that a signal has arrived, and wake up code running outside of
997-
the handler using a latch. An example of such a handler is the
997+
the handler using RaiseInterrupt(). An example of such a handler is the
998998
following:
999999
<programlisting>
10001000
static void
10011001
handle_sighup(SIGNAL_ARGS)
10021002
{
10031003
got_SIGHUP = true;
1004-
SetLatch(MyLatch);
1004+
RaiseInterrupt(INTERRUPT_GENERAL);
10051005
}
10061006
</programlisting>
10071007
</para>

src/backend/access/heap/vacuumlazy.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "postmaster/autovacuum.h"
5454
#include "storage/bufmgr.h"
5555
#include "storage/freespace.h"
56+
#include "storage/interrupt.h"
5657
#include "storage/lmgr.h"
5758
#include "utils/lsyscache.h"
5859
#include "utils/pg_rusage.h"
@@ -2706,11 +2707,11 @@ lazy_truncate_heap(LVRelState *vacrel)
27062707
return;
27072708
}
27082709

2709-
(void) WaitLatch(MyLatch,
2710-
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
2711-
VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL,
2712-
WAIT_EVENT_VACUUM_TRUNCATE);
2713-
ResetLatch(MyLatch);
2710+
(void) WaitInterrupt(1 << INTERRUPT_GENERAL,
2711+
WL_INTERRUPT | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
2712+
VACUUM_TRUNCATE_LOCK_WAIT_INTERVAL,
2713+
WAIT_EVENT_VACUUM_TRUNCATE);
2714+
ClearInterrupt(INTERRUPT_GENERAL);
27142715
}
27152716

27162717
/*

src/backend/access/transam/README

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ commit to minimize the window in which the filesystem change has been made
794794
but the transaction isn't guaranteed committed.
795795

796796
The walwriter regularly wakes up (via wal_writer_delay) or is woken up
797-
(via its latch, which is set by backends committing asynchronously) and
797+
(via an interrupt, which is set by backends committing asynchronously) and
798798
performs an XLogBackgroundFlush(). This checks the location of the last
799799
completely filled WAL page. If that has moved forwards, then we write all
800800
the changed buffers up to that point, so that under full load we write

src/backend/access/transam/parallel.c

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "miscadmin.h"
3434
#include "optimizer/optimizer.h"
3535
#include "pgstat.h"
36+
#include "storage/interrupt.h"
3637
#include "storage/ipc.h"
3738
#include "storage/predicate.h"
3839
#include "storage/spin.h"
@@ -599,7 +600,6 @@ LaunchParallelWorkers(ParallelContext *pcxt)
599600
sprintf(worker.bgw_library_name, "postgres");
600601
sprintf(worker.bgw_function_name, "ParallelWorkerMain");
601602
worker.bgw_main_arg = UInt32GetDatum(dsm_segment_handle(pcxt->seg));
602-
worker.bgw_notify_pid = MyProcPid;
603603

604604
/*
605605
* Start workers.
@@ -755,16 +755,16 @@ WaitForParallelWorkersToAttach(ParallelContext *pcxt)
755755
{
756756
/*
757757
* Worker not yet started, so we must wait. The postmaster
758-
* will notify us if the worker's state changes. Our latch
759-
* might also get set for some other reason, but if so we'll
760-
* just end up waiting for the same worker again.
758+
* will notify us if the worker's state changes. The
759+
* interrupt might also get set for some other reason, but if
760+
* so we'll just end up waiting for the same worker again.
761761
*/
762-
rc = WaitLatch(MyLatch,
763-
WL_LATCH_SET | WL_EXIT_ON_PM_DEATH,
764-
-1, WAIT_EVENT_BGWORKER_STARTUP);
762+
rc = WaitInterrupt(1 << INTERRUPT_GENERAL,
763+
WL_INTERRUPT | WL_EXIT_ON_PM_DEATH,
764+
-1, WAIT_EVENT_BGWORKER_STARTUP);
765765

766-
if (rc & WL_LATCH_SET)
767-
ResetLatch(MyLatch);
766+
if (rc & WL_INTERRUPT)
767+
ClearInterrupt(INTERRUPT_GENERAL);
768768
}
769769
}
770770

@@ -873,15 +873,16 @@ WaitForParallelWorkersToFinish(ParallelContext *pcxt)
873873
* the worker writes messages and terminates after the
874874
* CHECK_FOR_INTERRUPTS() near the top of this function and
875875
* before the call to GetBackgroundWorkerPid(). In that case,
876-
* or latch should have been set as well and the right things
877-
* will happen on the next pass through the loop.
876+
* INTERRUPT_GENERAL should have been set as well and the
877+
* right things will happen on the next pass through the loop.
878878
*/
879879
}
880880
}
881881

882-
(void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, -1,
883-
WAIT_EVENT_PARALLEL_FINISH);
884-
ResetLatch(MyLatch);
882+
(void) WaitInterrupt(1 << INTERRUPT_GENERAL,
883+
WL_INTERRUPT | WL_EXIT_ON_PM_DEATH, -1,
884+
WAIT_EVENT_PARALLEL_FINISH);
885+
ClearInterrupt(INTERRUPT_GENERAL);
885886
}
886887

887888
if (pcxt->toc != NULL)
@@ -1034,7 +1035,7 @@ HandleParallelMessageInterrupt(void)
10341035
{
10351036
InterruptPending = true;
10361037
ParallelMessagePending = true;
1037-
SetLatch(MyLatch);
1038+
RaiseInterrupt(INTERRUPT_GENERAL);
10381039
}
10391040

10401041
/*

src/backend/access/transam/xlog.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,9 @@
8484
#include "replication/walsender.h"
8585
#include "storage/bufmgr.h"
8686
#include "storage/fd.h"
87+
#include "storage/interrupt.h"
8788
#include "storage/ipc.h"
8889
#include "storage/large_object.h"
89-
#include "storage/latch.h"
9090
#include "storage/predicate.h"
9191
#include "storage/proc.h"
9292
#include "storage/procarray.h"
@@ -2676,7 +2676,7 @@ XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
26762676
ProcNumber walwriterProc = procglobal->walwriterProc;
26772677

26782678
if (walwriterProc != INVALID_PROC_NUMBER)
2679-
SetLatch(&GetPGProcByNumber(walwriterProc)->procLatch);
2679+
SendInterrupt(INTERRUPT_GENERAL, ProcGlobal->walwriterProc);
26802680
}
26812681
}
26822682

@@ -9347,11 +9347,11 @@ do_pg_backup_stop(BackupState *state, bool waitforarchive)
93479347
reported_waiting = true;
93489348
}
93499349

9350-
(void) WaitLatch(MyLatch,
9351-
WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
9352-
1000L,
9353-
WAIT_EVENT_BACKUP_WAIT_WAL_ARCHIVE);
9354-
ResetLatch(MyLatch);
9350+
(void) WaitInterrupt(1 << INTERRUPT_GENERAL,
9351+
WL_INTERRUPT | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
9352+
1000L,
9353+
WAIT_EVENT_BACKUP_WAIT_WAL_ARCHIVE);
9354+
ClearInterrupt(INTERRUPT_GENERAL);
93559355

93569356
if (++waits >= seconds_before_warning)
93579357
{

0 commit comments

Comments
 (0)