23
23
*
24
24
*
25
25
* IDENTIFICATION
26
- * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.34 2007/09/21 17:36:53 tgl Exp $
26
+ * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.35 2007/09/23 18:50:38 tgl Exp $
27
27
*
28
28
*-------------------------------------------------------------------------
29
29
*/
@@ -61,23 +61,29 @@ static ProcArrayStruct *procArray;
61
61
/* counters for XidCache measurement */
62
62
static long xc_by_recent_xmin = 0 ;
63
63
static long xc_by_my_xact = 0 ;
64
+ static long xc_by_latest_xid = 0 ;
64
65
static long xc_by_main_xid = 0 ;
65
66
static long xc_by_child_xid = 0 ;
67
+ static long xc_no_overflow = 0 ;
66
68
static long xc_slow_answer = 0 ;
67
69
68
70
#define xc_by_recent_xmin_inc () (xc_by_recent_xmin++)
69
71
#define xc_by_my_xact_inc () (xc_by_my_xact++)
72
+ #define xc_by_latest_xid_inc () (xc_by_latest_xid++)
70
73
#define xc_by_main_xid_inc () (xc_by_main_xid++)
71
74
#define xc_by_child_xid_inc () (xc_by_child_xid++)
75
+ #define xc_no_overflow_inc () (xc_no_overflow++)
72
76
#define xc_slow_answer_inc () (xc_slow_answer++)
73
77
74
78
static void DisplayXidCache (void );
75
79
#else /* !XIDCACHE_DEBUG */
76
80
77
81
#define xc_by_recent_xmin_inc () ((void) 0)
78
82
#define xc_by_my_xact_inc () ((void) 0)
83
+ #define xc_by_latest_xid_inc () ((void) 0)
79
84
#define xc_by_main_xid_inc () ((void) 0)
80
85
#define xc_by_child_xid_inc () ((void) 0)
86
+ #define xc_no_overflow_inc () ((void) 0)
81
87
#define xc_slow_answer_inc () ((void) 0)
82
88
#endif /* XIDCACHE_DEBUG */
83
89
@@ -302,7 +308,8 @@ ProcArrayClearTransaction(PGPROC *proc)
302
308
/*
303
309
* TransactionIdIsInProgress -- is given transaction running in some backend
304
310
*
305
- * There are three possibilities for finding a running transaction:
311
+ * Aside from some shortcuts such as checking RecentXmin and our own Xid,
312
+ * there are three possibilities for finding a running transaction:
306
313
*
307
314
* 1. the given Xid is a main transaction Id. We will find this out cheaply
308
315
* by looking at the PGPROC struct for each backend.
@@ -368,6 +375,18 @@ TransactionIdIsInProgress(TransactionId xid)
368
375
369
376
LWLockAcquire (ProcArrayLock , LW_SHARED );
370
377
378
+ /*
379
+ * Now that we have the lock, we can check latestCompletedXid; if the
380
+ * target Xid is after that, it's surely still running.
381
+ */
382
+ if (TransactionIdPrecedes (ShmemVariableCache -> latestCompletedXid , xid ))
383
+ {
384
+ LWLockRelease (ProcArrayLock );
385
+ xc_by_latest_xid_inc ();
386
+ return true;
387
+ }
388
+
389
+ /* No shortcuts, gotta grovel through the array */
371
390
for (i = 0 ; i < arrayP -> numProcs ; i ++ )
372
391
{
373
392
volatile PGPROC * proc = arrayP -> procs [i ];
@@ -434,7 +453,10 @@ TransactionIdIsInProgress(TransactionId xid)
434
453
* running without looking at pg_subtrans.
435
454
*/
436
455
if (nxids == 0 )
456
+ {
457
+ xc_no_overflow_inc ();
437
458
return false;
459
+ }
438
460
439
461
/*
440
462
* Step 3: have to check pg_subtrans.
@@ -451,8 +473,8 @@ TransactionIdIsInProgress(TransactionId xid)
451
473
452
474
/*
453
475
* It isn't aborted, so check whether the transaction tree it belongs to
454
- * is still running (or, more precisely, whether it was running when this
455
- * routine started -- note that we already released ProcArrayLock).
476
+ * is still running (or, more precisely, whether it was running when
477
+ * we held ProcArrayLock).
456
478
*/
457
479
topxid = SubTransGetTopmostTransaction (xid );
458
480
Assert (TransactionIdIsValid (topxid ));
@@ -1300,11 +1322,13 @@ static void
1300
1322
DisplayXidCache (void )
1301
1323
{
1302
1324
fprintf (stderr ,
1303
- "XidCache: xmin: %ld, myxact: %ld, mainxid: %ld, childxid: %ld, slow: %ld\n" ,
1325
+ "XidCache: xmin: %ld, myxact: %ld, latest: %ld, mainxid: %ld, childxid: %ld, nooflo : %ld, slow: %ld\n" ,
1304
1326
xc_by_recent_xmin ,
1305
1327
xc_by_my_xact ,
1328
+ xc_by_latest_xid ,
1306
1329
xc_by_main_xid ,
1307
1330
xc_by_child_xid ,
1331
+ xc_no_overflow ,
1308
1332
xc_slow_answer );
1309
1333
}
1310
1334
0 commit comments