Skip to content

Commit 612495e

Browse files
schmiddydvarrazzo
authored andcommitted
Print a status message while waiting on old transactions to finish, including a backend PID we are waiting on, so that the user knows pg_reorg is hung and can do something about it.
1 parent 2ba1a57 commit 612495e

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

bin/pg_reorg.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ const char *PROGRAM_EMAIL = "reorg-general@lists.pgfoundry.org";
3434
" WHERE locktype = 'transactionid' AND pid <> pg_backend_pid()"
3535

3636
#define SQL_XID_ALIVE_80300 \
37-
"SELECT 1 FROM pg_locks WHERE locktype = 'virtualxid'"\
38-
" AND pid <> pg_backend_pid() AND virtualtransaction = ANY($1) LIMIT 1"
37+
"SELECT pid FROM pg_locks WHERE locktype = 'virtualxid'"\
38+
" AND pid <> pg_backend_pid() AND virtualtransaction = ANY($1)"
3939
#define SQL_XID_ALIVE_80200 \
40-
"SELECT 1 FROM pg_locks WHERE locktype = 'transactionid'"\
41-
" AND pid <> pg_backend_pid() AND transactionid = ANY($1) LIMIT 1"
40+
"SELECT pid FROM pg_locks WHERE locktype = 'transactionid'"\
41+
" AND pid <> pg_backend_pid() AND transactionid = ANY($1)"
4242

4343
#define SQL_XID_SNAPSHOT \
4444
(PQserverVersion(connection) >= 80300 \
@@ -383,6 +383,7 @@ reorg_one_table(const reorg_table *table, const char *orderby)
383383
const char *params[1];
384384
int num;
385385
int i;
386+
int num_waiting = 0;
386387
char *vxid;
387388
char buffer[12];
388389
StringInfoData sql;
@@ -509,16 +510,32 @@ reorg_one_table(const reorg_table *table, const char *orderby)
509510
params[0] = vxid;
510511
res = execute(SQL_XID_ALIVE, 1, params);
511512
num = PQntuples(res);
512-
PQclear(res);
513513

514514
if (num > 0)
515515
{
516+
/* Wait for old transactions.
517+
* Only display the message below when the number of
518+
* transactions we are waiting on changes (presumably,
519+
* num_waiting should only go down), so as not to
520+
* be too noisy.
521+
*/
522+
if (num != num_waiting)
523+
{
524+
elog(NOTICE, "Waiting for %d transactions to finish. First PID: %s", num, PQgetvalue(res, 0, 0));
525+
num_waiting = num;
526+
}
527+
528+
PQclear(res);
516529
sleep(1);
517-
continue; /* wait for old transactions */
530+
continue;
531+
}
532+
else
533+
{
534+
/* All old transactions are finished;
535+
* go to next step. */
536+
PQclear(res);
537+
break;
518538
}
519-
520-
/* ok, go to next step. */
521-
break;
522539
}
523540

524541
/*

0 commit comments

Comments
 (0)