Commit 0d21f919 authored by Alvaro Herrera's avatar Alvaro Herrera

Fix crash when reporting CREATE INDEX progress

A race condition can make us try to dereference a NULL pointer to the
PGPROC struct of a process that's already finished.  That results in
crashes during REINDEX CONCURRENTLY and CREATE INDEX CONCURRENTLY.

This was introduced in ab0dfc96, so backpatch to pg12.

Reported by: Justin Pryzby
Reviewed-by: Michaël Paquier
Discussion: https://postgr.es/m/20191012004446.GT10470@telsasoft.com
parent a524f50d
...@@ -384,12 +384,14 @@ WaitForOlderSnapshots(TransactionId limitXmin, bool progress) ...@@ -384,12 +384,14 @@ WaitForOlderSnapshots(TransactionId limitXmin, bool progress)
if (VirtualTransactionIdIsValid(old_snapshots[i])) if (VirtualTransactionIdIsValid(old_snapshots[i]))
{ {
/* If requested, publish who we're going to wait for. */
if (progress) if (progress)
{ {
PGPROC *holder = BackendIdGetProc(old_snapshots[i].backendId); PGPROC *holder = BackendIdGetProc(old_snapshots[i].backendId);
pgstat_progress_update_param(PROGRESS_WAITFOR_CURRENT_PID, if (holder)
holder->pid); pgstat_progress_update_param(PROGRESS_WAITFOR_CURRENT_PID,
holder->pid);
} }
VirtualXactLock(old_snapshots[i], true); VirtualXactLock(old_snapshots[i], true);
} }
......
...@@ -900,16 +900,14 @@ WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress) ...@@ -900,16 +900,14 @@ WaitForLockersMultiple(List *locktags, LOCKMODE lockmode, bool progress)
while (VirtualTransactionIdIsValid(*lockholders)) while (VirtualTransactionIdIsValid(*lockholders))
{ {
/* /* If requested, publish who we're going to wait for. */
* If requested, publish who we're going to wait for. This is not
* 100% accurate if they're already gone, but we don't care.
*/
if (progress) if (progress)
{ {
PGPROC *holder = BackendIdGetProc(lockholders->backendId); PGPROC *holder = BackendIdGetProc(lockholders->backendId);
pgstat_progress_update_param(PROGRESS_WAITFOR_CURRENT_PID, if (holder)
holder->pid); pgstat_progress_update_param(PROGRESS_WAITFOR_CURRENT_PID,
holder->pid);
} }
VirtualXactLock(*lockholders, true); VirtualXactLock(*lockholders, true);
lockholders++; lockholders++;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment