Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * proc.c
4 : : * routines to manage per-process shared memory data structure
5 : : *
6 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
7 : : * Portions Copyright (c) 1994, Regents of the University of California
8 : : *
9 : : *
10 : : * IDENTIFICATION
11 : : * src/backend/storage/lmgr/proc.c
12 : : *
13 : : *-------------------------------------------------------------------------
14 : : */
15 : : /*
16 : : * Interface (a):
17 : : * JoinWaitQueue(), ProcSleep(), ProcWakeup()
18 : : *
19 : : * Waiting for a lock causes the backend to be put to sleep. Whoever releases
20 : : * the lock wakes the process up again (and gives it an error code so it knows
21 : : * whether it was awoken on an error condition).
22 : : *
23 : : * Interface (b):
24 : : *
25 : : * ProcReleaseLocks -- frees the locks associated with current transaction
26 : : *
27 : : * ProcKill -- destroys the shared memory state (and locks)
28 : : * associated with the process.
29 : : */
30 : : #include "postgres.h"
31 : :
32 : : #include <signal.h>
33 : : #include <unistd.h>
34 : : #include <sys/time.h>
35 : :
36 : : #include "access/transam.h"
37 : : #include "access/twophase.h"
38 : : #include "access/xlogutils.h"
39 : : #include "access/xlogwait.h"
40 : : #include "miscadmin.h"
41 : : #include "pgstat.h"
42 : : #include "postmaster/autovacuum.h"
43 : : #include "replication/slotsync.h"
44 : : #include "replication/syncrep.h"
45 : : #include "storage/condition_variable.h"
46 : : #include "storage/ipc.h"
47 : : #include "storage/lmgr.h"
48 : : #include "storage/pmsignal.h"
49 : : #include "storage/proc.h"
50 : : #include "storage/procarray.h"
51 : : #include "storage/procsignal.h"
52 : : #include "storage/spin.h"
53 : : #include "storage/standby.h"
54 : : #include "utils/timeout.h"
55 : : #include "utils/timestamp.h"
56 : : #include "utils/wait_event.h"
57 : :
58 : : /* GUC variables */
59 : : int DeadlockTimeout = 1000;
60 : : int StatementTimeout = 0;
61 : : int LockTimeout = 0;
62 : : int IdleInTransactionSessionTimeout = 0;
63 : : int TransactionTimeout = 0;
64 : : int IdleSessionTimeout = 0;
65 : : bool log_lock_waits = true;
66 : :
67 : : /* Pointer to this process's PGPROC struct, if any */
68 : : PGPROC *MyProc = NULL;
69 : :
70 : : /* Pointers to shared-memory structures */
71 : : PROC_HDR *ProcGlobal = NULL;
72 : : NON_EXEC_STATIC PGPROC *AuxiliaryProcs = NULL;
73 : : PGPROC *PreparedXactProcs = NULL;
74 : :
75 : : /* Is a deadlock check pending? */
76 : : static volatile sig_atomic_t got_deadlock_timeout;
77 : :
78 : : static void RemoveProcFromArray(int code, Datum arg);
79 : : static void ProcKill(int code, Datum arg);
80 : : static void AuxiliaryProcKill(int code, Datum arg);
81 : : static DeadLockState CheckDeadLock(void);
82 : :
83 : :
84 : : /*
85 : : * Report shared-memory space needed by PGPROC.
86 : : */
87 : : static Size
347 tomas.vondra@postgre 88 :CBC 3297 : PGProcShmemSize(void)
89 : : {
7512 tgl@sss.pgh.pa.us 90 : 3297 : Size size = 0;
91 : : Size TotalProcs =
1031 92 : 3297 : add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts));
93 : :
2039 andres@anarazel.de 94 : 3297 : size = add_size(size, mul_size(TotalProcs, sizeof(PGPROC)));
95 : 3297 : size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->xids)));
96 : 3297 : size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->subxidStates)));
1945 alvherre@alvh.no-ip. 97 : 3297 : size = add_size(size, mul_size(TotalProcs, sizeof(*ProcGlobal->statusFlags)));
98 : :
347 tomas.vondra@postgre 99 : 3297 : return size;
100 : : }
101 : :
102 : : /*
103 : : * Report shared-memory space needed by Fast-Path locks.
104 : : */
105 : : static Size
106 : 3297 : FastPathLockShmemSize(void)
107 : : {
108 : 3297 : Size size = 0;
109 : : Size TotalProcs =
110 : 3297 : add_size(MaxBackends, add_size(NUM_AUXILIARY_PROCS, max_prepared_xacts));
111 : : Size fpLockBitsSize,
112 : : fpRelIdSize;
113 : :
114 : : /*
115 : : * Memory needed for PGPROC fast-path lock arrays. Make sure the sizes are
116 : : * nicely aligned in each backend.
117 : : */
540 118 : 3297 : fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64));
376 119 : 3297 : fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid));
120 : :
540 121 : 3297 : size = add_size(size, mul_size(TotalProcs, (fpLockBitsSize + fpRelIdSize)));
122 : :
7837 tgl@sss.pgh.pa.us 123 : 3297 : return size;
124 : : }
125 : :
126 : : /*
127 : : * Report shared-memory space needed by InitProcGlobal.
128 : : */
129 : : Size
347 tomas.vondra@postgre 130 : 2147 : ProcGlobalShmemSize(void)
131 : : {
132 : 2147 : Size size = 0;
133 : :
134 : : /* ProcGlobal */
135 : 2147 : size = add_size(size, sizeof(PROC_HDR));
136 : 2147 : size = add_size(size, sizeof(slock_t));
137 : :
129 heikki.linnakangas@i 138 :GNC 2147 : size = add_size(size, PGSemaphoreShmemSize(ProcGlobalSemas()));
347 tomas.vondra@postgre 139 :CBC 2147 : size = add_size(size, PGProcShmemSize());
140 : 2147 : size = add_size(size, FastPathLockShmemSize());
141 : :
142 : 2147 : return size;
143 : : }
144 : :
145 : : /*
146 : : * Report number of semaphores needed by InitProcGlobal.
147 : : */
148 : : int
7576 tgl@sss.pgh.pa.us 149 : 4292 : ProcGlobalSemas(void)
150 : : {
151 : : /*
152 : : * We need a sema per backend (including autovacuum), plus one for each
153 : : * auxiliary process.
154 : : */
1433 rhaas@postgresql.org 155 : 4292 : return MaxBackends + NUM_AUXILIARY_PROCS;
156 : : }
157 : :
158 : : /*
159 : : * InitProcGlobal -
160 : : * Initialize the global process table during postmaster or standalone
161 : : * backend startup.
162 : : *
163 : : * We also create all the per-process semaphores we will need to support
164 : : * the requested number of backends. We used to allocate semaphores
165 : : * only when backends were actually started up, but that is bad because
166 : : * it lets Postgres fail under load --- a lot of Unix systems are
167 : : * (mis)configured with small limits on the number of semaphores, and
168 : : * running out when trying to start another backend is a common failure.
169 : : * So, now we grab enough semaphores to support the desired max number
170 : : * of backends immediately at initialization --- if the sysadmin has set
171 : : * MaxConnections, max_worker_processes, max_wal_senders, or
172 : : * autovacuum_worker_slots higher than his kernel will support, he'll
173 : : * find out sooner rather than later.
174 : : *
175 : : * Another reason for creating semaphores here is that the semaphore
176 : : * implementation typically requires us to create semaphores in the
177 : : * postmaster, not in backends.
178 : : *
179 : : * Note: this is NOT called by individual backends under a postmaster,
180 : : * not even in the EXEC_BACKEND case. The ProcGlobal and AuxiliaryProcs
181 : : * pointers must be propagated specially for EXEC_BACKEND operation.
182 : : */
183 : : void
7576 tgl@sss.pgh.pa.us 184 : 1150 : InitProcGlobal(void)
185 : : {
186 : : PGPROC *procs;
187 : : int i,
188 : : j;
189 : : bool found;
1433 rhaas@postgresql.org 190 : 1150 : uint32 TotalProcs = MaxBackends + NUM_AUXILIARY_PROCS + max_prepared_xacts;
191 : :
192 : : /* Used for setup of per-backend fast-path slots. */
193 : : char *fpPtr,
194 : : *fpEndPtr PG_USED_FOR_ASSERTS_ONLY;
195 : : Size fpLockBitsSize,
196 : : fpRelIdSize;
197 : : Size requestSize;
198 : : char *ptr;
199 : :
200 : : /* Create the ProcGlobal shared structure */
10416 bruce@momjian.us 201 : 1150 : ProcGlobal = (PROC_HDR *)
7375 tgl@sss.pgh.pa.us 202 : 1150 : ShmemInitStruct("Proc Header", sizeof(PROC_HDR), &found);
203 [ - + ]: 1150 : Assert(!found);
204 : :
205 : : /*
206 : : * Initialize the data structures.
207 : : */
5390 rhaas@postgresql.org 208 : 1150 : ProcGlobal->spins_per_delay = DEFAULT_SPINS_PER_DELAY;
32 heikki.linnakangas@i 209 :GNC 1150 : SpinLockInit(&ProcGlobal->freeProcsLock);
1152 andres@anarazel.de 210 :CBC 1150 : dlist_init(&ProcGlobal->freeProcs);
211 : 1150 : dlist_init(&ProcGlobal->autovacFreeProcs);
212 : 1150 : dlist_init(&ProcGlobal->bgworkerFreeProcs);
213 : 1150 : dlist_init(&ProcGlobal->walsenderFreeProcs);
5339 tgl@sss.pgh.pa.us 214 : 1150 : ProcGlobal->startupBufferPinWaitBufId = -1;
499 heikki.linnakangas@i 215 : 1150 : ProcGlobal->walwriterProc = INVALID_PROC_NUMBER;
216 : 1150 : ProcGlobal->checkpointerProc = INVALID_PROC_NUMBER;
742 217 : 1150 : pg_atomic_init_u32(&ProcGlobal->procArrayGroupFirst, INVALID_PROC_NUMBER);
218 : 1150 : pg_atomic_init_u32(&ProcGlobal->clogGroupFirst, INVALID_PROC_NUMBER);
219 : :
220 : : /*
221 : : * Create and initialize all the PGPROC structures we'll need. There are
222 : : * six separate consumers: (1) normal backends, (2) autovacuum workers and
223 : : * special workers, (3) background workers, (4) walsenders, (5) auxiliary
224 : : * processes, and (6) prepared transactions. (For largely-historical
225 : : * reasons, we combine autovacuum and special workers into one category
226 : : * with a single freelist.) Each PGPROC structure is dedicated to exactly
227 : : * one of these purposes, and they do not move between groups.
228 : : */
347 tomas.vondra@postgre 229 : 1150 : requestSize = PGProcShmemSize();
230 : :
231 : 1150 : ptr = ShmemInitStruct("PGPROC structures",
232 : : requestSize,
233 : : &found);
234 : :
235 [ + - + + : 1150 : MemSet(ptr, 0, requestSize);
+ - - + -
- ]
236 : :
237 : 1150 : procs = (PGPROC *) ptr;
103 peter@eisentraut.org 238 :GNC 1150 : ptr = ptr + TotalProcs * sizeof(PGPROC);
239 : :
5405 rhaas@postgresql.org 240 :CBC 1150 : ProcGlobal->allProcs = procs;
241 : : /* XXX allProcCount isn't really all of them; it excludes prepared xacts */
1433 242 : 1150 : ProcGlobal->allProcCount = MaxBackends + NUM_AUXILIARY_PROCS;
243 : :
244 : : /*
245 : : * Allocate arrays mirroring PGPROC fields in a dense manner. See
246 : : * PROC_HDR.
247 : : *
248 : : * XXX: It might make sense to increase padding for these arrays, given
249 : : * how hotly they are accessed.
250 : : */
347 tomas.vondra@postgre 251 : 1150 : ProcGlobal->xids = (TransactionId *) ptr;
103 peter@eisentraut.org 252 :GNC 1150 : ptr = ptr + (TotalProcs * sizeof(*ProcGlobal->xids));
253 : :
347 tomas.vondra@postgre 254 :CBC 1150 : ProcGlobal->subxidStates = (XidCacheStatus *) ptr;
103 peter@eisentraut.org 255 :GNC 1150 : ptr = ptr + (TotalProcs * sizeof(*ProcGlobal->subxidStates));
256 : :
347 tomas.vondra@postgre 257 :CBC 1150 : ProcGlobal->statusFlags = (uint8 *) ptr;
103 peter@eisentraut.org 258 :GNC 1150 : ptr = ptr + (TotalProcs * sizeof(*ProcGlobal->statusFlags));
259 : :
260 : : /* make sure wer didn't overflow */
347 tomas.vondra@postgre 261 [ + - - + ]:CBC 1150 : Assert((ptr > (char *) procs) && (ptr <= (char *) procs + requestSize));
262 : :
263 : : /*
264 : : * Allocate arrays for fast-path locks. Those are variable-length, so
265 : : * can't be included in PGPROC directly. We allocate a separate piece of
266 : : * shared memory and then divide that between backends.
267 : : */
540 268 : 1150 : fpLockBitsSize = MAXALIGN(FastPathLockGroupsPerBackend * sizeof(uint64));
376 269 : 1150 : fpRelIdSize = MAXALIGN(FastPathLockSlotsPerBackend() * sizeof(Oid));
270 : :
347 271 : 1150 : requestSize = FastPathLockShmemSize();
272 : :
273 : 1150 : fpPtr = ShmemInitStruct("Fast-Path Lock Array",
274 : : requestSize,
275 : : &found);
276 : :
277 [ + - + - : 1150 : MemSet(fpPtr, 0, requestSize);
+ - - + -
- ]
278 : :
279 : : /* For asserts checking we did not overflow. */
280 : 1150 : fpEndPtr = fpPtr + requestSize;
281 : :
282 : : /* Reserve space for semaphores. */
129 heikki.linnakangas@i 283 :GNC 1150 : PGReserveSemaphores(ProcGlobalSemas());
284 : :
5390 rhaas@postgresql.org 285 [ + + ]:CBC 151402 : for (i = 0; i < TotalProcs; i++)
286 : : {
1152 andres@anarazel.de 287 : 150252 : PGPROC *proc = &procs[i];
288 : :
289 : : /* Common initialization for all PGPROCs, regardless of type. */
290 : :
291 : : /*
292 : : * Set the fast-path lock arrays, and move the pointer. We interleave
293 : : * the two arrays, to (hopefully) get some locality for each backend.
294 : : */
540 tomas.vondra@postgre 295 : 150252 : proc->fpLockBits = (uint64 *) fpPtr;
296 : 150252 : fpPtr += fpLockBitsSize;
297 : :
298 : 150252 : proc->fpRelId = (Oid *) fpPtr;
299 : 150252 : fpPtr += fpRelIdSize;
300 : :
301 [ - + ]: 150252 : Assert(fpPtr <= fpEndPtr);
302 : :
303 : : /*
304 : : * Set up per-PGPROC semaphore, latch, and fpInfoLock. Prepared xact
305 : : * dummy PGPROCs don't need these though - they're never associated
306 : : * with a real process
307 : : */
13 heikki.linnakangas@i 308 [ + + ]: 150252 : if (i < FIRST_PREPARED_XACT_PROC_NUMBER)
309 : : {
1152 andres@anarazel.de 310 : 149384 : proc->sem = PGSemaphoreCreate();
311 : 149384 : InitSharedLatch(&(proc->procLatch));
312 : 149384 : LWLockInitialize(&(proc->fpInfoLock), LWTRANCHE_LOCK_FASTPATH);
313 : : }
314 : :
315 : : /*
316 : : * Newly created PGPROCs for normal backends, autovacuum workers,
317 : : * special workers, bgworkers, and walsenders must be queued up on the
318 : : * appropriate free list. Because there can only ever be a small,
319 : : * fixed number of auxiliary processes, no free list is used in that
320 : : * case; InitAuxiliaryProcess() instead uses a linear search. PGPROCs
321 : : * for prepared transactions are added to a free list by
322 : : * TwoPhaseShmemInit().
323 : : */
5390 rhaas@postgresql.org 324 [ + + ]: 150252 : if (i < MaxConnections)
325 : : {
326 : : /* PGPROC for normal backend, add to freeProcs list */
23 heikki.linnakangas@i 327 :GNC 74162 : dlist_push_tail(&ProcGlobal->freeProcs, &proc->freeProcsLink);
1152 andres@anarazel.de 328 :CBC 74162 : proc->procgloballist = &ProcGlobal->freeProcs;
329 : : }
433 nathan@postgresql.or 330 [ + + ]: 76090 : else if (i < MaxConnections + autovacuum_worker_slots + NUM_SPECIAL_WORKER_PROCS)
331 : : {
332 : : /* PGPROC for AV or special worker, add to autovacFreeProcs list */
23 heikki.linnakangas@i 333 :GNC 14863 : dlist_push_tail(&ProcGlobal->autovacFreeProcs, &proc->freeProcsLink);
1152 andres@anarazel.de 334 :CBC 14863 : proc->procgloballist = &ProcGlobal->autovacFreeProcs;
335 : : }
433 nathan@postgresql.or 336 [ + + ]: 61227 : else if (i < MaxConnections + autovacuum_worker_slots + NUM_SPECIAL_WORKER_PROCS + max_worker_processes)
337 : : {
338 : : /* PGPROC for bgworker, add to bgworkerFreeProcs list */
23 heikki.linnakangas@i 339 :GNC 9191 : dlist_push_tail(&ProcGlobal->bgworkerFreeProcs, &proc->freeProcsLink);
1152 andres@anarazel.de 340 :CBC 9191 : proc->procgloballist = &ProcGlobal->bgworkerFreeProcs;
341 : : }
1433 rhaas@postgresql.org 342 [ + + ]: 52036 : else if (i < MaxBackends)
343 : : {
344 : : /* PGPROC for walsender, add to walsenderFreeProcs list */
23 heikki.linnakangas@i 345 :GNC 7468 : dlist_push_tail(&ProcGlobal->walsenderFreeProcs, &proc->freeProcsLink);
1152 andres@anarazel.de 346 :CBC 7468 : proc->procgloballist = &ProcGlobal->walsenderFreeProcs;
347 : : }
348 : :
349 : : /* Initialize myProcLocks[] shared memory queues. */
5248 rhaas@postgresql.org 350 [ + + ]: 2554284 : for (j = 0; j < NUM_LOCK_PARTITIONS; j++)
1152 andres@anarazel.de 351 : 2404032 : dlist_init(&(proc->myProcLocks[j]));
352 : :
353 : : /* Initialize lockGroupMembers list. */
354 : 150252 : dlist_init(&proc->lockGroupMembers);
355 : :
356 : : /*
357 : : * Initialize the atomic variables, otherwise, it won't be safe to
358 : : * access them for backends that aren't currently in use.
359 : : */
742 heikki.linnakangas@i 360 : 150252 : pg_atomic_init_u32(&(proc->procArrayGroupNext), INVALID_PROC_NUMBER);
361 : 150252 : pg_atomic_init_u32(&(proc->clogGroupNext), INVALID_PROC_NUMBER);
1152 andres@anarazel.de 362 : 150252 : pg_atomic_init_u64(&(proc->waitStart), 0);
363 : : }
364 : :
365 : : /* Should have consumed exactly the expected amount of fast-path memory. */
538 tomas.vondra@postgre 366 [ - + ]: 1150 : Assert(fpPtr == fpEndPtr);
367 : :
368 : : /*
369 : : * Save pointers to the blocks of PGPROC structures reserved for auxiliary
370 : : * processes and prepared transactions.
371 : : */
1433 rhaas@postgresql.org 372 : 1150 : AuxiliaryProcs = &procs[MaxBackends];
13 heikki.linnakangas@i 373 : 1150 : PreparedXactProcs = &procs[FIRST_PREPARED_XACT_PROC_NUMBER];
10841 scrappy@hub.org 374 : 1150 : }
375 : :
376 : : /*
377 : : * InitProcess -- initialize a per-process PGPROC entry for this backend
378 : : */
379 : : void
9238 tgl@sss.pgh.pa.us 380 : 16988 : InitProcess(void)
381 : : {
382 : : dlist_head *procgloballist;
383 : :
384 : : /*
385 : : * ProcGlobal should be set up already (if we are a backend, we inherit
386 : : * this by fork() or EXEC_BACKEND mechanism from the postmaster).
387 : : */
3803 rhaas@postgresql.org 388 [ - + ]: 16988 : if (ProcGlobal == NULL)
8270 tgl@sss.pgh.pa.us 389 [ # # ]:UBC 0 : elog(PANIC, "proc header uninitialized");
390 : :
8955 tgl@sss.pgh.pa.us 391 [ - + ]:CBC 16988 : if (MyProc != NULL)
8270 tgl@sss.pgh.pa.us 392 [ # # ]:UBC 0 : elog(ERROR, "you already exist");
393 : :
394 : : /*
395 : : * Before we start accessing the shared memory in a serious way, mark
396 : : * ourselves as an active postmaster child; this is so that the postmaster
397 : : * can detect it if we exit without cleaning up.
398 : : */
486 heikki.linnakangas@i 399 [ + + ]:CBC 16988 : if (IsUnderPostmaster)
523 400 : 16867 : RegisterPostmasterChildActive();
401 : :
402 : : /*
403 : : * Decide which list should supply our PGPROC. This logic must match the
404 : : * way the freelists were constructed in InitProcGlobal().
405 : : */
442 tgl@sss.pgh.pa.us 406 [ + + + + : 16988 : if (AmAutoVacuumWorkerProcess() || AmSpecialWorkerProcess())
+ + ]
3803 rhaas@postgresql.org 407 : 471 : procgloballist = &ProcGlobal->autovacFreeProcs;
741 heikki.linnakangas@i 408 [ + + ]: 16517 : else if (AmBackgroundWorkerProcess())
3803 rhaas@postgresql.org 409 : 2547 : procgloballist = &ProcGlobal->bgworkerFreeProcs;
741 heikki.linnakangas@i 410 [ + + ]: 13970 : else if (AmWalSenderProcess())
2588 michael@paquier.xyz 411 : 1248 : procgloballist = &ProcGlobal->walsenderFreeProcs;
412 : : else
3803 rhaas@postgresql.org 413 : 12722 : procgloballist = &ProcGlobal->freeProcs;
414 : :
415 : : /*
416 : : * Try to get a proc struct from the appropriate free list. If this
417 : : * fails, we must be out of PGPROC structures (not to mention semaphores).
418 : : *
419 : : * While we are holding the spinlock, also copy the current shared
420 : : * estimate of spins_per_delay to local storage.
421 : : */
32 heikki.linnakangas@i 422 :GNC 16988 : SpinLockAcquire(&ProcGlobal->freeProcsLock);
423 : :
3803 rhaas@postgresql.org 424 :CBC 16988 : set_spins_per_delay(ProcGlobal->spins_per_delay);
425 : :
1152 andres@anarazel.de 426 [ + + ]: 16988 : if (!dlist_is_empty(procgloballist))
427 : : {
23 heikki.linnakangas@i 428 :GNC 16985 : MyProc = dlist_container(PGPROC, freeProcsLink, dlist_pop_head_node(procgloballist));
32 429 : 16985 : SpinLockRelease(&ProcGlobal->freeProcsLock);
430 : : }
431 : : else
432 : : {
433 : : /*
434 : : * If we reach here, all the PGPROCs are in use. This is one of the
435 : : * possible places to detect "too many backends", so give the standard
436 : : * error message. XXX do we need to give a different failure message
437 : : * in the autovacuum case?
438 : : */
439 : 3 : SpinLockRelease(&ProcGlobal->freeProcsLock);
741 heikki.linnakangas@i 440 [ + + ]:CBC 3 : if (AmWalSenderProcess())
2588 michael@paquier.xyz 441 [ + - ]: 2 : ereport(FATAL,
442 : : (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
443 : : errmsg("number of requested standby connections exceeds \"max_wal_senders\" (currently %d)",
444 : : max_wal_senders)));
8270 tgl@sss.pgh.pa.us 445 [ + - ]: 1 : ereport(FATAL,
446 : : (errcode(ERRCODE_TOO_MANY_CONNECTIONS),
447 : : errmsg("sorry, too many clients already")));
448 : : }
752 heikki.linnakangas@i 449 : 16985 : MyProcNumber = GetNumberFromPGProc(MyProc);
450 : :
451 : : /*
452 : : * Cross-check that the PGPROC is of the type we expect; if this were not
453 : : * the case, it would get returned to the wrong list.
454 : : */
3883 rhaas@postgresql.org 455 [ - + ]: 16985 : Assert(MyProc->procgloballist == procgloballist);
456 : :
457 : : /*
458 : : * Initialize all fields of MyProc, except for those previously
459 : : * initialized by InitProcGlobal.
460 : : */
23 heikki.linnakangas@i 461 :GNC 16985 : dlist_node_init(&MyProc->freeProcsLink);
2097 peter@eisentraut.org 462 :CBC 16985 : MyProc->waitStatus = PROC_WAIT_STATUS_OK;
4854 simon@2ndQuadrant.co 463 : 16985 : MyProc->fpVXIDLock = false;
464 : 16985 : MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
2039 andres@anarazel.de 465 : 16985 : MyProc->xid = InvalidTransactionId;
2040 466 : 16985 : MyProc->xmin = InvalidTransactionId;
8933 tgl@sss.pgh.pa.us 467 : 16985 : MyProc->pid = MyProcPid;
742 heikki.linnakangas@i 468 : 16985 : MyProc->vxid.procNumber = MyProcNumber;
469 : 16985 : MyProc->vxid.lxid = InvalidLocalTransactionId;
470 : : /* databaseId and roleId will be filled in later */
7375 tgl@sss.pgh.pa.us 471 : 16985 : MyProc->databaseId = InvalidOid;
7532 472 : 16985 : MyProc->roleId = InvalidOid;
2771 michael@paquier.xyz 473 : 16985 : MyProc->tempNamespaceId = InvalidOid;
39 heikki.linnakangas@i 474 :GNC 16985 : MyProc->backendType = MyBackendType;
1437 rhaas@postgresql.org 475 :CBC 16985 : MyProc->delayChkptFlags = 0;
1945 alvherre@alvh.no-ip. 476 : 16985 : MyProc->statusFlags = 0;
477 : : /* NB -- autovac launcher intentionally does not set IS_AUTOVACUUM */
741 heikki.linnakangas@i 478 [ + + ]: 16985 : if (AmAutoVacuumWorkerProcess())
1945 alvherre@alvh.no-ip. 479 : 38 : MyProc->statusFlags |= PROC_IS_AUTOVACUUM;
1211 andres@anarazel.de 480 : 16985 : MyProc->lwWaiting = LW_WS_NOT_WAITING;
5158 heikki.linnakangas@i 481 : 16985 : MyProc->lwWaitMode = 0;
9183 tgl@sss.pgh.pa.us 482 : 16985 : MyProc->waitLock = NULL;
23 heikki.linnakangas@i 483 :GNC 16985 : dlist_node_init(&MyProc->waitLink);
7870 tgl@sss.pgh.pa.us 484 :CBC 16985 : MyProc->waitProcLock = NULL;
1847 fujii@postgresql.org 485 : 16985 : pg_atomic_write_u64(&MyProc->waitStart, 0);
486 : : #ifdef USE_ASSERT_CHECKING
487 : : {
488 : : int i;
489 : :
490 : : /* Last process should have released all locks. */
5248 rhaas@postgresql.org 491 [ + + ]: 288745 : for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1152 andres@anarazel.de 492 [ - + ]: 271760 : Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
493 : : }
494 : : #endif
33 heikki.linnakangas@i 495 :GNC 16985 : pg_atomic_write_u32(&MyProc->pendingRecoveryConflicts, 0);
496 : :
497 : : /* Initialize fields for sync rep */
45 alvherre@kurilemu.de 498 : 16985 : MyProc->waitLSN = InvalidXLogRecPtr;
5488 simon@2ndQuadrant.co 499 :CBC 16985 : MyProc->syncRepState = SYNC_REP_NOT_WAITING;
1152 andres@anarazel.de 500 : 16985 : dlist_node_init(&MyProc->syncRepLinks);
501 : :
502 : : /* Initialize fields for group XID clearing. */
3685 rhaas@postgresql.org 503 : 16985 : MyProc->procArrayGroupMember = false;
504 : 16985 : MyProc->procArrayGroupMemberXid = InvalidTransactionId;
742 heikki.linnakangas@i 505 [ - + ]: 16985 : Assert(pg_atomic_read_u32(&MyProc->procArrayGroupNext) == INVALID_PROC_NUMBER);
506 : :
507 : : /* Check that group locking fields are in a proper initial state. */
3689 rhaas@postgresql.org 508 [ - + ]: 16985 : Assert(MyProc->lockGroupLeader == NULL);
509 [ - + ]: 16985 : Assert(dlist_is_empty(&MyProc->lockGroupMembers));
510 : :
511 : : /* Initialize wait event information. */
3657 512 : 16985 : MyProc->wait_event_info = 0;
513 : :
514 : : /* Initialize fields for group transaction status update. */
3117 515 : 16985 : MyProc->clogGroupMember = false;
516 : 16985 : MyProc->clogGroupMemberXid = InvalidTransactionId;
517 : 16985 : MyProc->clogGroupMemberXidStatus = TRANSACTION_STATUS_IN_PROGRESS;
518 : 16985 : MyProc->clogGroupMemberPage = -1;
519 : 16985 : MyProc->clogGroupMemberLsn = InvalidXLogRecPtr;
742 heikki.linnakangas@i 520 [ - + ]: 16985 : Assert(pg_atomic_read_u32(&MyProc->clogGroupNext) == INVALID_PROC_NUMBER);
521 : :
522 : : /*
523 : : * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
524 : : * on it. That allows us to repoint the process latch, which so far
525 : : * points to process local one, to the shared one.
526 : : */
5331 tgl@sss.pgh.pa.us 527 : 16985 : OwnLatch(&MyProc->procLatch);
4078 andres@anarazel.de 528 : 16985 : SwitchToSharedLatch();
529 : :
530 : : /* now that we have a proc, report wait events to shared memory */
1807 531 : 16985 : pgstat_set_wait_event_storage(&MyProc->wait_event_info);
532 : :
533 : : /*
534 : : * We might be reusing a semaphore that belonged to a failed process. So
535 : : * be careful and reinitialize its value here. (This is not strictly
536 : : * necessary anymore, but seems like a good idea for cleanliness.)
537 : : */
3380 tgl@sss.pgh.pa.us 538 : 16985 : PGSemaphoreReset(MyProc->sem);
539 : :
540 : : /*
541 : : * Arrange to clean up at backend exit.
542 : : */
9191 543 : 16985 : on_shmem_exit(ProcKill, 0);
544 : :
545 : : /*
546 : : * Now that we have a PGPROC, we could try to acquire locks, so initialize
547 : : * local state needed for LWLocks, and the deadlock checker.
548 : : */
4276 heikki.linnakangas@i 549 : 16985 : InitLWLockAccess();
9180 tgl@sss.pgh.pa.us 550 : 16985 : InitDeadLockChecking();
551 : :
552 : : #ifdef EXEC_BACKEND
553 : :
554 : : /*
555 : : * Initialize backend-local pointers to all the shared data structures.
556 : : * (We couldn't do this until now because it needs LWLocks.)
557 : : */
558 : : if (IsUnderPostmaster)
559 : : AttachSharedMemoryStructs();
560 : : #endif
9191 561 : 16985 : }
562 : :
563 : : /*
564 : : * InitProcessPhase2 -- make MyProc visible in the shared ProcArray.
565 : : *
566 : : * This is separate from InitProcess because we can't acquire LWLocks until
567 : : * we've created a PGPROC, but in the EXEC_BACKEND case ProcArrayAdd won't
568 : : * work until after we've done AttachSharedMemoryStructs.
569 : : */
570 : : void
7375 571 : 16976 : InitProcessPhase2(void)
572 : : {
573 [ - + ]: 16976 : Assert(MyProc != NULL);
574 : :
575 : : /*
576 : : * Add our PGPROC to the PGPROC array in shared memory.
577 : : */
578 : 16976 : ProcArrayAdd(MyProc);
579 : :
580 : : /*
581 : : * Arrange to clean that up at backend exit.
582 : : */
583 : 16976 : on_shmem_exit(RemoveProcFromArray, 0);
584 : 16976 : }
585 : :
586 : : /*
587 : : * InitAuxiliaryProcess -- create a PGPROC entry for an auxiliary process
588 : : *
589 : : * This is called by bgwriter and similar processes so that they will have a
590 : : * MyProc value that's real enough to let them wait for LWLocks. The PGPROC
591 : : * and sema that are assigned are one of the extra ones created during
592 : : * InitProcGlobal.
593 : : *
594 : : * Auxiliary processes are presently not expected to wait for real (lockmgr)
595 : : * locks, so we need not set up the deadlock checker. They are never added
596 : : * to the ProcArray or the sinval messaging mechanism, either. They also
597 : : * don't get a VXID assigned, since this is only useful when we actually
598 : : * hold lockmgr locks.
599 : : *
600 : : * Startup process however uses locks but never waits for them in the
601 : : * normal backend sense. Startup process also takes part in sinval messaging
602 : : * as a sendOnly process, so never reads messages from sinval queue. So
603 : : * Startup process does have a VXID and does show up in pg_locks.
604 : : */
605 : : void
6948 alvherre@alvh.no-ip. 606 : 4579 : InitAuxiliaryProcess(void)
607 : : {
608 : : PGPROC *auxproc;
609 : : int proctype;
610 : :
611 : : /*
612 : : * ProcGlobal should be set up already (if we are a backend, we inherit
613 : : * this by fork() or EXEC_BACKEND mechanism from the postmaster).
614 : : */
615 [ + - - + ]: 4579 : if (ProcGlobal == NULL || AuxiliaryProcs == NULL)
8270 tgl@sss.pgh.pa.us 616 [ # # ]:UBC 0 : elog(PANIC, "proc header uninitialized");
617 : :
8933 tgl@sss.pgh.pa.us 618 [ - + ]:CBC 4579 : if (MyProc != NULL)
8270 tgl@sss.pgh.pa.us 619 [ # # ]:UBC 0 : elog(ERROR, "you already exist");
620 : :
486 heikki.linnakangas@i 621 [ + - ]:CBC 4579 : if (IsUnderPostmaster)
622 : 4579 : RegisterPostmasterChildActive();
623 : :
624 : : /*
625 : : * We use the freeProcsLock to protect assignment and releasing of
626 : : * AuxiliaryProcs entries.
627 : : *
628 : : * While we are holding the spinlock, also copy the current shared
629 : : * estimate of spins_per_delay to local storage.
630 : : */
32 heikki.linnakangas@i 631 :GNC 4579 : SpinLockAcquire(&ProcGlobal->freeProcsLock);
632 : :
7460 tgl@sss.pgh.pa.us 633 :CBC 4579 : set_spins_per_delay(ProcGlobal->spins_per_delay);
634 : :
635 : : /*
636 : : * Find a free auxproc ... *big* trouble if there isn't one ...
637 : : */
6948 alvherre@alvh.no-ip. 638 [ + - ]: 18692 : for (proctype = 0; proctype < NUM_AUXILIARY_PROCS; proctype++)
639 : : {
640 : 18692 : auxproc = &AuxiliaryProcs[proctype];
641 [ + + ]: 18692 : if (auxproc->pid == 0)
7375 tgl@sss.pgh.pa.us 642 : 4579 : break;
643 : : }
6948 alvherre@alvh.no-ip. 644 [ - + ]: 4579 : if (proctype >= NUM_AUXILIARY_PROCS)
645 : : {
32 heikki.linnakangas@i 646 :UNC 0 : SpinLockRelease(&ProcGlobal->freeProcsLock);
6948 alvherre@alvh.no-ip. 647 [ # # ]:UBC 0 : elog(FATAL, "all AuxiliaryProcs are in use");
648 : : }
649 : :
650 : : /* Mark auxiliary proc as in use by me */
651 : : /* use volatile pointer to prevent code rearrangement */
6948 alvherre@alvh.no-ip. 652 :CBC 4579 : ((volatile PGPROC *) auxproc)->pid = MyProcPid;
653 : :
32 heikki.linnakangas@i 654 :GNC 4579 : SpinLockRelease(&ProcGlobal->freeProcsLock);
655 : :
742 heikki.linnakangas@i 656 :CBC 4579 : MyProc = auxproc;
752 657 : 4579 : MyProcNumber = GetNumberFromPGProc(MyProc);
658 : :
659 : : /*
660 : : * Initialize all fields of MyProc, except for those previously
661 : : * initialized by InitProcGlobal.
662 : : */
23 heikki.linnakangas@i 663 :GNC 4579 : dlist_node_init(&MyProc->freeProcsLink);
2097 peter@eisentraut.org 664 :CBC 4579 : MyProc->waitStatus = PROC_WAIT_STATUS_OK;
4854 simon@2ndQuadrant.co 665 : 4579 : MyProc->fpVXIDLock = false;
666 : 4579 : MyProc->fpLocalTransactionId = InvalidLocalTransactionId;
2039 andres@anarazel.de 667 : 4579 : MyProc->xid = InvalidTransactionId;
2040 668 : 4579 : MyProc->xmin = InvalidTransactionId;
742 heikki.linnakangas@i 669 : 4579 : MyProc->vxid.procNumber = INVALID_PROC_NUMBER;
670 : 4579 : MyProc->vxid.lxid = InvalidLocalTransactionId;
7375 tgl@sss.pgh.pa.us 671 : 4579 : MyProc->databaseId = InvalidOid;
7532 672 : 4579 : MyProc->roleId = InvalidOid;
2771 michael@paquier.xyz 673 : 4579 : MyProc->tempNamespaceId = InvalidOid;
39 heikki.linnakangas@i 674 :GNC 4579 : MyProc->backendType = MyBackendType;
1437 rhaas@postgresql.org 675 :CBC 4579 : MyProc->delayChkptFlags = 0;
1945 alvherre@alvh.no-ip. 676 : 4579 : MyProc->statusFlags = 0;
1211 andres@anarazel.de 677 : 4579 : MyProc->lwWaiting = LW_WS_NOT_WAITING;
5158 heikki.linnakangas@i 678 : 4579 : MyProc->lwWaitMode = 0;
8933 tgl@sss.pgh.pa.us 679 : 4579 : MyProc->waitLock = NULL;
23 heikki.linnakangas@i 680 :GNC 4579 : dlist_node_init(&MyProc->waitLink);
7870 tgl@sss.pgh.pa.us 681 :CBC 4579 : MyProc->waitProcLock = NULL;
1847 fujii@postgresql.org 682 : 4579 : pg_atomic_write_u64(&MyProc->waitStart, 0);
683 : : #ifdef USE_ASSERT_CHECKING
684 : : {
685 : : int i;
686 : :
687 : : /* Last process should have released all locks. */
5248 rhaas@postgresql.org 688 [ + + ]: 77843 : for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1152 andres@anarazel.de 689 [ - + ]: 73264 : Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
690 : : }
691 : : #endif
4 heikki.linnakangas@i 692 :GNC 4579 : pg_atomic_write_u32(&MyProc->pendingRecoveryConflicts, 0);
693 : :
694 : : /*
695 : : * Acquire ownership of the PGPROC's latch, so that we can use WaitLatch
696 : : * on it. That allows us to repoint the process latch, which so far
697 : : * points to process local one, to the shared one.
698 : : */
5331 tgl@sss.pgh.pa.us 699 :CBC 4579 : OwnLatch(&MyProc->procLatch);
4078 andres@anarazel.de 700 : 4579 : SwitchToSharedLatch();
701 : :
702 : : /* now that we have a proc, report wait events to shared memory */
1807 703 : 4579 : pgstat_set_wait_event_storage(&MyProc->wait_event_info);
704 : :
705 : : /* Check that group locking fields are in a proper initial state. */
3689 rhaas@postgresql.org 706 [ - + ]: 4579 : Assert(MyProc->lockGroupLeader == NULL);
707 [ - + ]: 4579 : Assert(dlist_is_empty(&MyProc->lockGroupMembers));
708 : :
709 : : /*
710 : : * We might be reusing a semaphore that belonged to a failed process. So
711 : : * be careful and reinitialize its value here. (This is not strictly
712 : : * necessary anymore, but seems like a good idea for cleanliness.)
713 : : */
3380 tgl@sss.pgh.pa.us 714 : 4579 : PGSemaphoreReset(MyProc->sem);
715 : :
716 : : /*
717 : : * Arrange to clean up at process exit.
718 : : */
6948 alvherre@alvh.no-ip. 719 : 4579 : on_shmem_exit(AuxiliaryProcKill, Int32GetDatum(proctype));
720 : :
721 : : /*
722 : : * Now that we have a PGPROC, we could try to acquire lightweight locks.
723 : : * Initialize local state needed for them. (Heavyweight locks cannot be
724 : : * acquired in aux processes.)
725 : : */
835 heikki.linnakangas@i 726 : 4579 : InitLWLockAccess();
727 : :
728 : : #ifdef EXEC_BACKEND
729 : :
730 : : /*
731 : : * Initialize backend-local pointers to all the shared data structures.
732 : : * (We couldn't do this until now because it needs LWLocks.)
733 : : */
734 : : if (IsUnderPostmaster)
735 : : AttachSharedMemoryStructs();
736 : : #endif
10841 scrappy@hub.org 737 : 4579 : }
738 : :
739 : : /*
740 : : * Used from bufmgr to share the value of the buffer that Startup waits on,
741 : : * or to reset the value to "not waiting" (-1). This allows processing
742 : : * of recovery conflicts for buffer pins. Set is made before backends look
743 : : * at this value, so locking not required, especially since the set is
744 : : * an atomic integer set operation.
745 : : */
746 : : void
5895 simon@2ndQuadrant.co 747 : 20 : SetStartupBufferPinWaitBufId(int bufid)
748 : : {
749 : : /* use volatile pointer to prevent code rearrangement */
750 : 20 : volatile PROC_HDR *procglobal = ProcGlobal;
751 : :
752 : 20 : procglobal->startupBufferPinWaitBufId = bufid;
753 : 20 : }
754 : :
755 : : /*
756 : : * Used by backends when they receive a request to check for buffer pin waits.
757 : : */
758 : : int
759 : 4 : GetStartupBufferPinWaitBufId(void)
760 : : {
761 : : /* use volatile pointer to prevent code rearrangement */
762 : 4 : volatile PROC_HDR *procglobal = ProcGlobal;
763 : :
5339 tgl@sss.pgh.pa.us 764 : 4 : return procglobal->startupBufferPinWaitBufId;
765 : : }
766 : :
767 : : /*
768 : : * Check whether there are at least N free PGPROC objects. If false is
769 : : * returned, *nfree will be set to the number of free PGPROC objects.
770 : : * Otherwise, *nfree will be set to n.
771 : : *
772 : : * Note: this is designed on the assumption that N will generally be small.
773 : : */
774 : : bool
1150 rhaas@postgresql.org 775 : 378 : HaveNFreeProcs(int n, int *nfree)
776 : : {
777 : : dlist_iter iter;
778 : :
779 [ - + ]: 378 : Assert(n > 0);
780 [ - + ]: 378 : Assert(nfree);
781 : :
32 heikki.linnakangas@i 782 :GNC 378 : SpinLockAcquire(&ProcGlobal->freeProcsLock);
783 : :
1150 rhaas@postgresql.org 784 :CBC 378 : *nfree = 0;
1152 andres@anarazel.de 785 [ + - + + ]: 1132 : dlist_foreach(iter, &ProcGlobal->freeProcs)
786 : : {
1150 rhaas@postgresql.org 787 : 1130 : (*nfree)++;
788 [ + + ]: 1130 : if (*nfree == n)
1152 andres@anarazel.de 789 : 376 : break;
790 : : }
791 : :
32 heikki.linnakangas@i 792 :GNC 378 : SpinLockRelease(&ProcGlobal->freeProcsLock);
793 : :
1150 rhaas@postgresql.org 794 :CBC 378 : return (*nfree == n);
795 : : }
796 : :
797 : : /*
798 : : * Cancel any pending wait for lock, when aborting a transaction, and revert
799 : : * any strong lock count acquisition for a lock being acquired.
800 : : *
801 : : * (Normally, this would only happen if we accept a cancel/die
802 : : * interrupt while waiting; but an ereport(ERROR) before or during the lock
803 : : * wait is within the realm of possibility, too.)
804 : : */
805 : : void
5079 806 : 370071 : LockErrorCleanup(void)
807 : : {
808 : : LOCALLOCK *lockAwaited;
809 : : LWLock *partitionLock;
810 : : DisableTimeoutParams timeouts[2];
811 : :
4059 heikki.linnakangas@i 812 : 370071 : HOLD_INTERRUPTS();
813 : :
5079 rhaas@postgresql.org 814 : 370071 : AbortStrongLockAcquire();
815 : :
816 : : /* Nothing to do if we weren't waiting for a lock */
496 heikki.linnakangas@i 817 : 370071 : lockAwaited = GetAwaitedLock();
7399 tgl@sss.pgh.pa.us 818 [ + + ]: 370071 : if (lockAwaited == NULL)
819 : : {
4059 heikki.linnakangas@i 820 [ - + ]: 370032 : RESUME_INTERRUPTS();
6623 tgl@sss.pgh.pa.us 821 : 370032 : return;
822 : : }
823 : :
824 : : /*
825 : : * Turn off the deadlock and lock timeout timers, if they are still
826 : : * running (see ProcSleep). Note we must preserve the LOCK_TIMEOUT
827 : : * indicator flag, since this function is executed before
828 : : * ProcessInterrupts when responding to SIGINT; else we'd lose the
829 : : * knowledge that the SIGINT came from a lock timeout and not an external
830 : : * source.
831 : : */
4747 832 : 39 : timeouts[0].id = DEADLOCK_TIMEOUT;
833 : 39 : timeouts[0].keep_indicator = false;
834 : 39 : timeouts[1].id = LOCK_TIMEOUT;
835 : 39 : timeouts[1].keep_indicator = true;
836 : 39 : disable_timeouts(timeouts, 2);
837 : :
838 : : /* Unlink myself from the wait queue, if on it (might not be anymore!) */
7175 839 : 39 : partitionLock = LockHashPartitionLock(lockAwaited->hashcode);
7399 840 : 39 : LWLockAcquire(partitionLock, LW_EXCLUSIVE);
841 : :
23 heikki.linnakangas@i 842 [ + - ]:GNC 39 : if (!dlist_node_is_detached(&MyProc->waitLink))
843 : : {
844 : : /* We could not have been granted the lock yet */
7175 tgl@sss.pgh.pa.us 845 :CBC 39 : RemoveFromWaitQueue(MyProc, lockAwaited->hashcode);
846 : : }
847 : : else
848 : : {
849 : : /*
850 : : * Somebody kicked us off the lock queue already. Perhaps they
851 : : * granted us the lock, or perhaps they detected a deadlock. If they
852 : : * did grant us the lock, we'd better remember it in our local lock
853 : : * table.
854 : : */
2097 peter@eisentraut.org 855 [ # # ]:LBC (1) : if (MyProc->waitStatus == PROC_WAIT_STATUS_OK)
7870 tgl@sss.pgh.pa.us 856 : (1) : GrantAwaitedLock();
857 : : }
858 : :
352 heikki.linnakangas@i 859 :CBC 39 : ResetAwaitedLock();
860 : :
7399 tgl@sss.pgh.pa.us 861 : 39 : LWLockRelease(partitionLock);
862 : :
4059 heikki.linnakangas@i 863 [ - + ]: 39 : RESUME_INTERRUPTS();
864 : : }
865 : :
866 : :
867 : : /*
868 : : * ProcReleaseLocks() -- release locks associated with current transaction
869 : : * at main transaction commit or abort
870 : : *
871 : : * At main transaction commit, we release standard locks except session locks.
872 : : * At main transaction abort, we release all locks including session locks.
873 : : *
874 : : * Advisory locks are released only if they are transaction-level;
875 : : * session-level holds remain, whether this is a commit or not.
876 : : *
877 : : * At subtransaction commit, we don't release any locks (so this func is not
878 : : * needed at all); we will defer the releasing to the parent transaction.
879 : : * At subtransaction abort, we release all locks held by the subtransaction;
880 : : * this is implemented by retail releasing of the locks under control of
881 : : * the ResourceOwner mechanism.
882 : : */
883 : : void
7911 tgl@sss.pgh.pa.us 884 : 337824 : ProcReleaseLocks(bool isCommit)
885 : : {
10416 bruce@momjian.us 886 [ - + ]: 337824 : if (!MyProc)
10416 bruce@momjian.us 887 :UBC 0 : return;
888 : : /* If waiting, get off wait queue (should only be needed after error) */
5079 rhaas@postgresql.org 889 :CBC 337824 : LockErrorCleanup();
890 : : /* Release standard locks, including session-level if aborting */
7870 tgl@sss.pgh.pa.us 891 : 337824 : LockReleaseAll(DEFAULT_LOCKMETHOD, !isCommit);
892 : : /* Release transaction-level advisory locks */
5504 itagaki.takahiro@gma 893 : 337824 : LockReleaseAll(USER_LOCKMETHOD, false);
894 : : }
895 : :
896 : :
897 : : /*
898 : : * RemoveProcFromArray() -- Remove this process from the shared ProcArray.
899 : : */
900 : : static void
7375 tgl@sss.pgh.pa.us 901 : 16976 : RemoveProcFromArray(int code, Datum arg)
902 : : {
903 [ - + ]: 16976 : Assert(MyProc != NULL);
6763 904 : 16976 : ProcArrayRemove(MyProc, InvalidTransactionId);
7375 905 : 16976 : }
906 : :
907 : : /*
908 : : * ProcKill() -- Destroy the per-proc data structure for
909 : : * this process. Release any of its held LW locks.
910 : : */
911 : : static void
8129 peter_e@gmx.net 912 : 16985 : ProcKill(int code, Datum arg)
913 : : {
914 : : PGPROC *proc;
915 : : dlist_head *procgloballist;
916 : :
8955 tgl@sss.pgh.pa.us 917 [ - + ]: 16985 : Assert(MyProc != NULL);
918 : :
919 : : /* not safe if forked by system(), etc. */
880 nathan@postgresql.or 920 [ - + ]: 16985 : if (MyProc->pid != (int) getpid())
880 nathan@postgresql.or 921 [ # # ]:UBC 0 : elog(PANIC, "ProcKill() called in child process");
922 : :
923 : : /* Make sure we're out of the sync rep lists */
5331 tgl@sss.pgh.pa.us 924 :CBC 16985 : SyncRepCleanupAtProcExit();
925 : :
926 : : #ifdef USE_ASSERT_CHECKING
927 : : {
928 : : int i;
929 : :
930 : : /* Last process should have released all locks. */
5248 rhaas@postgresql.org 931 [ + + ]: 288745 : for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
1152 andres@anarazel.de 932 [ - + ]: 271760 : Assert(dlist_is_empty(&(MyProc->myProcLocks[i])));
933 : : }
934 : : #endif
935 : :
936 : : /*
937 : : * Release any LW locks I am holding. There really shouldn't be any, but
938 : : * it's cheap to check again before we cut the knees off the LWLock
939 : : * facility by releasing our PGPROC ...
940 : : */
7524 tgl@sss.pgh.pa.us 941 : 16985 : LWLockReleaseAll();
942 : :
943 : : /*
944 : : * Cleanup waiting for LSN if any.
945 : : */
130 akorotkov@postgresql 946 :GNC 16985 : WaitLSNCleanup();
947 : :
948 : : /* Cancel any pending condition variable sleep, too */
3400 rhaas@postgresql.org 949 :CBC 16985 : ConditionVariableCancelSleep();
950 : :
951 : : /*
952 : : * Detach from any lock group of which we are a member. If the leader
953 : : * exits before all other group members, its PGPROC will remain allocated
954 : : * until the last group process exits; that process must return the
955 : : * leader's PGPROC to the appropriate list.
956 : : */
3689 957 [ + + ]: 16985 : if (MyProc->lockGroupLeader != NULL)
958 : : {
959 : 1576 : PGPROC *leader = MyProc->lockGroupLeader;
960 : 1576 : LWLock *leader_lwlock = LockHashPartitionLockByProc(leader);
961 : :
962 : 1576 : LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
963 [ - + ]: 1576 : Assert(!dlist_is_empty(&leader->lockGroupMembers));
964 : 1576 : dlist_delete(&MyProc->lockGroupLink);
965 [ + + ]: 1576 : if (dlist_is_empty(&leader->lockGroupMembers))
966 : : {
967 : 85 : leader->lockGroupLeader = NULL;
968 [ - + ]: 85 : if (leader != MyProc)
969 : : {
3689 rhaas@postgresql.org 970 :UBC 0 : procgloballist = leader->procgloballist;
971 : :
972 : : /* Leader exited first; return its PGPROC. */
32 heikki.linnakangas@i 973 :UNC 0 : SpinLockAcquire(&ProcGlobal->freeProcsLock);
23 974 : 0 : dlist_push_head(procgloballist, &leader->freeProcsLink);
32 975 : 0 : SpinLockRelease(&ProcGlobal->freeProcsLock);
976 : : }
977 : : }
3689 rhaas@postgresql.org 978 [ + - ]:CBC 1491 : else if (leader != MyProc)
979 : 1491 : MyProc->lockGroupLeader = NULL;
980 : 1576 : LWLockRelease(leader_lwlock);
981 : : }
982 : :
983 : : /*
984 : : * Reset MyLatch to the process local one. This is so that signal
985 : : * handlers et al can continue using the latch after the shared latch
986 : : * isn't ours anymore.
987 : : *
988 : : * Similarly, stop reporting wait events to MyProc->wait_event_info.
989 : : *
990 : : * After that clear MyProc and disown the shared latch.
991 : : */
4078 andres@anarazel.de 992 : 16985 : SwitchBackToLocalLatch();
1807 993 : 16985 : pgstat_reset_wait_event_storage();
994 : :
4426 rhaas@postgresql.org 995 : 16985 : proc = MyProc;
996 : 16985 : MyProc = NULL;
742 heikki.linnakangas@i 997 : 16985 : MyProcNumber = INVALID_PROC_NUMBER;
4426 rhaas@postgresql.org 998 : 16985 : DisownLatch(&proc->procLatch);
999 : :
1000 : : /* Mark the proc no longer in use */
742 heikki.linnakangas@i 1001 : 16985 : proc->pid = 0;
1002 : 16985 : proc->vxid.procNumber = INVALID_PROC_NUMBER;
1003 : 16985 : proc->vxid.lxid = InvalidTransactionId;
1004 : :
3883 rhaas@postgresql.org 1005 : 16985 : procgloballist = proc->procgloballist;
32 heikki.linnakangas@i 1006 :GNC 16985 : SpinLockAcquire(&ProcGlobal->freeProcsLock);
1007 : :
1008 : : /*
1009 : : * If we're still a member of a locking group, that means we're a leader
1010 : : * which has somehow exited before its children. The last remaining child
1011 : : * will release our PGPROC. Otherwise, release it now.
1012 : : */
3689 rhaas@postgresql.org 1013 [ + - ]:CBC 16985 : if (proc->lockGroupLeader == NULL)
1014 : : {
1015 : : /* Since lockGroupLeader is NULL, lockGroupMembers should be empty. */
1016 [ - + ]: 16985 : Assert(dlist_is_empty(&proc->lockGroupMembers));
1017 : :
1018 : : /* Return PGPROC structure (and semaphore) to appropriate freelist */
23 heikki.linnakangas@i 1019 :GNC 16985 : dlist_push_tail(procgloballist, &proc->freeProcsLink);
1020 : : }
1021 : :
1022 : : /* Update shared estimate of spins_per_delay */
3803 rhaas@postgresql.org 1023 :CBC 16985 : ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
1024 : :
32 heikki.linnakangas@i 1025 :GNC 16985 : SpinLockRelease(&ProcGlobal->freeProcsLock);
8933 tgl@sss.pgh.pa.us 1026 :CBC 16985 : }
1027 : :
1028 : : /*
1029 : : * AuxiliaryProcKill() -- Cut-down version of ProcKill for auxiliary
1030 : : * processes (bgwriter, etc). The PGPROC and sema are not released, only
1031 : : * marked as not-in-use.
1032 : : */
1033 : : static void
6948 alvherre@alvh.no-ip. 1034 : 4579 : AuxiliaryProcKill(int code, Datum arg)
1035 : : {
7868 bruce@momjian.us 1036 : 4579 : int proctype = DatumGetInt32(arg);
1037 : : PGPROC *auxproc PG_USED_FOR_ASSERTS_ONLY;
1038 : : PGPROC *proc;
1039 : :
6948 alvherre@alvh.no-ip. 1040 [ + - - + ]: 4579 : Assert(proctype >= 0 && proctype < NUM_AUXILIARY_PROCS);
1041 : :
1042 : : /* not safe if forked by system(), etc. */
880 nathan@postgresql.or 1043 [ - + ]: 4579 : if (MyProc->pid != (int) getpid())
880 nathan@postgresql.or 1044 [ # # ]:UBC 0 : elog(PANIC, "AuxiliaryProcKill() called in child process");
1045 : :
6948 alvherre@alvh.no-ip. 1046 :CBC 4579 : auxproc = &AuxiliaryProcs[proctype];
1047 : :
1048 [ - + ]: 4579 : Assert(MyProc == auxproc);
1049 : :
1050 : : /* Release any LW locks I am holding (see notes above) */
8933 tgl@sss.pgh.pa.us 1051 : 4579 : LWLockReleaseAll();
1052 : :
1053 : : /* Cancel any pending condition variable sleep, too */
3400 rhaas@postgresql.org 1054 : 4579 : ConditionVariableCancelSleep();
1055 : :
1056 : : /* look at the equivalent ProcKill() code for comments */
4078 andres@anarazel.de 1057 : 4579 : SwitchBackToLocalLatch();
1807 1058 : 4579 : pgstat_reset_wait_event_storage();
1059 : :
4426 rhaas@postgresql.org 1060 : 4579 : proc = MyProc;
1061 : 4579 : MyProc = NULL;
742 heikki.linnakangas@i 1062 : 4579 : MyProcNumber = INVALID_PROC_NUMBER;
4426 rhaas@postgresql.org 1063 : 4579 : DisownLatch(&proc->procLatch);
1064 : :
32 heikki.linnakangas@i 1065 :GNC 4579 : SpinLockAcquire(&ProcGlobal->freeProcsLock);
1066 : :
1067 : : /* Mark auxiliary proc no longer in use */
4426 rhaas@postgresql.org 1068 :CBC 4579 : proc->pid = 0;
742 heikki.linnakangas@i 1069 : 4579 : proc->vxid.procNumber = INVALID_PROC_NUMBER;
1070 : 4579 : proc->vxid.lxid = InvalidTransactionId;
1071 : :
1072 : : /* Update shared estimate of spins_per_delay */
7460 tgl@sss.pgh.pa.us 1073 : 4579 : ProcGlobal->spins_per_delay = update_spins_per_delay(ProcGlobal->spins_per_delay);
1074 : :
32 heikki.linnakangas@i 1075 :GNC 4579 : SpinLockRelease(&ProcGlobal->freeProcsLock);
10841 scrappy@hub.org 1076 :CBC 4579 : }
1077 : :
1078 : : /*
1079 : : * AuxiliaryPidGetProc -- get PGPROC for an auxiliary process
1080 : : * given its PID
1081 : : *
1082 : : * Returns NULL if not found.
1083 : : */
1084 : : PGPROC *
3276 rhaas@postgresql.org 1085 : 6031 : AuxiliaryPidGetProc(int pid)
1086 : : {
1087 : 6031 : PGPROC *result = NULL;
1088 : : int index;
1089 : :
1090 [ + + ]: 6031 : if (pid == 0) /* never match dummy PGPROCs */
1091 : 3 : return NULL;
1092 : :
1093 [ + - ]: 27082 : for (index = 0; index < NUM_AUXILIARY_PROCS; index++)
1094 : : {
1095 : 27082 : PGPROC *proc = &AuxiliaryProcs[index];
1096 : :
1097 [ + + ]: 27082 : if (proc->pid == pid)
1098 : : {
1099 : 6028 : result = proc;
1100 : 6028 : break;
1101 : : }
1102 : : }
1103 : 6028 : return result;
1104 : : }
1105 : :
1106 : :
1107 : : /*
1108 : : * JoinWaitQueue -- join the wait queue on the specified lock
1109 : : *
1110 : : * It's not actually guaranteed that we need to wait when this function is
1111 : : * called, because it could be that when we try to find a position at which
1112 : : * to insert ourself into the wait queue, we discover that we must be inserted
1113 : : * ahead of everyone who wants a lock that conflict with ours. In that case,
1114 : : * we get the lock immediately. Because of this, it's sensible for this function
1115 : : * to have a dontWait argument, despite the name.
1116 : : *
1117 : : * On entry, the caller has already set up LOCK and PROCLOCK entries to
1118 : : * reflect that we have "requested" the lock. The caller is responsible for
1119 : : * cleaning that up, if we end up not joining the queue after all.
1120 : : *
1121 : : * The lock table's partition lock must be held at entry, and is still held
1122 : : * at exit. The caller must release it before calling ProcSleep().
1123 : : *
1124 : : * Result is one of the following:
1125 : : *
1126 : : * PROC_WAIT_STATUS_OK - lock was immediately granted
1127 : : * PROC_WAIT_STATUS_WAITING - joined the wait queue; call ProcSleep()
1128 : : * PROC_WAIT_STATUS_ERROR - immediate deadlock was detected, or would
1129 : : * need to wait and dontWait == true
1130 : : *
1131 : : * NOTES: The process queue is now a priority queue for locking.
1132 : : */
1133 : : ProcWaitStatus
496 heikki.linnakangas@i 1134 : 2165 : JoinWaitQueue(LOCALLOCK *locallock, LockMethod lockMethodTable, bool dontWait)
1135 : : {
7399 tgl@sss.pgh.pa.us 1136 : 2165 : LOCKMODE lockmode = locallock->tag.mode;
1137 : 2165 : LOCK *lock = locallock->lock;
1138 : 2165 : PROCLOCK *proclock = locallock->proclock;
7175 1139 : 2165 : uint32 hashcode = locallock->hashcode;
496 heikki.linnakangas@i 1140 : 2165 : LWLock *partitionLock PG_USED_FOR_ASSERTS_ONLY = LockHashPartitionLock(hashcode);
1152 andres@anarazel.de 1141 : 2165 : dclist_head *waitQueue = &lock->waitProcs;
1031 tgl@sss.pgh.pa.us 1142 : 2165 : PGPROC *insert_before = NULL;
1143 : : LOCKMASK myProcHeldLocks;
1144 : : LOCKMASK myHeldLocks;
8958 1145 : 2165 : bool early_deadlock = false;
3689 rhaas@postgresql.org 1146 : 2165 : PGPROC *leader = MyProc->lockGroupLeader;
1147 : :
496 heikki.linnakangas@i 1148 [ - + ]: 2165 : Assert(LWLockHeldByMeInMode(partitionLock, LW_EXCLUSIVE));
1149 : :
1150 : : /*
1151 : : * Set bitmask of locks this process already holds on this object.
1152 : : */
1153 : 2165 : myHeldLocks = MyProc->heldLocks = proclock->holdMask;
1154 : :
1155 : : /*
1156 : : * Determine which locks we're already holding.
1157 : : *
1158 : : * If group locking is in use, locks held by members of my locking group
1159 : : * need to be included in myHeldLocks. This is not required for relation
1160 : : * extension lock which conflict among group members. However, including
1161 : : * them in myHeldLocks will give group members the priority to get those
1162 : : * locks as compared to other backends which are also trying to acquire
1163 : : * those locks. OTOH, we can avoid giving priority to group members for
1164 : : * that kind of locks, but there doesn't appear to be a clear advantage of
1165 : : * the same.
1166 : : */
1167 : 2165 : myProcHeldLocks = proclock->holdMask;
1168 : 2165 : myHeldLocks = myProcHeldLocks;
3689 rhaas@postgresql.org 1169 [ + + ]: 2165 : if (leader != NULL)
1170 : : {
1171 : : dlist_iter iter;
1172 : :
1152 andres@anarazel.de 1173 [ + - + + ]: 44 : dlist_foreach(iter, &lock->procLocks)
1174 : : {
1175 : : PROCLOCK *otherproclock;
1176 : :
1177 : 33 : otherproclock = dlist_container(PROCLOCK, lockLink, iter.cur);
1178 : :
3689 rhaas@postgresql.org 1179 [ + + ]: 33 : if (otherproclock->groupLeader == leader)
1180 : 15 : myHeldLocks |= otherproclock->holdMask;
1181 : : }
1182 : : }
1183 : :
1184 : : /*
1185 : : * Determine where to add myself in the wait queue.
1186 : : *
1187 : : * Normally I should go at the end of the queue. However, if I already
1188 : : * hold locks that conflict with the request of any previous waiter, put
1189 : : * myself in the queue just in front of the first such waiter. This is not
1190 : : * a necessary step, since deadlock detection would move me to before that
1191 : : * waiter anyway; but it's relatively cheap to detect such a conflict
1192 : : * immediately, and avoid delaying till deadlock timeout.
1193 : : *
1194 : : * Special case: if I find I should go in front of some waiter, check to
1195 : : * see if I conflict with already-held locks or the requests before that
1196 : : * waiter. If not, then just grant myself the requested lock immediately.
1197 : : * This is the same as the test for immediate grant in LockAcquire, except
1198 : : * we are only considering the part of the wait queue before my insertion
1199 : : * point.
1200 : : */
1152 andres@anarazel.de 1201 [ + + + + ]: 2165 : if (myHeldLocks != 0 && !dclist_is_empty(waitQueue))
1202 : : {
8140 bruce@momjian.us 1203 : 6 : LOCKMASK aheadRequests = 0;
1204 : : dlist_iter iter;
1205 : :
1152 andres@anarazel.de 1206 [ + - + - ]: 6 : dclist_foreach(iter, waitQueue)
1207 : : {
23 heikki.linnakangas@i 1208 :GNC 6 : PGPROC *proc = dlist_container(PGPROC, waitLink, iter.cur);
1209 : :
1210 : : /*
1211 : : * If we're part of the same locking group as this waiter, its
1212 : : * locks neither conflict with ours nor contribute to
1213 : : * aheadRequests.
1214 : : */
3689 rhaas@postgresql.org 1215 [ - + - - ]:CBC 6 : if (leader != NULL && leader == proc->lockGroupLeader)
3689 rhaas@postgresql.org 1216 :UBC 0 : continue;
1217 : :
1218 : : /* Must he wait for me? */
8641 bruce@momjian.us 1219 [ + - ]:CBC 6 : if (lockMethodTable->conflictTab[proc->waitLockMode] & myHeldLocks)
1220 : : {
1221 : : /* Must I wait for him ? */
1222 [ + + ]: 6 : if (lockMethodTable->conflictTab[lockmode] & proc->heldLocks)
1223 : : {
1224 : : /*
1225 : : * Yes, so we have a deadlock. Easiest way to clean up
1226 : : * correctly is to call RemoveFromWaitQueue(), but we
1227 : : * can't do that until we are *on* the wait queue. So, set
1228 : : * a flag to check below, and break out of loop. Also,
1229 : : * record deadlock info for later message.
1230 : : */
8459 tgl@sss.pgh.pa.us 1231 : 1 : RememberSimpleDeadLock(MyProc, lockmode, lock, proc);
8958 1232 : 1 : early_deadlock = true;
1233 : 1 : break;
1234 : : }
1235 : : /* I must go before this waiter. Check special case. */
8641 bruce@momjian.us 1236 [ + - ]: 5 : if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
2268 peter@eisentraut.org 1237 [ + - ]: 5 : !LockCheckConflicts(lockMethodTable, lockmode, lock,
1238 : : proclock))
1239 : : {
1240 : : /* Skip the wait and just grant myself the lock. */
8426 bruce@momjian.us 1241 : 5 : GrantLock(lock, proclock, lockmode);
2097 peter@eisentraut.org 1242 : 5 : return PROC_WAIT_STATUS_OK;
1243 : : }
1244 : :
1245 : : /* Put myself into wait queue before conflicting process */
1152 andres@anarazel.de 1246 :UBC 0 : insert_before = proc;
9809 vadim4o@yahoo.com 1247 : 0 : break;
1248 : : }
1249 : : /* Nope, so advance to next waiter */
8140 bruce@momjian.us 1250 : 0 : aheadRequests |= LOCKBIT_ON(proc->waitLockMode);
1251 : : }
1252 : : }
1253 : :
1254 : : /*
1255 : : * If we detected deadlock, give up without waiting. This must agree with
1256 : : * CheckDeadLock's recovery code.
1257 : : */
496 heikki.linnakangas@i 1258 [ + + ]:CBC 2160 : if (early_deadlock)
1259 : 1 : return PROC_WAIT_STATUS_ERROR;
1260 : :
1261 : : /*
1262 : : * At this point we know that we'd really need to sleep. If we've been
1263 : : * commanded not to do that, bail out.
1264 : : */
731 rhaas@postgresql.org 1265 [ + + ]: 2159 : if (dontWait)
1266 : 739 : return PROC_WAIT_STATUS_ERROR;
1267 : :
1268 : : /*
1269 : : * Insert self into queue, at the position determined above.
1270 : : */
1152 andres@anarazel.de 1271 [ - + ]: 1420 : if (insert_before)
23 heikki.linnakangas@i 1272 :UNC 0 : dclist_insert_before(waitQueue, &insert_before->waitLink, &MyProc->waitLink);
1273 : : else
23 heikki.linnakangas@i 1274 :GNC 1420 : dclist_push_tail(waitQueue, &MyProc->waitLink);
1275 : :
8140 bruce@momjian.us 1276 :CBC 1420 : lock->waitMask |= LOCKBIT_ON(lockmode);
1277 : :
1278 : : /* Set up wait information in PGPROC object, too */
496 heikki.linnakangas@i 1279 : 1420 : MyProc->heldLocks = myProcHeldLocks;
9183 tgl@sss.pgh.pa.us 1280 : 1420 : MyProc->waitLock = lock;
7870 1281 : 1420 : MyProc->waitProcLock = proclock;
9183 1282 : 1420 : MyProc->waitLockMode = lockmode;
1283 : :
2097 peter@eisentraut.org 1284 : 1420 : MyProc->waitStatus = PROC_WAIT_STATUS_WAITING;
1285 : :
496 heikki.linnakangas@i 1286 : 1420 : return PROC_WAIT_STATUS_WAITING;
1287 : : }
1288 : :
1289 : : /*
1290 : : * ProcSleep -- put process to sleep waiting on lock
1291 : : *
1292 : : * This must be called when JoinWaitQueue() returns PROC_WAIT_STATUS_WAITING.
1293 : : * Returns after the lock has been granted, or if a deadlock is detected. Can
1294 : : * also bail out with ereport(ERROR), if some other error condition, or a
1295 : : * timeout or cancellation is triggered.
1296 : : *
1297 : : * Result is one of the following:
1298 : : *
1299 : : * PROC_WAIT_STATUS_OK - lock was granted
1300 : : * PROC_WAIT_STATUS_ERROR - a deadlock was detected
1301 : : */
1302 : : ProcWaitStatus
1303 : 1420 : ProcSleep(LOCALLOCK *locallock)
1304 : : {
1305 : 1420 : LOCKMODE lockmode = locallock->tag.mode;
1306 : 1420 : LOCK *lock = locallock->lock;
1307 : 1420 : uint32 hashcode = locallock->hashcode;
1308 : 1420 : LWLock *partitionLock = LockHashPartitionLock(hashcode);
1309 : 1420 : TimestampTz standbyWaitStart = 0;
1310 : 1420 : bool allow_autovacuum_cancel = true;
1311 : 1420 : bool logged_recovery_conflict = false;
1312 : : ProcWaitStatus myWaitStatus;
1313 : : DeadLockState deadlock_state;
1314 : :
1315 : : /* The caller must've armed the on-error cleanup mechanism */
1316 [ - + ]: 1420 : Assert(GetAwaitedLock() == locallock);
1317 [ - + ]: 1420 : Assert(!LWLockHeldByMe(partitionLock));
1318 : :
1319 : : /*
1320 : : * Now that we will successfully clean up after an ereport, it's safe to
1321 : : * check to see if there's a buffer pin deadlock against the Startup
1322 : : * process. Of course, that's only necessary if we're doing Hot Standby
1323 : : * and are not the Startup process ourselves.
1324 : : */
5339 tgl@sss.pgh.pa.us 1325 [ + + + + ]: 1420 : if (RecoveryInProgress() && !InRecovery)
1326 : 1 : CheckRecoveryConflictDeadlock();
1327 : :
1328 : : /* Reset deadlock_state before enabling the timeout handler */
6844 1329 : 1420 : deadlock_state = DS_NOT_YET_CHECKED;
4058 andres@anarazel.de 1330 : 1420 : got_deadlock_timeout = false;
1331 : :
1332 : : /*
1333 : : * Set timer so we can wake up after awhile and check for a deadlock. If a
1334 : : * deadlock is detected, the handler sets MyProc->waitStatus =
1335 : : * PROC_WAIT_STATUS_ERROR, allowing us to know that we must report failure
1336 : : * rather than success.
1337 : : *
1338 : : * By delaying the check until we've waited for a bit, we can avoid
1339 : : * running the rather expensive deadlock-check code in most cases.
1340 : : *
1341 : : * If LockTimeout is set, also enable the timeout for that. We can save a
1342 : : * few cycles by enabling both timeout sources in one call.
1343 : : *
1344 : : * If InHotStandby we set lock waits slightly later for clarity with other
1345 : : * code.
1346 : : */
3657 simon@2ndQuadrant.co 1347 [ + + ]: 1420 : if (!InHotStandby)
1348 : : {
1349 [ + + ]: 1419 : if (LockTimeout > 0)
1350 : : {
1351 : : EnableTimeoutParams timeouts[2];
1352 : :
1353 : 98 : timeouts[0].id = DEADLOCK_TIMEOUT;
1354 : 98 : timeouts[0].type = TMPARAM_AFTER;
1355 : 98 : timeouts[0].delay_ms = DeadlockTimeout;
1356 : 98 : timeouts[1].id = LOCK_TIMEOUT;
1357 : 98 : timeouts[1].type = TMPARAM_AFTER;
1358 : 98 : timeouts[1].delay_ms = LockTimeout;
1359 : 98 : enable_timeouts(timeouts, 2);
1360 : : }
1361 : : else
1362 : 1321 : enable_timeout_after(DEADLOCK_TIMEOUT, DeadlockTimeout);
1363 : :
1364 : : /*
1365 : : * Use the current time obtained for the deadlock timeout timer as
1366 : : * waitStart (i.e., the time when this process started waiting for the
1367 : : * lock). Since getting the current time newly can cause overhead, we
1368 : : * reuse the already-obtained time to avoid that overhead.
1369 : : *
1370 : : * Note that waitStart is updated without holding the lock table's
1371 : : * partition lock, to avoid the overhead by additional lock
1372 : : * acquisition. This can cause "waitstart" in pg_locks to become NULL
1373 : : * for a very short period of time after the wait started even though
1374 : : * "granted" is false. This is OK in practice because we can assume
1375 : : * that users are likely to look at "waitstart" when waiting for the
1376 : : * lock for a long time.
1377 : : */
1854 fujii@postgresql.org 1378 : 1419 : pg_atomic_write_u64(&MyProc->waitStart,
1379 : 1419 : get_timeout_start_time(DEADLOCK_TIMEOUT));
1380 : : }
1892 1381 [ + - ]: 1 : else if (log_recovery_conflict_waits)
1382 : : {
1383 : : /*
1384 : : * Set the wait start timestamp if logging is enabled and in hot
1385 : : * standby.
1386 : : */
1387 : 1 : standbyWaitStart = GetCurrentTimestamp();
1388 : : }
1389 : :
1390 : : /*
1391 : : * If somebody wakes us between LWLockRelease and WaitLatch, the latch
1392 : : * will not wait. But a set latch does not necessarily mean that the lock
1393 : : * is free now, as there are many other sources for latch sets than
1394 : : * somebody releasing the lock.
1395 : : *
1396 : : * We process interrupts whenever the latch has been set, so cancel/die
1397 : : * interrupts are processed quickly. This means we must not mind losing
1398 : : * control to a cancel/die interrupt here. We don't, because we have no
1399 : : * shared-state-change work to do after being granted the lock (the
1400 : : * grantor did it all). We do have to worry about canceling the deadlock
1401 : : * timeout and updating the locallock table, but if we lose control to an
1402 : : * error, LockErrorCleanup will fix that up.
1403 : : */
1404 : : do
1405 : : {
3657 simon@2ndQuadrant.co 1406 [ + + ]: 1791 : if (InHotStandby)
1407 : : {
1892 fujii@postgresql.org 1408 : 3 : bool maybe_log_conflict =
1031 tgl@sss.pgh.pa.us 1409 [ + - + + ]: 3 : (standbyWaitStart != 0 && !logged_recovery_conflict);
1410 : :
1411 : : /* Set a timer and wait for that or for the lock to be granted */
1892 fujii@postgresql.org 1412 : 3 : ResolveRecoveryConflictWithLock(locallock->tag.lock,
1413 : : maybe_log_conflict);
1414 : :
1415 : : /*
1416 : : * Emit the log message if the startup process is waiting longer
1417 : : * than deadlock_timeout for recovery conflict on lock.
1418 : : */
1419 [ + + ]: 3 : if (maybe_log_conflict)
1420 : : {
1421 : 1 : TimestampTz now = GetCurrentTimestamp();
1422 : :
1423 [ + - ]: 1 : if (TimestampDifferenceExceeds(standbyWaitStart, now,
1424 : : DeadlockTimeout))
1425 : : {
1426 : : VirtualTransactionId *vxids;
1427 : : int cnt;
1428 : :
1429 : 1 : vxids = GetLockConflicts(&locallock->tag.lock,
1430 : : AccessExclusiveLock, &cnt);
1431 : :
1432 : : /*
1433 : : * Log the recovery conflict and the list of PIDs of
1434 : : * backends holding the conflicting lock. Note that we do
1435 : : * logging even if there are no such backends right now
1436 : : * because the startup process here has already waited
1437 : : * longer than deadlock_timeout.
1438 : : */
33 heikki.linnakangas@i 1439 :GNC 1 : LogRecoveryConflict(RECOVERY_CONFLICT_LOCK,
1440 : : standbyWaitStart, now,
1887 fujii@postgresql.org 1441 [ + - ]:CBC 1 : cnt > 0 ? vxids : NULL, true);
1892 1442 : 1 : logged_recovery_conflict = true;
1443 : : }
1444 : : }
1445 : : }
1446 : : else
1447 : : {
2669 tmunro@postgresql.or 1448 : 1788 : (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
1449 : 1788 : PG_WAIT_LOCK | locallock->tag.lock.locktag_type);
3657 simon@2ndQuadrant.co 1450 : 1788 : ResetLatch(MyLatch);
1451 : : /* check for deadlocks first, as that's probably log-worthy */
1452 [ + + ]: 1788 : if (got_deadlock_timeout)
1453 : : {
34 heikki.linnakangas@i 1454 :GNC 32 : deadlock_state = CheckDeadLock();
3657 simon@2ndQuadrant.co 1455 :CBC 32 : got_deadlock_timeout = false;
1456 : : }
1457 [ + + ]: 1788 : CHECK_FOR_INTERRUPTS();
1458 : : }
1459 : :
1460 : : /*
1461 : : * waitStatus could change from PROC_WAIT_STATUS_WAITING to something
1462 : : * else asynchronously. Read it just once per loop to prevent
1463 : : * surprising behavior (such as missing log messages).
1464 : : */
2097 peter@eisentraut.org 1465 : 1750 : myWaitStatus = *((volatile ProcWaitStatus *) &MyProc->waitStatus);
1466 : :
1467 : : /*
1468 : : * If we are not deadlocked, but are waiting on an autovacuum-induced
1469 : : * task, send a signal to interrupt it.
1470 : : */
6715 alvherre@alvh.no-ip. 1471 [ - + - - ]: 1750 : if (deadlock_state == DS_BLOCKED_BY_AUTOVACUUM && allow_autovacuum_cancel)
1472 : : {
6695 bruce@momjian.us 1473 :UBC 0 : PGPROC *autovac = GetBlockingAutoVacuumPgproc();
1474 : : uint8 statusFlags;
1475 : : uint8 lockmethod_copy;
1476 : : LOCKTAG locktag_copy;
1477 : :
1478 : : /*
1479 : : * Grab info we need, then release lock immediately. Note this
1480 : : * coding means that there is a tiny chance that the process
1481 : : * terminates its current transaction and starts a different one
1482 : : * before we have a change to send the signal; the worst possible
1483 : : * consequence is that a for-wraparound vacuum is canceled. But
1484 : : * that could happen in any case unless we were to do kill() with
1485 : : * the lock held, which is much more undesirable.
1486 : : */
6715 alvherre@alvh.no-ip. 1487 : 0 : LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1938 1488 : 0 : statusFlags = ProcGlobal->statusFlags[autovac->pgxactoff];
1489 : 0 : lockmethod_copy = lock->tag.locktag_lockmethodid;
1490 : 0 : locktag_copy = lock->tag;
1491 : 0 : LWLockRelease(ProcArrayLock);
1492 : :
1493 : : /*
1494 : : * Only do it if the worker is not working to protect against Xid
1495 : : * wraparound.
1496 : : */
1945 1497 [ # # ]: 0 : if ((statusFlags & PROC_IS_AUTOVACUUM) &&
1498 [ # # ]: 0 : !(statusFlags & PROC_VACUUM_FOR_WRAPAROUND))
1499 : : {
6695 bruce@momjian.us 1500 : 0 : int pid = autovac->pid;
1501 : :
1502 : : /* report the case, if configured to do so */
1938 tgl@sss.pgh.pa.us 1503 [ # # ]: 0 : if (message_level_is_interesting(DEBUG1))
1504 : : {
1505 : : StringInfoData locktagbuf;
1506 : : StringInfoData logbuf; /* errdetail for server log */
1507 : :
1508 : 0 : initStringInfo(&locktagbuf);
1509 : 0 : initStringInfo(&logbuf);
1510 : 0 : DescribeLockTag(&locktagbuf, &locktag_copy);
1511 : 0 : appendStringInfo(&logbuf,
1512 : : "Process %d waits for %s on %s.",
1513 : : MyProcPid,
1514 : : GetLockmodeName(lockmethod_copy, lockmode),
1515 : : locktagbuf.data);
1516 : :
1517 [ # # ]: 0 : ereport(DEBUG1,
1518 : : (errmsg_internal("sending cancel to blocking autovacuum PID %d",
1519 : : pid),
1520 : : errdetail_log("%s", logbuf.data)));
1521 : :
1522 : 0 : pfree(locktagbuf.data);
1523 : 0 : pfree(logbuf.data);
1524 : : }
1525 : :
1526 : : /* send the autovacuum worker Back to Old Kent Road */
6715 alvherre@alvh.no-ip. 1527 [ # # ]: 0 : if (kill(pid, SIGINT) < 0)
1528 : : {
1529 : : /*
1530 : : * There's a race condition here: once we release the
1531 : : * ProcArrayLock, it's possible for the autovac worker to
1532 : : * close up shop and exit before we can do the kill().
1533 : : * Therefore, we do not whinge about no-such-process.
1534 : : * Other errors such as EPERM could conceivably happen if
1535 : : * the kernel recycles the PID fast enough, but such cases
1536 : : * seem improbable enough that it's probably best to issue
1537 : : * a warning if we see some other errno.
1538 : : */
3883 tgl@sss.pgh.pa.us 1539 [ # # ]: 0 : if (errno != ESRCH)
1540 [ # # ]: 0 : ereport(WARNING,
1541 : : (errmsg("could not send signal to process %d: %m",
1542 : : pid)));
1543 : : }
1544 : : }
1545 : :
1546 : : /* prevent signal from being sent again more than once */
6715 alvherre@alvh.no-ip. 1547 : 0 : allow_autovacuum_cancel = false;
1548 : : }
1549 : :
1550 : : /*
1551 : : * If awoken after the deadlock check interrupt has run, and
1552 : : * log_lock_waits is on, then report about the wait.
1553 : : */
6774 tgl@sss.pgh.pa.us 1554 [ + - + + ]:CBC 1750 : if (log_lock_waits && deadlock_state != DS_NOT_YET_CHECKED)
1555 : : {
1556 : : StringInfoData buf,
1557 : : lock_waiters_sbuf,
1558 : : lock_holders_sbuf;
1559 : : const char *modename;
1560 : : long secs;
1561 : : int usecs;
1562 : : long msecs;
4385 fujii@postgresql.org 1563 : 286 : int lockHoldersNum = 0;
1564 : :
6774 tgl@sss.pgh.pa.us 1565 : 286 : initStringInfo(&buf);
4385 fujii@postgresql.org 1566 : 286 : initStringInfo(&lock_waiters_sbuf);
1567 : 286 : initStringInfo(&lock_holders_sbuf);
1568 : :
6774 tgl@sss.pgh.pa.us 1569 : 286 : DescribeLockTag(&buf, &locallock->tag.lock);
1570 : 286 : modename = GetLockmodeName(locallock->tag.lock.locktag_lockmethodid,
1571 : : lockmode);
4990 alvherre@alvh.no-ip. 1572 : 286 : TimestampDifference(get_timeout_start_time(DEADLOCK_TIMEOUT),
1573 : : GetCurrentTimestamp(),
1574 : : &secs, &usecs);
6774 tgl@sss.pgh.pa.us 1575 : 286 : msecs = secs * 1000 + usecs / 1000;
1576 : 286 : usecs = usecs % 1000;
1577 : :
1578 : : /* Gather a list of all lock holders and waiters */
4385 fujii@postgresql.org 1579 : 286 : LWLockAcquire(partitionLock, LW_SHARED);
366 1580 : 286 : GetLockHoldersAndWaiters(locallock, &lock_holders_sbuf,
1581 : : &lock_waiters_sbuf, &lockHoldersNum);
4385 1582 : 286 : LWLockRelease(partitionLock);
1583 : :
6774 tgl@sss.pgh.pa.us 1584 [ + + ]: 286 : if (deadlock_state == DS_SOFT_DEADLOCK)
1585 [ + - ]: 3 : ereport(LOG,
1586 : : (errmsg("process %d avoided deadlock for %s on %s by rearranging queue order after %ld.%03d ms",
1587 : : MyProcPid, modename, buf.data, msecs, usecs),
1588 : : (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1589 : : "Processes holding the lock: %s. Wait queue: %s.",
1590 : : lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1591 [ + + ]: 283 : else if (deadlock_state == DS_HARD_DEADLOCK)
1592 : : {
1593 : : /*
1594 : : * This message is a bit redundant with the error that will be
1595 : : * reported subsequently, but in some cases the error report
1596 : : * might not make it to the log (eg, if it's caught by an
1597 : : * exception handler), and we want to ensure all long-wait
1598 : : * events get logged.
1599 : : */
1600 [ + - ]: 5 : ereport(LOG,
1601 : : (errmsg("process %d detected deadlock while waiting for %s on %s after %ld.%03d ms",
1602 : : MyProcPid, modename, buf.data, msecs, usecs),
1603 : : (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1604 : : "Processes holding the lock: %s. Wait queue: %s.",
1605 : : lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1606 : : }
1607 : :
2097 peter@eisentraut.org 1608 [ + + ]: 286 : if (myWaitStatus == PROC_WAIT_STATUS_WAITING)
6774 tgl@sss.pgh.pa.us 1609 [ + - ]: 257 : ereport(LOG,
1610 : : (errmsg("process %d still waiting for %s on %s after %ld.%03d ms",
1611 : : MyProcPid, modename, buf.data, msecs, usecs),
1612 : : (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1613 : : "Processes holding the lock: %s. Wait queue: %s.",
1614 : : lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
2097 peter@eisentraut.org 1615 [ + + ]: 29 : else if (myWaitStatus == PROC_WAIT_STATUS_OK)
6774 tgl@sss.pgh.pa.us 1616 [ + - ]: 24 : ereport(LOG,
1617 : : (errmsg("process %d acquired %s on %s after %ld.%03d ms",
1618 : : MyProcPid, modename, buf.data, msecs, usecs)));
1619 : : else
1620 : : {
2097 peter@eisentraut.org 1621 [ - + ]: 5 : Assert(myWaitStatus == PROC_WAIT_STATUS_ERROR);
1622 : :
1623 : : /*
1624 : : * Currently, the deadlock checker always kicks its own
1625 : : * process, which means that we'll only see
1626 : : * PROC_WAIT_STATUS_ERROR when deadlock_state ==
1627 : : * DS_HARD_DEADLOCK, and there's no need to print redundant
1628 : : * messages. But for completeness and future-proofing, print
1629 : : * a message if it looks like someone else kicked us off the
1630 : : * lock.
1631 : : */
6774 tgl@sss.pgh.pa.us 1632 [ - + ]: 5 : if (deadlock_state != DS_HARD_DEADLOCK)
6774 tgl@sss.pgh.pa.us 1633 [ # # ]:UBC 0 : ereport(LOG,
1634 : : (errmsg("process %d failed to acquire %s on %s after %ld.%03d ms",
1635 : : MyProcPid, modename, buf.data, msecs, usecs),
1636 : : (errdetail_log_plural("Process holding the lock: %s. Wait queue: %s.",
1637 : : "Processes holding the lock: %s. Wait queue: %s.",
1638 : : lockHoldersNum, lock_holders_sbuf.data, lock_waiters_sbuf.data))));
1639 : : }
1640 : :
1641 : : /*
1642 : : * At this point we might still need to wait for the lock. Reset
1643 : : * state so we don't print the above messages again.
1644 : : */
6774 tgl@sss.pgh.pa.us 1645 :CBC 286 : deadlock_state = DS_NO_DEADLOCK;
1646 : :
1647 : 286 : pfree(buf.data);
4385 fujii@postgresql.org 1648 : 286 : pfree(lock_holders_sbuf.data);
1649 : 286 : pfree(lock_waiters_sbuf.data);
1650 : : }
2097 peter@eisentraut.org 1651 [ + + ]: 1750 : } while (myWaitStatus == PROC_WAIT_STATUS_WAITING);
1652 : :
1653 : : /*
1654 : : * Disable the timers, if they are still running. As in LockErrorCleanup,
1655 : : * we must preserve the LOCK_TIMEOUT indicator flag: if a lock timeout has
1656 : : * already caused QueryCancelPending to become set, we want the cancel to
1657 : : * be reported as a lock timeout, not a user cancel.
1658 : : */
3657 simon@2ndQuadrant.co 1659 [ + + ]: 1379 : if (!InHotStandby)
1660 : : {
1661 [ + + ]: 1378 : if (LockTimeout > 0)
1662 : : {
1663 : : DisableTimeoutParams timeouts[2];
1664 : :
1665 : 92 : timeouts[0].id = DEADLOCK_TIMEOUT;
1666 : 92 : timeouts[0].keep_indicator = false;
1667 : 92 : timeouts[1].id = LOCK_TIMEOUT;
1668 : 92 : timeouts[1].keep_indicator = true;
1669 : 92 : disable_timeouts(timeouts, 2);
1670 : : }
1671 : : else
1672 : 1286 : disable_timeout(DEADLOCK_TIMEOUT, false);
1673 : : }
1674 : :
1675 : : /*
1676 : : * Emit the log message if recovery conflict on lock was resolved but the
1677 : : * startup process waited longer than deadlock_timeout for it.
1678 : : */
1887 fujii@postgresql.org 1679 [ + + + - ]: 1379 : if (InHotStandby && logged_recovery_conflict)
33 heikki.linnakangas@i 1680 :GNC 1 : LogRecoveryConflict(RECOVERY_CONFLICT_LOCK,
1681 : : standbyWaitStart, GetCurrentTimestamp(),
1682 : : NULL, false);
1683 : :
1684 : : /*
1685 : : * We don't have to do anything else, because the awaker did all the
1686 : : * necessary updates of the lock table and MyProc. (The caller is
1687 : : * responsible for updating the local lock table.)
1688 : : */
496 heikki.linnakangas@i 1689 :CBC 1379 : return myWaitStatus;
1690 : : }
1691 : :
1692 : :
1693 : : /*
1694 : : * ProcWakeup -- wake up a process by setting its latch.
1695 : : *
1696 : : * Also remove the process from the wait queue and set its waitLink invalid.
1697 : : *
1698 : : * The appropriate lock partition lock must be held by caller.
1699 : : *
1700 : : * XXX: presently, this code is only used for the "success" case, and only
1701 : : * works correctly for that case. To clean up in failure case, would need
1702 : : * to twiddle the lock's request counts too --- see RemoveFromWaitQueue.
1703 : : * Hence, in practice the waitStatus parameter must be PROC_WAIT_STATUS_OK.
1704 : : */
1705 : : void
2097 peter@eisentraut.org 1706 : 1376 : ProcWakeup(PGPROC *proc, ProcWaitStatus waitStatus)
1707 : : {
23 heikki.linnakangas@i 1708 [ - + ]:GNC 1376 : if (dlist_node_is_detached(&proc->waitLink))
1152 andres@anarazel.de 1709 :UBC 0 : return;
1710 : :
2097 peter@eisentraut.org 1711 [ - + ]:CBC 1376 : Assert(proc->waitStatus == PROC_WAIT_STATUS_WAITING);
1712 : :
1713 : : /* Remove process from wait queue */
23 heikki.linnakangas@i 1714 :GNC 1376 : dclist_delete_from_thoroughly(&proc->waitLock->waitProcs, &proc->waitLink);
1715 : :
1716 : : /* Clean up process' state and pass it the ok/fail signal */
9183 tgl@sss.pgh.pa.us 1717 :CBC 1376 : proc->waitLock = NULL;
7870 1718 : 1376 : proc->waitProcLock = NULL;
7911 1719 : 1376 : proc->waitStatus = waitStatus;
17 fujii@postgresql.org 1720 : 1376 : pg_atomic_write_u64(&proc->waitStart, 0);
1721 : :
1722 : : /* And awaken it */
4058 andres@anarazel.de 1723 : 1376 : SetLatch(&proc->procLatch);
1724 : : }
1725 : :
1726 : : /*
1727 : : * ProcLockWakeup -- routine for waking up processes when a lock is
1728 : : * released (or a prior waiter is aborted). Scan all waiters
1729 : : * for lock, waken any that are no longer blocked.
1730 : : *
1731 : : * The appropriate lock partition lock must be held by caller.
1732 : : */
1733 : : void
8140 bruce@momjian.us 1734 : 1392 : ProcLockWakeup(LockMethod lockMethodTable, LOCK *lock)
1735 : : {
1152 andres@anarazel.de 1736 : 1392 : dclist_head *waitQueue = &lock->waitProcs;
8140 bruce@momjian.us 1737 : 1392 : LOCKMASK aheadRequests = 0;
1738 : : dlist_mutable_iter miter;
1739 : :
1152 andres@anarazel.de 1740 [ + + ]: 1392 : if (dclist_is_empty(waitQueue))
9180 tgl@sss.pgh.pa.us 1741 : 44 : return;
1742 : :
1152 andres@anarazel.de 1743 [ + - + + ]: 3350 : dclist_foreach_modify(miter, waitQueue)
1744 : : {
23 heikki.linnakangas@i 1745 :GNC 2002 : PGPROC *proc = dlist_container(PGPROC, waitLink, miter.cur);
9124 bruce@momjian.us 1746 :CBC 2002 : LOCKMODE lockmode = proc->waitLockMode;
1747 : :
1748 : : /*
1749 : : * Waken if (a) doesn't conflict with requests of earlier waiters, and
1750 : : * (b) doesn't conflict with already-held locks.
1751 : : */
8641 1752 [ + + ]: 2002 : if ((lockMethodTable->conflictTab[lockmode] & aheadRequests) == 0 &&
2268 peter@eisentraut.org 1753 [ + + ]: 1642 : !LockCheckConflicts(lockMethodTable, lockmode, lock,
1754 : : proc->waitProcLock))
1755 : : {
1756 : : /* OK to waken */
7870 tgl@sss.pgh.pa.us 1757 : 1376 : GrantLock(lock, proc->waitProcLock, lockmode);
1758 : : /* removes proc from the lock's waiting process queue */
1152 andres@anarazel.de 1759 : 1376 : ProcWakeup(proc, PROC_WAIT_STATUS_OK);
1760 : : }
1761 : : else
1762 : : {
1763 : : /*
1764 : : * Lock conflicts: Don't wake, but remember requested mode for
1765 : : * later checks.
1766 : : */
8140 bruce@momjian.us 1767 : 626 : aheadRequests |= LOCKBIT_ON(lockmode);
1768 : : }
1769 : : }
1770 : : }
1771 : :
1772 : : /*
1773 : : * CheckDeadLock
1774 : : *
1775 : : * We only get to this routine, if DEADLOCK_TIMEOUT fired while waiting for a
1776 : : * lock to be released by some other process. Check if there's a deadlock; if
1777 : : * not, just return. If we have a real deadlock, remove ourselves from the
1778 : : * lock's wait queue.
1779 : : */
1780 : : static DeadLockState
8646 1781 : 32 : CheckDeadLock(void)
1782 : : {
1783 : : int i;
1784 : : DeadLockState result;
1785 : :
1786 : : /*
1787 : : * Acquire exclusive lock on the entire shared lock data structures. Must
1788 : : * grab LWLocks in partition-number order to avoid LWLock deadlock.
1789 : : *
1790 : : * Note that the deadlock check interrupt had better not be enabled
1791 : : * anywhere that this process itself holds lock partition locks, else this
1792 : : * will wait forever. Also note that LWLockAcquire creates a critical
1793 : : * section, so that this routine cannot be interrupted by cancel/die
1794 : : * interrupts.
1795 : : */
7399 tgl@sss.pgh.pa.us 1796 [ + + ]: 544 : for (i = 0; i < NUM_LOCK_PARTITIONS; i++)
4430 rhaas@postgresql.org 1797 : 512 : LWLockAcquire(LockHashPartitionLockByIndex(i), LW_EXCLUSIVE);
1798 : :
1799 : : /*
1800 : : * Check to see if we've been awoken by anyone in the interim.
1801 : : *
1802 : : * If we have, we can return and resume our transaction -- happy day.
1803 : : * Before we are awoken the process releasing the lock grants it to us so
1804 : : * we know that we don't have to wait anymore.
1805 : : *
1806 : : * We check by looking to see if we've been unlinked from the wait queue.
1807 : : * This is safe because we hold the lock partition lock.
1808 : : */
23 heikki.linnakangas@i 1809 [ - + ]:GNC 32 : if (dlist_node_is_detached(&MyProc->waitLink))
1810 : : {
34 heikki.linnakangas@i 1811 :UNC 0 : result = DS_NO_DEADLOCK;
6844 tgl@sss.pgh.pa.us 1812 :UBC 0 : goto check_done;
1813 : : }
1814 : :
1815 : : #ifdef LOCK_DEBUG
1816 : : if (Debug_deadlocks)
1817 : : DumpAllLocks();
1818 : : #endif
1819 : :
1820 : : /* Run the deadlock check */
34 heikki.linnakangas@i 1821 :GNC 32 : result = DeadLockCheck(MyProc);
1822 : :
1823 [ + + ]: 32 : if (result == DS_HARD_DEADLOCK)
1824 : : {
1825 : : /*
1826 : : * Oops. We have a deadlock.
1827 : : *
1828 : : * Get this process out of wait state. (Note: we could do this more
1829 : : * efficiently by relying on lockAwaited, but use this coding to
1830 : : * preserve the flexibility to kill some other transaction than the
1831 : : * one detecting the deadlock.)
1832 : : *
1833 : : * RemoveFromWaitQueue sets MyProc->waitStatus to
1834 : : * PROC_WAIT_STATUS_ERROR, so ProcSleep will report an error after we
1835 : : * return.
1836 : : */
6952 bruce@momjian.us 1837 [ - + ]:CBC 5 : Assert(MyProc->waitLock != NULL);
1838 : 5 : RemoveFromWaitQueue(MyProc, LockTagHashCode(&(MyProc->waitLock->tag)));
1839 : :
1840 : : /*
1841 : : * We're done here. Transaction abort caused by the error that
1842 : : * ProcSleep will raise will cause any other locks we hold to be
1843 : : * released, thus allowing other processes to wake up; we don't need
1844 : : * to do that here. NOTE: an exception is that releasing locks we
1845 : : * hold doesn't consider the possibility of waiters that were blocked
1846 : : * behind us on the lock we just failed to get, and might now be
1847 : : * wakable because we're not in front of them anymore. However,
1848 : : * RemoveFromWaitQueue took care of waking up any such processes.
1849 : : */
1850 : : }
1851 : :
1852 : : /*
1853 : : * And release locks. We do this in reverse order for two reasons: (1)
1854 : : * Anyone else who needs more than one of the locks will be trying to lock
1855 : : * them in increasing order; we don't want to release the other process
1856 : : * until it can get all the locks it needs. (2) This avoids O(N^2)
1857 : : * behavior inside LWLockRelease.
1858 : : */
6844 tgl@sss.pgh.pa.us 1859 : 27 : check_done:
7102 bruce@momjian.us 1860 [ + + ]: 544 : for (i = NUM_LOCK_PARTITIONS; --i >= 0;)
4430 rhaas@postgresql.org 1861 : 512 : LWLockRelease(LockHashPartitionLockByIndex(i));
1862 : :
34 heikki.linnakangas@i 1863 :GNC 32 : return result;
10841 scrappy@hub.org 1864 :ECB (30) : }
1865 : :
1866 : : /*
1867 : : * CheckDeadLockAlert - Handle the expiry of deadlock_timeout.
1868 : : *
1869 : : * NB: Runs inside a signal handler, be careful.
1870 : : */
1871 : : void
4058 andres@anarazel.de 1872 :CBC 32 : CheckDeadLockAlert(void)
1873 : : {
1874 : 32 : int save_errno = errno;
1875 : :
1876 : 32 : got_deadlock_timeout = true;
1877 : :
1878 : : /*
1879 : : * Have to set the latch again, even if handle_sig_alarm already did. Back
1880 : : * then got_deadlock_timeout wasn't yet set... It's unlikely that this
1881 : : * ever would be a problem, but setting a set latch again is cheap.
1882 : : *
1883 : : * Note that, when this function runs inside procsignal_sigusr1_handler(),
1884 : : * the handler function sets the latch again after the latch is set here.
1885 : : */
1886 : 32 : SetLatch(MyLatch);
1887 : 32 : errno = save_errno;
1888 : 32 : }
1889 : :
1890 : : /*
1891 : : * GetLockHoldersAndWaiters - get lock holders and waiters for a lock
1892 : : *
1893 : : * Fill lock_holders_sbuf and lock_waiters_sbuf with the PIDs of processes holding
1894 : : * and waiting for the lock, and set lockHoldersNum to the number of lock holders.
1895 : : *
1896 : : * The lock table's partition lock must be held on entry and remains held on exit.
1897 : : */
1898 : : void
366 fujii@postgresql.org 1899 : 286 : GetLockHoldersAndWaiters(LOCALLOCK *locallock, StringInfo lock_holders_sbuf,
1900 : : StringInfo lock_waiters_sbuf, int *lockHoldersNum)
1901 : : {
1902 : : dlist_iter proc_iter;
1903 : : PROCLOCK *curproclock;
1904 : 286 : LOCK *lock = locallock->lock;
1905 : 286 : bool first_holder = true,
1906 : 286 : first_waiter = true;
1907 : :
1908 : : #ifdef USE_ASSERT_CHECKING
1909 : : {
1910 : 286 : uint32 hashcode = locallock->hashcode;
1911 : 286 : LWLock *partitionLock = LockHashPartitionLock(hashcode);
1912 : :
1913 [ - + ]: 286 : Assert(LWLockHeldByMe(partitionLock));
1914 : : }
1915 : : #endif
1916 : :
1917 : 286 : *lockHoldersNum = 0;
1918 : :
1919 : : /*
1920 : : * Loop over the lock's procLocks to gather a list of all holders and
1921 : : * waiters. Thus we will be able to provide more detailed information for
1922 : : * lock debugging purposes.
1923 : : *
1924 : : * lock->procLocks contains all processes which hold or wait for this
1925 : : * lock.
1926 : : */
1927 [ + - + + ]: 852 : dlist_foreach(proc_iter, &lock->procLocks)
1928 : : {
1929 : 566 : curproclock =
1930 : 566 : dlist_container(PROCLOCK, lockLink, proc_iter.cur);
1931 : :
1932 : : /*
1933 : : * We are a waiter if myProc->waitProcLock == curproclock; we are a
1934 : : * holder if it is NULL or something different.
1935 : : */
1936 [ + + ]: 566 : if (curproclock->tag.myProc->waitProcLock == curproclock)
1937 : : {
1938 [ + + ]: 273 : if (first_waiter)
1939 : : {
1940 : 258 : appendStringInfo(lock_waiters_sbuf, "%d",
1941 : 258 : curproclock->tag.myProc->pid);
1942 : 258 : first_waiter = false;
1943 : : }
1944 : : else
1945 : 15 : appendStringInfo(lock_waiters_sbuf, ", %d",
1946 : 15 : curproclock->tag.myProc->pid);
1947 : : }
1948 : : else
1949 : : {
1950 [ + + ]: 293 : if (first_holder)
1951 : : {
1952 : 286 : appendStringInfo(lock_holders_sbuf, "%d",
1953 : 286 : curproclock->tag.myProc->pid);
1954 : 286 : first_holder = false;
1955 : : }
1956 : : else
1957 : 7 : appendStringInfo(lock_holders_sbuf, ", %d",
1958 : 7 : curproclock->tag.myProc->pid);
1959 : :
1960 : 293 : (*lockHoldersNum)++;
1961 : : }
1962 : : }
1963 : 286 : }
1964 : :
1965 : : /*
1966 : : * ProcWaitForSignal - wait for a signal from another backend.
1967 : : *
1968 : : * As this uses the generic process latch the caller has to be robust against
1969 : : * unrelated wakeups: Always check that the desired state has occurred, and
1970 : : * wait again if not.
1971 : : */
1972 : : void
3449 rhaas@postgresql.org 1973 : 108 : ProcWaitForSignal(uint32 wait_event_info)
1974 : : {
2669 tmunro@postgresql.or 1975 : 108 : (void) WaitLatch(MyLatch, WL_LATCH_SET | WL_EXIT_ON_PM_DEATH, 0,
1976 : : wait_event_info);
4058 andres@anarazel.de 1977 : 108 : ResetLatch(MyLatch);
1978 [ - + ]: 108 : CHECK_FOR_INTERRUPTS();
9018 tgl@sss.pgh.pa.us 1979 : 108 : }
1980 : :
1981 : : /*
1982 : : * ProcSendSignal - set the latch of a backend identified by ProcNumber
1983 : : */
1984 : : void
742 heikki.linnakangas@i 1985 : 95 : ProcSendSignal(ProcNumber procNumber)
1986 : : {
1987 [ + - - + ]: 95 : if (procNumber < 0 || procNumber >= ProcGlobal->allProcCount)
742 heikki.linnakangas@i 1988 [ # # ]:UBC 0 : elog(ERROR, "procNumber out of range");
1989 : :
69 drowley@postgresql.o 1990 :GNC 95 : SetLatch(&GetPGProcByNumber(procNumber)->procLatch);
9018 tgl@sss.pgh.pa.us 1991 :CBC 95 : }
1992 : :
1993 : : /*
1994 : : * BecomeLockGroupLeader - designate process as lock group leader
1995 : : *
1996 : : * Once this function has returned, other processes can join the lock group
1997 : : * by calling BecomeLockGroupMember.
1998 : : */
1999 : : void
3689 rhaas@postgresql.org 2000 : 636 : BecomeLockGroupLeader(void)
2001 : : {
2002 : : LWLock *leader_lwlock;
2003 : :
2004 : : /* If we already did it, we don't need to do it again. */
2005 [ + + ]: 636 : if (MyProc->lockGroupLeader == MyProc)
2006 : 551 : return;
2007 : :
2008 : : /* We had better not be a follower. */
2009 [ - + ]: 85 : Assert(MyProc->lockGroupLeader == NULL);
2010 : :
2011 : : /* Create single-member group, containing only ourselves. */
2012 : 85 : leader_lwlock = LockHashPartitionLockByProc(MyProc);
2013 : 85 : LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
2014 : 85 : MyProc->lockGroupLeader = MyProc;
2015 : 85 : dlist_push_head(&MyProc->lockGroupMembers, &MyProc->lockGroupLink);
2016 : 85 : LWLockRelease(leader_lwlock);
2017 : : }
2018 : :
2019 : : /*
2020 : : * BecomeLockGroupMember - designate process as lock group member
2021 : : *
2022 : : * This is pretty straightforward except for the possibility that the leader
2023 : : * whose group we're trying to join might exit before we manage to do so;
2024 : : * and the PGPROC might get recycled for an unrelated process. To avoid
2025 : : * that, we require the caller to pass the PID of the intended PGPROC as
2026 : : * an interlock. Returns true if we successfully join the intended lock
2027 : : * group, and false if not.
2028 : : */
2029 : : bool
2030 : 1491 : BecomeLockGroupMember(PGPROC *leader, int pid)
2031 : : {
2032 : : LWLock *leader_lwlock;
2033 : 1491 : bool ok = false;
2034 : :
2035 : : /* Group leader can't become member of group */
2036 [ - + ]: 1491 : Assert(MyProc != leader);
2037 : :
2038 : : /* Can't already be a member of a group */
3674 tgl@sss.pgh.pa.us 2039 [ - + ]: 1491 : Assert(MyProc->lockGroupLeader == NULL);
2040 : :
2041 : : /* PID must be valid. */
3689 rhaas@postgresql.org 2042 [ - + ]: 1491 : Assert(pid != 0);
2043 : :
2044 : : /*
2045 : : * Get lock protecting the group fields. Note LockHashPartitionLockByProc
2046 : : * calculates the proc number based on the PGPROC slot without looking at
2047 : : * its contents, so we will acquire the correct lock even if the leader
2048 : : * PGPROC is in process of being recycled.
2049 : : */
3675 2050 : 1491 : leader_lwlock = LockHashPartitionLockByProc(leader);
3689 2051 : 1491 : LWLockAcquire(leader_lwlock, LW_EXCLUSIVE);
2052 : :
2053 : : /* Is this the leader we're looking for? */
3674 tgl@sss.pgh.pa.us 2054 [ + - + - ]: 1491 : if (leader->pid == pid && leader->lockGroupLeader == leader)
2055 : : {
2056 : : /* OK, join the group */
3689 rhaas@postgresql.org 2057 : 1491 : ok = true;
2058 : 1491 : MyProc->lockGroupLeader = leader;
2059 : 1491 : dlist_push_tail(&leader->lockGroupMembers, &MyProc->lockGroupLink);
2060 : : }
2061 : 1491 : LWLockRelease(leader_lwlock);
2062 : :
2063 : 1491 : return ok;
2064 : : }
|