Skip to content

Commit 317632f

Browse files
committed
Move InRecovery and standbyState global vars to xlogutils.c.
They are used in code that runs both during normal operation and during WAL replay, and needs to behave differently during replay. Move them to xlogutils.c, because that's where we have other helper functions used by redo routines. Reviewed-by: Andres Freund Discussion: https://www.postgresql.org/message-id/b3b71061-4919-e882-4857-27e370ab134a%40iki.fi
1 parent 4fe8dcd commit 317632f

File tree

17 files changed

+75
-67
lines changed

17 files changed

+75
-67
lines changed

src/backend/access/heap/visibilitymap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888

8989
#include "access/heapam_xlog.h"
9090
#include "access/visibilitymap.h"
91-
#include "access/xlog.h"
91+
#include "access/xlogutils.h"
9292
#include "miscadmin.h"
9393
#include "port/pg_bitutils.h"
9494
#include "storage/bufmgr.h"

src/backend/access/transam/commit_ts.c

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "access/htup_details.h"
2929
#include "access/slru.h"
3030
#include "access/transam.h"
31+
#include "access/xlogutils.h"
3132
#include "catalog/pg_type.h"
3233
#include "funcapi.h"
3334
#include "miscadmin.h"

src/backend/access/transam/multixact.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
#include "access/twophase.h"
7575
#include "access/twophase_rmgr.h"
7676
#include "access/xact.h"
77-
#include "access/xlog.h"
7877
#include "access/xloginsert.h"
78+
#include "access/xlogutils.h"
7979
#include "catalog/pg_type.h"
8080
#include "commands/dbcommands.h"
8181
#include "funcapi.h"

src/backend/access/transam/slru.c

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include "access/slru.h"
5555
#include "access/transam.h"
5656
#include "access/xlog.h"
57+
#include "access/xlogutils.h"
5758
#include "miscadmin.h"
5859
#include "pgstat.h"
5960
#include "storage/fd.h"

src/backend/access/transam/varsup.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "access/subtrans.h"
1919
#include "access/transam.h"
2020
#include "access/xact.h"
21-
#include "access/xlog.h"
21+
#include "access/xlogutils.h"
2222
#include "commands/dbcommands.h"
2323
#include "miscadmin.h"
2424
#include "postmaster/autovacuum.h"

src/backend/access/transam/xlog.c

-16
Original file line numberDiff line numberDiff line change
@@ -193,22 +193,6 @@ CheckpointStatsData CheckpointStats;
193193
*/
194194
TimeLineID ThisTimeLineID = 0;
195195

196-
/*
197-
* Are we doing recovery from XLOG?
198-
*
199-
* This is only ever true in the startup process; it should be read as meaning
200-
* "this process is replaying WAL records", rather than "the system is in
201-
* recovery mode". It should be examined primarily by functions that need
202-
* to act differently when called from a WAL redo function (e.g., to skip WAL
203-
* logging). To check whether the system is in recovery regardless of which
204-
* process you're running in, use RecoveryInProgress() but only after shared
205-
* memory startup and lock initialization.
206-
*/
207-
bool InRecovery = false;
208-
209-
/* Are we in Hot Standby mode? Only valid in startup process, see xlog.h */
210-
HotStandbyState standbyState = STANDBY_DISABLED;
211-
212196
static XLogRecPtr LastRec;
213197

214198
/* Local copy of WalRcv->flushedUpto */

src/backend/access/transam/xlogutils.c

+20
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "access/xlogutils.h"
2626
#include "miscadmin.h"
2727
#include "pgstat.h"
28+
#include "storage/fd.h"
2829
#include "storage/smgr.h"
2930
#include "utils/guc.h"
3031
#include "utils/hsearch.h"
@@ -34,6 +35,25 @@
3435
/* GUC variable */
3536
bool ignore_invalid_pages = false;
3637

38+
/*
39+
* Are we doing recovery from XLOG?
40+
*
41+
* This is only ever true in the startup process; it should be read as meaning
42+
* "this process is replaying WAL records", rather than "the system is in
43+
* recovery mode". It should be examined primarily by functions that need
44+
* to act differently when called from a WAL redo function (e.g., to skip WAL
45+
* logging). To check whether the system is in recovery regardless of which
46+
* process you're running in, use RecoveryInProgress() but only after shared
47+
* memory startup and lock initialization.
48+
*
49+
* This is updated from xlog.c, but lives here because it's mostly read by
50+
* WAL redo functions.
51+
*/
52+
bool InRecovery = false;
53+
54+
/* Are we in Hot Standby mode? Only valid in startup process, see xlogutils.h */
55+
HotStandbyState standbyState = STANDBY_DISABLED;
56+
3757
/*
3858
* During XLOG replay, we may see XLOG records for incremental updates of
3959
* pages that no longer exist, because their relation was later dropped or

src/backend/commands/tablespace.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
#include "access/sysattr.h"
5757
#include "access/tableam.h"
5858
#include "access/xact.h"
59-
#include "access/xlog.h"
6059
#include "access/xloginsert.h"
60+
#include "access/xlogutils.h"
6161
#include "catalog/catalog.h"
6262
#include "catalog/dependency.h"
6363
#include "catalog/indexing.h"

src/backend/postmaster/startup.c

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "postgres.h"
2121

2222
#include "access/xlog.h"
23+
#include "access/xlogutils.h"
2324
#include "libpq/pqsignal.h"
2425
#include "miscadmin.h"
2526
#include "pgstat.h"

src/backend/storage/buffer/bufmgr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include <unistd.h>
3535

3636
#include "access/tableam.h"
37-
#include "access/xlog.h"
37+
#include "access/xlogutils.h"
3838
#include "catalog/catalog.h"
3939
#include "catalog/storage.h"
4040
#include "executor/instrument.h"

src/backend/storage/ipc/procarray.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#include "access/transam.h"
5353
#include "access/twophase.h"
5454
#include "access/xact.h"
55-
#include "access/xlog.h"
55+
#include "access/xlogutils.h"
5656
#include "catalog/catalog.h"
5757
#include "catalog/pg_authid.h"
5858
#include "commands/dbcommands.h"

src/backend/storage/ipc/standby.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
#include "access/transam.h"
2020
#include "access/twophase.h"
2121
#include "access/xact.h"
22-
#include "access/xlog.h"
2322
#include "access/xloginsert.h"
23+
#include "access/xlogutils.h"
2424
#include "miscadmin.h"
2525
#include "pgstat.h"
2626
#include "storage/bufmgr.h"

src/backend/storage/lmgr/lock.c

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "access/twophase_rmgr.h"
3838
#include "access/xact.h"
3939
#include "access/xlog.h"
40+
#include "access/xlogutils.h"
4041
#include "miscadmin.h"
4142
#include "pg_trace.h"
4243
#include "pgstat.h"

src/backend/storage/lmgr/proc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
#include "access/transam.h"
3939
#include "access/twophase.h"
40-
#include "access/xact.h"
40+
#include "access/xlogutils.h"
4141
#include "miscadmin.h"
4242
#include "pgstat.h"
4343
#include "postmaster/autovacuum.h"

src/backend/storage/smgr/smgr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*/
1818
#include "postgres.h"
1919

20-
#include "access/xlog.h"
20+
#include "access/xlogutils.h"
2121
#include "lib/ilist.h"
2222
#include "storage/bufmgr.h"
2323
#include "storage/ipc.h"

src/include/access/xlog.h

-42
Original file line numberDiff line numberDiff line change
@@ -31,48 +31,6 @@ extern int sync_method;
3131

3232
extern PGDLLIMPORT TimeLineID ThisTimeLineID; /* current TLI */
3333

34-
/*
35-
* Prior to 8.4, all activity during recovery was carried out by the startup
36-
* process. This local variable continues to be used in many parts of the
37-
* code to indicate actions taken by RecoveryManagers. Other processes that
38-
* potentially perform work during recovery should check RecoveryInProgress().
39-
* See XLogCtl notes in xlog.c.
40-
*/
41-
extern bool InRecovery;
42-
43-
/*
44-
* Like InRecovery, standbyState is only valid in the startup process.
45-
* In all other processes it will have the value STANDBY_DISABLED (so
46-
* InHotStandby will read as false).
47-
*
48-
* In DISABLED state, we're performing crash recovery or hot standby was
49-
* disabled in postgresql.conf.
50-
*
51-
* In INITIALIZED state, we've run InitRecoveryTransactionEnvironment, but
52-
* we haven't yet processed a RUNNING_XACTS or shutdown-checkpoint WAL record
53-
* to initialize our primary-transaction tracking system.
54-
*
55-
* When the transaction tracking is initialized, we enter the SNAPSHOT_PENDING
56-
* state. The tracked information might still be incomplete, so we can't allow
57-
* connections yet, but redo functions must update the in-memory state when
58-
* appropriate.
59-
*
60-
* In SNAPSHOT_READY mode, we have full knowledge of transactions that are
61-
* (or were) running on the primary at the current WAL location. Snapshots
62-
* can be taken, and read-only queries can be run.
63-
*/
64-
typedef enum
65-
{
66-
STANDBY_DISABLED,
67-
STANDBY_INITIALIZED,
68-
STANDBY_SNAPSHOT_PENDING,
69-
STANDBY_SNAPSHOT_READY
70-
} HotStandbyState;
71-
72-
extern HotStandbyState standbyState;
73-
74-
#define InHotStandby (standbyState >= STANDBY_SNAPSHOT_PENDING)
75-
7634
/*
7735
* Recovery target type.
7836
* Only set during a Point in Time recovery, not when in standby mode.

src/include/access/xlogutils.h

+42
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,48 @@
1414
#include "access/xlogreader.h"
1515
#include "storage/bufmgr.h"
1616

17+
/*
18+
* Prior to 8.4, all activity during recovery was carried out by the startup
19+
* process. This local variable continues to be used in many parts of the
20+
* code to indicate actions taken by RecoveryManagers. Other processes that
21+
* potentially perform work during recovery should check RecoveryInProgress().
22+
* See XLogCtl notes in xlog.c.
23+
*/
24+
extern bool InRecovery;
25+
26+
/*
27+
* Like InRecovery, standbyState is only valid in the startup process.
28+
* In all other processes it will have the value STANDBY_DISABLED (so
29+
* InHotStandby will read as false).
30+
*
31+
* In DISABLED state, we're performing crash recovery or hot standby was
32+
* disabled in postgresql.conf.
33+
*
34+
* In INITIALIZED state, we've run InitRecoveryTransactionEnvironment, but
35+
* we haven't yet processed a RUNNING_XACTS or shutdown-checkpoint WAL record
36+
* to initialize our primary-transaction tracking system.
37+
*
38+
* When the transaction tracking is initialized, we enter the SNAPSHOT_PENDING
39+
* state. The tracked information might still be incomplete, so we can't allow
40+
* connections yet, but redo functions must update the in-memory state when
41+
* appropriate.
42+
*
43+
* In SNAPSHOT_READY mode, we have full knowledge of transactions that are
44+
* (or were) running on the primary at the current WAL location. Snapshots
45+
* can be taken, and read-only queries can be run.
46+
*/
47+
typedef enum
48+
{
49+
STANDBY_DISABLED,
50+
STANDBY_INITIALIZED,
51+
STANDBY_SNAPSHOT_PENDING,
52+
STANDBY_SNAPSHOT_READY
53+
} HotStandbyState;
54+
55+
extern HotStandbyState standbyState;
56+
57+
#define InHotStandby (standbyState >= STANDBY_SNAPSHOT_PENDING)
58+
1759

1860
extern bool XLogHaveInvalidPages(void);
1961
extern void XLogCheckInvalidPages(void);

0 commit comments

Comments
 (0)