~vcs-imports/mammoth-replicator/trunk

« back to all changes in this revision

Viewing changes to src/backend/access/transam/subtrans.c

  • Committer: alvherre
  • Date: 2005-12-16 21:24:52 UTC
  • Revision ID: svn-v4:db760fc0-0f08-0410-9d63-cc6633f64896:trunk:1
Initial import of the REL8_0_3 sources from the Pgsql CVS repository.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*-------------------------------------------------------------------------
 
2
 *
 
3
 * subtrans.c
 
4
 *              PostgreSQL subtransaction-log manager
 
5
 *
 
6
 * The pg_subtrans manager is a pg_clog-like manager that stores the parent
 
7
 * transaction Id for each transaction.  It is a fundamental part of the
 
8
 * nested transactions implementation.  A main transaction has a parent
 
9
 * of InvalidTransactionId, and each subtransaction has its immediate parent.
 
10
 * The tree can easily be walked from child to parent, but not in the
 
11
 * opposite direction.
 
12
 *
 
13
 * This code is based on clog.c, but the robustness requirements
 
14
 * are completely different from pg_clog, because we only need to remember
 
15
 * pg_subtrans information for currently-open transactions.  Thus, there is
 
16
 * no need to preserve data over a crash and restart.
 
17
 *
 
18
 * There are no XLOG interactions since we do not care about preserving
 
19
 * data across crashes.  During database startup, we simply force the
 
20
 * currently-active page of SUBTRANS to zeroes.
 
21
 *
 
22
 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
 
23
 * Portions Copyright (c) 1994, Regents of the University of California
 
24
 *
 
25
 * $PostgreSQL: pgsql/src/backend/access/transam/subtrans.c,v 1.7 2004-12-31 21:59:29 pgsql Exp $
 
26
 *
 
27
 *-------------------------------------------------------------------------
 
28
 */
 
29
#include "postgres.h"
 
30
 
 
31
#include "access/slru.h"
 
32
#include "access/subtrans.h"
 
33
#include "storage/sinval.h"
 
34
#include "utils/tqual.h"
 
35
 
 
36
 
 
37
/*
 
38
 * Defines for SubTrans page sizes.  A page is the same BLCKSZ as is used
 
39
 * everywhere else in Postgres.
 
40
 *
 
41
 * Note: because TransactionIds are 32 bits and wrap around at 0xFFFFFFFF,
 
42
 * SubTrans page numbering also wraps around at
 
43
 * 0xFFFFFFFF/SUBTRANS_XACTS_PER_PAGE, and segment numbering at
 
44
 * 0xFFFFFFFF/SUBTRANS_XACTS_PER_PAGE/SLRU_SEGMENTS_PER_PAGE.  We need take no
 
45
 * explicit notice of that fact in this module, except when comparing segment
 
46
 * and page numbers in TruncateSUBTRANS (see SubTransPagePrecedes).
 
47
 */
 
48
 
 
49
/* We need four bytes per xact */
 
50
#define SUBTRANS_XACTS_PER_PAGE (BLCKSZ / sizeof(TransactionId))
 
51
 
 
52
#define TransactionIdToPage(xid) ((xid) / (TransactionId) SUBTRANS_XACTS_PER_PAGE)
 
53
#define TransactionIdToEntry(xid) ((xid) % (TransactionId) SUBTRANS_XACTS_PER_PAGE)
 
54
 
 
55
 
 
56
/*
 
57
 * Link to shared-memory data structures for SUBTRANS control
 
58
 */
 
59
static SlruCtlData SubTransCtlData;
 
60
 
 
61
#define SubTransCtl  (&SubTransCtlData)
 
62
 
 
63
 
 
64
static int      ZeroSUBTRANSPage(int pageno);
 
65
static bool SubTransPagePrecedes(int page1, int page2);
 
66
 
 
67
 
 
68
/*
 
69
 * Record the parent of a subtransaction in the subtrans log.
 
70
 */
 
71
void
 
72
SubTransSetParent(TransactionId xid, TransactionId parent)
 
73
{
 
74
        int                     pageno = TransactionIdToPage(xid);
 
75
        int                     entryno = TransactionIdToEntry(xid);
 
76
        int                     slotno;
 
77
        TransactionId *ptr;
 
78
 
 
79
        LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
 
80
 
 
81
        slotno = SimpleLruReadPage(SubTransCtl, pageno, xid);
 
82
        ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
 
83
        ptr += entryno;
 
84
 
 
85
        /* Current state should be 0 */
 
86
        Assert(*ptr == InvalidTransactionId);
 
87
 
 
88
        *ptr = parent;
 
89
 
 
90
        SubTransCtl->shared->page_status[slotno] = SLRU_PAGE_DIRTY;
 
91
 
 
92
        LWLockRelease(SubtransControlLock);
 
93
}
 
94
 
 
95
/*
 
96
 * Interrogate the parent of a transaction in the subtrans log.
 
97
 */
 
98
TransactionId
 
99
SubTransGetParent(TransactionId xid)
 
100
{
 
101
        int                     pageno = TransactionIdToPage(xid);
 
102
        int                     entryno = TransactionIdToEntry(xid);
 
103
        int                     slotno;
 
104
        TransactionId *ptr;
 
105
        TransactionId parent;
 
106
 
 
107
        /* Can't ask about stuff that might not be around anymore */
 
108
        Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin));
 
109
 
 
110
        /* Bootstrap and frozen XIDs have no parent */
 
111
        if (!TransactionIdIsNormal(xid))
 
112
                return InvalidTransactionId;
 
113
 
 
114
        LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
 
115
 
 
116
        slotno = SimpleLruReadPage(SubTransCtl, pageno, xid);
 
117
        ptr = (TransactionId *) SubTransCtl->shared->page_buffer[slotno];
 
118
        ptr += entryno;
 
119
 
 
120
        parent = *ptr;
 
121
 
 
122
        LWLockRelease(SubtransControlLock);
 
123
 
 
124
        return parent;
 
125
}
 
126
 
 
127
/*
 
128
 * SubTransGetTopmostTransaction
 
129
 *
 
130
 * Returns the topmost transaction of the given transaction id.
 
131
 *
 
132
 * Because we cannot look back further than TransactionXmin, it is possible
 
133
 * that this function will lie and return an intermediate subtransaction ID
 
134
 * instead of the true topmost parent ID.  This is OK, because in practice
 
135
 * we only care about detecting whether the topmost parent is still running
 
136
 * or is part of a current snapshot's list of still-running transactions.
 
137
 * Therefore, any XID before TransactionXmin is as good as any other.
 
138
 */
 
139
TransactionId
 
140
SubTransGetTopmostTransaction(TransactionId xid)
 
141
{
 
142
        TransactionId parentXid = xid,
 
143
                                previousXid = xid;
 
144
 
 
145
        /* Can't ask about stuff that might not be around anymore */
 
146
        Assert(TransactionIdFollowsOrEquals(xid, TransactionXmin));
 
147
 
 
148
        while (TransactionIdIsValid(parentXid))
 
149
        {
 
150
                previousXid = parentXid;
 
151
                if (TransactionIdPrecedes(parentXid, TransactionXmin))
 
152
                        break;
 
153
                parentXid = SubTransGetParent(parentXid);
 
154
        }
 
155
 
 
156
        Assert(TransactionIdIsValid(previousXid));
 
157
 
 
158
        return previousXid;
 
159
}
 
160
 
 
161
 
 
162
/*
 
163
 * Initialization of shared memory for SUBTRANS
 
164
 */
 
165
 
 
166
int
 
167
SUBTRANSShmemSize(void)
 
168
{
 
169
        return SimpleLruShmemSize();
 
170
}
 
171
 
 
172
void
 
173
SUBTRANSShmemInit(void)
 
174
{
 
175
        SubTransCtl->PagePrecedes = SubTransPagePrecedes;
 
176
        SimpleLruInit(SubTransCtl, "SUBTRANS Ctl",
 
177
                                  SubtransControlLock, "pg_subtrans");
 
178
        /* Override default assumption that writes should be fsync'd */
 
179
        SubTransCtl->do_fsync = false;
 
180
}
 
181
 
 
182
/*
 
183
 * This func must be called ONCE on system install.  It creates
 
184
 * the initial SUBTRANS segment.  (The SUBTRANS directory is assumed to
 
185
 * have been created by the initdb shell script, and SUBTRANSShmemInit
 
186
 * must have been called already.)
 
187
 *
 
188
 * Note: it's not really necessary to create the initial segment now,
 
189
 * since slru.c would create it on first write anyway.  But we may as well
 
190
 * do it to be sure the directory is set up correctly.
 
191
 */
 
192
void
 
193
BootStrapSUBTRANS(void)
 
194
{
 
195
        int                     slotno;
 
196
 
 
197
        LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
 
198
 
 
199
        /* Create and zero the first page of the subtrans log */
 
200
        slotno = ZeroSUBTRANSPage(0);
 
201
 
 
202
        /* Make sure it's written out */
 
203
        SimpleLruWritePage(SubTransCtl, slotno, NULL);
 
204
        Assert(SubTransCtl->shared->page_status[slotno] == SLRU_PAGE_CLEAN);
 
205
 
 
206
        LWLockRelease(SubtransControlLock);
 
207
}
 
208
 
 
209
/*
 
210
 * Initialize (or reinitialize) a page of SUBTRANS to zeroes.
 
211
 *
 
212
 * The page is not actually written, just set up in shared memory.
 
213
 * The slot number of the new page is returned.
 
214
 *
 
215
 * Control lock must be held at entry, and will be held at exit.
 
216
 */
 
217
static int
 
218
ZeroSUBTRANSPage(int pageno)
 
219
{
 
220
        return SimpleLruZeroPage(SubTransCtl, pageno);
 
221
}
 
222
 
 
223
/*
 
224
 * This must be called ONCE during postmaster or standalone-backend startup,
 
225
 * after StartupXLOG has initialized ShmemVariableCache->nextXid.
 
226
 */
 
227
void
 
228
StartupSUBTRANS(void)
 
229
{
 
230
        int                     startPage;
 
231
 
 
232
        /*
 
233
         * Since we don't expect pg_subtrans to be valid across crashes, we
 
234
         * initialize the currently-active page to zeroes during startup.
 
235
         * Whenever we advance into a new page, ExtendSUBTRANS will likewise
 
236
         * zero the new page without regard to whatever was previously on
 
237
         * disk.
 
238
         */
 
239
        LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
 
240
 
 
241
        startPage = TransactionIdToPage(ShmemVariableCache->nextXid);
 
242
        (void) ZeroSUBTRANSPage(startPage);
 
243
 
 
244
        LWLockRelease(SubtransControlLock);
 
245
}
 
246
 
 
247
/*
 
248
 * This must be called ONCE during postmaster or standalone-backend shutdown
 
249
 */
 
250
void
 
251
ShutdownSUBTRANS(void)
 
252
{
 
253
        /*
 
254
         * Flush dirty SUBTRANS pages to disk
 
255
         *
 
256
         * This is not actually necessary from a correctness point of view. We do
 
257
         * it merely as a debugging aid.
 
258
         */
 
259
        SimpleLruFlush(SubTransCtl, false);
 
260
}
 
261
 
 
262
/*
 
263
 * Perform a checkpoint --- either during shutdown, or on-the-fly
 
264
 */
 
265
void
 
266
CheckPointSUBTRANS(void)
 
267
{
 
268
        /*
 
269
         * Flush dirty SUBTRANS pages to disk
 
270
         *
 
271
         * This is not actually necessary from a correctness point of view. We do
 
272
         * it merely to improve the odds that writing of dirty pages is done
 
273
         * by the checkpoint process and not by backends.
 
274
         */
 
275
        SimpleLruFlush(SubTransCtl, true);
 
276
}
 
277
 
 
278
 
 
279
/*
 
280
 * Make sure that SUBTRANS has room for a newly-allocated XID.
 
281
 *
 
282
 * NB: this is called while holding XidGenLock.  We want it to be very fast
 
283
 * most of the time; even when it's not so fast, no actual I/O need happen
 
284
 * unless we're forced to write out a dirty subtrans page to make room
 
285
 * in shared memory.
 
286
 */
 
287
void
 
288
ExtendSUBTRANS(TransactionId newestXact)
 
289
{
 
290
        int                     pageno;
 
291
 
 
292
        /*
 
293
         * No work except at first XID of a page.  But beware: just after
 
294
         * wraparound, the first XID of page zero is FirstNormalTransactionId.
 
295
         */
 
296
        if (TransactionIdToEntry(newestXact) != 0 &&
 
297
                !TransactionIdEquals(newestXact, FirstNormalTransactionId))
 
298
                return;
 
299
 
 
300
        pageno = TransactionIdToPage(newestXact);
 
301
 
 
302
        LWLockAcquire(SubtransControlLock, LW_EXCLUSIVE);
 
303
 
 
304
        /* Zero the page */
 
305
        ZeroSUBTRANSPage(pageno);
 
306
 
 
307
        LWLockRelease(SubtransControlLock);
 
308
}
 
309
 
 
310
 
 
311
/*
 
312
 * Remove all SUBTRANS segments before the one holding the passed transaction ID
 
313
 *
 
314
 * This is normally called during checkpoint, with oldestXact being the
 
315
 * oldest TransactionXmin of any running transaction.
 
316
 */
 
317
void
 
318
TruncateSUBTRANS(TransactionId oldestXact)
 
319
{
 
320
        int                     cutoffPage;
 
321
 
 
322
        /*
 
323
         * The cutoff point is the start of the segment containing oldestXact.
 
324
         * We pass the *page* containing oldestXact to SimpleLruTruncate.
 
325
         */
 
326
        cutoffPage = TransactionIdToPage(oldestXact);
 
327
 
 
328
        SimpleLruTruncate(SubTransCtl, cutoffPage);
 
329
}
 
330
 
 
331
 
 
332
/*
 
333
 * Decide which of two SUBTRANS page numbers is "older" for truncation purposes.
 
334
 *
 
335
 * We need to use comparison of TransactionIds here in order to do the right
 
336
 * thing with wraparound XID arithmetic.  However, if we are asked about
 
337
 * page number zero, we don't want to hand InvalidTransactionId to
 
338
 * TransactionIdPrecedes: it'll get weird about permanent xact IDs.  So,
 
339
 * offset both xids by FirstNormalTransactionId to avoid that.
 
340
 */
 
341
static bool
 
342
SubTransPagePrecedes(int page1, int page2)
 
343
{
 
344
        TransactionId xid1;
 
345
        TransactionId xid2;
 
346
 
 
347
        xid1 = ((TransactionId) page1) * SUBTRANS_XACTS_PER_PAGE;
 
348
        xid1 += FirstNormalTransactionId;
 
349
        xid2 = ((TransactionId) page2) * SUBTRANS_XACTS_PER_PAGE;
 
350
        xid2 += FirstNormalTransactionId;
 
351
 
 
352
        return TransactionIdPrecedes(xid1, xid2);
 
353
}