Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "postgres.h"
00030
00031 #include "access/slru.h"
00032 #include "access/subtrans.h"
00033 #include "access/transam.h"
00034 #include "pg_trace.h"
00035 #include "utils/snapmgr.h"
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051 #define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
00052
00053 #define TransactionIdToPage(xid) ((xid) / (TransactionId) SUBTRANS_XACTS_PER_PAGE)
00054 #define TransactionIdToEntry(xid) ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)
00055
00056
00057
00058
00059
00060 static SlruCtlData SubTransCtlData;
00061
00062 #define SubTransCtl (&SubTransCtlData)
00063
00064
00065 static int ZeroSUBTRANSPage(int pageno);
00066 static bool SubTransPagePrecedes(int page1, int page2);
00067
00068
00069
00070
00071
00072
00073
00074 void
00075 SubTransSetParent(TransactionId xid, TransactionId parent, bool overwriteOK)
00076 {
00077 int pageno = TransactionIdToPage(xid);
00078 int entryno = TransactionIdToEntry(xid);
00079 int slotno;
00080 TransactionId *ptr;
00081
00082 Assert(TransactionIdIsValid(parent));
00083
00084 LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
00085
00086 slotno = SimpleLruReadPage(SubTransCtl, pageno, true, xid);
00087 ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
00088 ptr += entryno;
00089
00090
00091 Assert(*ptr == InvalidTransactionId ||
00092 (*ptr == parent && overwriteOK));
00093
00094 *ptr = parent;
00095
00096 SubTransCtl->shared->page_dirty[slotno] = true;
00097
00098 LWLockRelease(SubtransControlLock);
00099 }
00100
00101
00102
00103
00104 TransactionId
00105 SubTransGetParent(TransactionId xid)
00106 {
00107 int pageno = TransactionIdToPage(xid);
00108 int entryno = TransactionIdToEntry(xid);
00109 int slotno;
00110 TransactionId *ptr;
00111 TransactionId parent;
00112
00113
00114 Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin));
00115
00116
00117 if (!TransactionIdIsNormal(xid))
00118 return InvalidTransactionId;
00119
00120
00121
00122 slotno = SimpleLruReadPage_ReadOnly(SubTransCtl, pageno, xid);
00123 ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
00124 ptr += entryno;
00125
00126 parent = *ptr;
00127
00128 LWLockRelease(SubtransControlLock);
00129
00130 return parent;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 TransactionId
00146 SubTransGetTopmostTransaction(TransactionId xid)
00147 {
00148 TransactionId parentXid = xid,
00149 previousXid = xid;
00150
00151
00152 Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin));
00153
00154 while (TransactionIdIsValid(parentXid))
00155 {
00156 previousXid = parentXid;
00157 if (TransactionIdPrecedes(parentXid, TransactionXmin))
00158 break;
00159 parentXid = SubTransGetParent(parentXid);
00160 }
00161
00162 Assert(TransactionIdIsValid(previousXid));
00163
00164 return previousXid;
00165 }
00166
00167
00168
00169
00170
00171 Size
00172 SUBTRANSShmemSize(void)
00173 {
00174 return SimpleLruShmemSize(NUM_SUBTRANS_BUFFERS, 0);
00175 }
00176
00177 void
00178 SUBTRANSShmemInit(void)
00179 {
00180 SubTransCtl->PagePrecedes = SubTransPagePrecedes;
00181 SimpleLruInit(SubTransCtl, "SUBTRANS Ctl", NUM_SUBTRANS_BUFFERS, 0,
00182 SubtransControlLock, "pg_subtrans");
00183
00184 SubTransCtl->do_fsync = false;
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197 void
00198 BootStrapSUBTRANS(void)
00199 {
00200 int slotno;
00201
00202 LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
00203
00204
00205 slotno = ZeroSUBTRANSPage(0);
00206
00207
00208 SimpleLruWritePage(SubTransCtl, slotno);
00209 Assert(!SubTransCtl->shared->page_dirty[slotno]);
00210
00211 LWLockRelease(SubtransControlLock);
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222 static int
00223 ZeroSUBTRANSPage(int pageno)
00224 {
00225 return SimpleLruZeroPage(SubTransCtl, pageno);
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235 void
00236 StartupSUBTRANS(TransactionId oldestActiveXID)
00237 {
00238 int startPage;
00239 int endPage;
00240
00241
00242
00243
00244
00245
00246
00247 LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
00248
00249 startPage = TransactionIdToPage(oldestActiveXID);
00250 endPage = TransactionIdToPage(ShmemVariableCache->nextXid);
00251
00252 while (startPage != endPage)
00253 {
00254 (void) ZeroSUBTRANSPage(startPage);
00255 startPage++;
00256 }
00257 (void) ZeroSUBTRANSPage(startPage);
00258
00259 LWLockRelease(SubtransControlLock);
00260 }
00261
00262
00263
00264
00265 void
00266 ShutdownSUBTRANS(void)
00267 {
00268
00269
00270
00271
00272
00273
00274 TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(false);
00275 SimpleLruFlush(SubTransCtl, false);
00276 TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(false);
00277 }
00278
00279
00280
00281
00282 void
00283 CheckPointSUBTRANS(void)
00284 {
00285
00286
00287
00288
00289
00290
00291
00292 TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_START(true);
00293 SimpleLruFlush(SubTransCtl, true);
00294 TRACE_POSTGRESQL_SUBTRANS_CHECKPOINT_DONE(true);
00295 }
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306 void
00307 ExtendSUBTRANS(TransactionId newestXact)
00308 {
00309 int pageno;
00310
00311
00312
00313
00314
00315 if (TransactionIdToEntry(newestXact) != 0 &&
00316 !TransactionIdEquals(newestXact, FirstNormalTransactionId))
00317 return;
00318
00319 pageno = TransactionIdToPage(newestXact);
00320
00321 LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
00322
00323
00324 ZeroSUBTRANSPage(pageno);
00325
00326 LWLockRelease(SubtransControlLock);
00327 }
00328
00329
00330
00331
00332
00333
00334
00335
00336 void
00337 TruncateSUBTRANS(TransactionId oldestXact)
00338 {
00339 int cutoffPage;
00340
00341
00342
00343
00344
00345 cutoffPage = TransactionIdToPage(oldestXact);
00346
00347 SimpleLruTruncate(SubTransCtl, cutoffPage);
00348 }
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 static bool
00361 SubTransPagePrecedes(int page1, int page2)
00362 {
00363 TransactionId xid1;
00364 TransactionId xid2;
00365
00366 xid1 = ((TransactionId) page1) * SUBTRANS_XACTS_PER_PAGE;
00367 xid1 += FirstNormalTransactionId;
00368 xid2 = ((TransactionId) page2) * SUBTRANS_XACTS_PER_PAGE;
00369 xid2 += FirstNormalTransactionId;
00370
00371 return TransactionIdPrecedes(xid1, xid2);
00372 }