Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 89bda95

Browse files
committed
Remove the 'slow' path for btree index build, which built the btree
incrementally by successive inserts rather than by sorting the data. We were only using the slow path during bootstrap, apparently because when first written it failed during bootstrap --- but it works fine now AFAICT. Removing it saves a hundred or so lines of code and produces noticeably (~10%) smaller initial states of the system catalog indexes. While that won't make much difference for heavily-modified catalogs, for the more static ones there may be a useful long-term performance improvement.
1 parent a8b8f4d commit 89bda95

File tree

4 files changed

+25
-161
lines changed

4 files changed

+25
-161
lines changed

src/backend/access/nbtree/nbtpage.c

+1-68
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtpage.c,v 1.94 2006/03/31 23:32:05 tgl Exp $
12+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtpage.c,v 1.95 2006/04/01 03:03:36 tgl Exp $
1313
*
1414
* NOTES
1515
* Postgres btree pages look like ordinary relation pages. The opaque
@@ -28,73 +28,6 @@
2828
#include "storage/lmgr.h"
2929

3030

31-
/*
32-
* _bt_metapinit() -- Initialize the metadata page of a new btree.
33-
*
34-
* Note: this is actually not used for standard btree index building;
35-
* nbtsort.c prefers not to make the metadata page valid until completion
36-
* of build.
37-
*
38-
* Note: there's no real need for any locking here. Since the transaction
39-
* creating the index hasn't committed yet, no one else can even see the index
40-
* much less be trying to use it. (In a REINDEX-in-place scenario, that's
41-
* not true, but we assume the caller holds sufficient locks on the index.)
42-
*/
43-
void
44-
_bt_metapinit(Relation rel)
45-
{
46-
Buffer buf;
47-
Page pg;
48-
BTMetaPageData *metad;
49-
50-
if (RelationGetNumberOfBlocks(rel) != 0)
51-
elog(ERROR, "cannot initialize non-empty btree index \"%s\"",
52-
RelationGetRelationName(rel));
53-
54-
buf = ReadBuffer(rel, P_NEW);
55-
Assert(BufferGetBlockNumber(buf) == BTREE_METAPAGE);
56-
LockBuffer(buf, BT_WRITE);
57-
pg = BufferGetPage(buf);
58-
59-
/* NO ELOG(ERROR) from here till newmeta op is logged */
60-
START_CRIT_SECTION();
61-
62-
_bt_initmetapage(pg, P_NONE, 0);
63-
metad = BTPageGetMeta(pg);
64-
65-
MarkBufferDirty(buf);
66-
67-
/* XLOG stuff */
68-
if (!rel->rd_istemp)
69-
{
70-
xl_btree_newmeta xlrec;
71-
XLogRecPtr recptr;
72-
XLogRecData rdata[1];
73-
74-
xlrec.node = rel->rd_node;
75-
xlrec.meta.root = metad->btm_root;
76-
xlrec.meta.level = metad->btm_level;
77-
xlrec.meta.fastroot = metad->btm_fastroot;
78-
xlrec.meta.fastlevel = metad->btm_fastlevel;
79-
80-
rdata[0].data = (char *) &xlrec;
81-
rdata[0].len = SizeOfBtreeNewmeta;
82-
rdata[0].buffer = InvalidBuffer;
83-
rdata[0].next = NULL;
84-
85-
recptr = XLogInsert(RM_BTREE_ID,
86-
XLOG_BTREE_NEWMETA,
87-
rdata);
88-
89-
PageSetLSN(pg, recptr);
90-
PageSetTLI(pg, ThisTimeLineID);
91-
}
92-
93-
END_CRIT_SECTION();
94-
95-
UnlockReleaseBuffer(buf);
96-
}
97-
9831
/*
9932
* _bt_initmetapage() -- Fill a page buffer with a correct metapage image
10033
*/

src/backend/access/nbtree/nbtree.c

+22-51
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* Portions Copyright (c) 1994, Regents of the University of California
1313
*
1414
* IDENTIFICATION
15-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.143 2006/03/31 23:32:05 tgl Exp $
15+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtree.c,v 1.144 2006/04/01 03:03:37 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -32,7 +32,6 @@
3232
/* Working state for btbuild and its callback */
3333
typedef struct
3434
{
35-
bool usefast;
3635
bool isUnique;
3736
bool haveDead;
3837
Relation heapRel;
@@ -48,8 +47,6 @@ typedef struct
4847
} BTBuildState;
4948

5049

51-
bool FastBuild = true; /* use SORT instead of insertion build */
52-
5350
static void _bt_restscan(IndexScanDesc scan);
5451
static void btbuildCallback(Relation index,
5552
HeapTuple htup,
@@ -71,13 +68,6 @@ btbuild(PG_FUNCTION_ARGS)
7168
double reltuples;
7269
BTBuildState buildstate;
7370

74-
/*
75-
* bootstrap processing does something strange, so don't use sort/build
76-
* for initial catalog indices. at some point i need to look harder at
77-
* this. (there is some kind of incremental processing going on there.)
78-
* -- pma 08/29/95
79-
*/
80-
buildstate.usefast = (FastBuild && IsNormalProcessingMode());
8171
buildstate.isUnique = indexInfo->ii_Unique;
8272
buildstate.haveDead = false;
8373
buildstate.heapRel = heap;
@@ -98,22 +88,14 @@ btbuild(PG_FUNCTION_ARGS)
9888
elog(ERROR, "index \"%s\" already contains data",
9989
RelationGetRelationName(index));
10090

101-
if (buildstate.usefast)
102-
{
103-
buildstate.spool = _bt_spoolinit(index, indexInfo->ii_Unique, false);
91+
buildstate.spool = _bt_spoolinit(index, indexInfo->ii_Unique, false);
10492

105-
/*
106-
* If building a unique index, put dead tuples in a second spool to
107-
* keep them out of the uniqueness check.
108-
*/
109-
if (indexInfo->ii_Unique)
110-
buildstate.spool2 = _bt_spoolinit(index, false, true);
111-
}
112-
else
113-
{
114-
/* if using slow build, initialize the btree index metadata page */
115-
_bt_metapinit(index);
116-
}
93+
/*
94+
* If building a unique index, put dead tuples in a second spool to
95+
* keep them out of the uniqueness check.
96+
*/
97+
if (indexInfo->ii_Unique)
98+
buildstate.spool2 = _bt_spoolinit(index, false, true);
11799

118100
/* do the heap scan */
119101
reltuples = IndexBuildHeapScan(heap, index, indexInfo,
@@ -128,17 +110,14 @@ btbuild(PG_FUNCTION_ARGS)
128110
}
129111

130112
/*
131-
* if we are doing bottom-up btree build, finish the build by (1)
132-
* completing the sort of the spool file, (2) inserting the sorted tuples
133-
* into btree pages and (3) building the upper levels.
113+
* Finish the build by (1) completing the sort of the spool file, (2)
114+
* inserting the sorted tuples into btree pages and (3) building the upper
115+
* levels.
134116
*/
135-
if (buildstate.usefast)
136-
{
137-
_bt_leafbuild(buildstate.spool, buildstate.spool2);
138-
_bt_spooldestroy(buildstate.spool);
139-
if (buildstate.spool2)
140-
_bt_spooldestroy(buildstate.spool2);
141-
}
117+
_bt_leafbuild(buildstate.spool, buildstate.spool2);
118+
_bt_spooldestroy(buildstate.spool);
119+
if (buildstate.spool2)
120+
_bt_spooldestroy(buildstate.spool2);
142121

143122
#ifdef BTREE_BUILD_STATS
144123
if (log_btree_build_stats)
@@ -173,24 +152,16 @@ btbuildCallback(Relation index,
173152
itup->t_tid = htup->t_self;
174153

175154
/*
176-
* if we are doing bottom-up btree build, we insert the index into a spool
177-
* file for subsequent processing. otherwise, we insert into the btree.
155+
* insert the index tuple into the appropriate spool file for subsequent
156+
* processing
178157
*/
179-
if (buildstate->usefast)
180-
{
181-
if (tupleIsAlive || buildstate->spool2 == NULL)
182-
_bt_spool(itup, buildstate->spool);
183-
else
184-
{
185-
/* dead tuples are put into spool2 */
186-
buildstate->haveDead = true;
187-
_bt_spool(itup, buildstate->spool2);
188-
}
189-
}
158+
if (tupleIsAlive || buildstate->spool2 == NULL)
159+
_bt_spool(itup, buildstate->spool);
190160
else
191161
{
192-
_bt_doinsert(index, itup,
193-
buildstate->isUnique, buildstate->heapRel);
162+
/* dead tuples are put into spool2 */
163+
buildstate->haveDead = true;
164+
_bt_spool(itup, buildstate->spool2);
194165
}
195166

196167
buildstate->indtuples += 1;

src/backend/access/nbtree/nbtxlog.c

+1-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtxlog.c,v 1.30 2006/03/31 23:32:05 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/access/nbtree/nbtxlog.c,v 1.31 2006/04/01 03:03:37 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -556,18 +556,6 @@ btree_xlog_newroot(XLogRecPtr lsn, XLogRecord *record)
556556
}
557557
}
558558

559-
static void
560-
btree_xlog_newmeta(XLogRecPtr lsn, XLogRecord *record)
561-
{
562-
xl_btree_newmeta *xlrec = (xl_btree_newmeta *) XLogRecGetData(record);
563-
Relation reln;
564-
565-
reln = XLogOpenRelation(xlrec->node);
566-
_bt_restore_meta(reln, lsn,
567-
xlrec->meta.root, xlrec->meta.level,
568-
xlrec->meta.fastroot, xlrec->meta.fastlevel);
569-
}
570-
571559

572560
void
573561
btree_redo(XLogRecPtr lsn, XLogRecord *record)
@@ -609,9 +597,6 @@ btree_redo(XLogRecPtr lsn, XLogRecord *record)
609597
case XLOG_BTREE_NEWROOT:
610598
btree_xlog_newroot(lsn, record);
611599
break;
612-
case XLOG_BTREE_NEWMETA:
613-
btree_xlog_newmeta(lsn, record);
614-
break;
615600
default:
616601
elog(PANIC, "btree_redo: unknown op code %u", info);
617602
}
@@ -727,17 +712,6 @@ btree_desc(StringInfo buf, uint8 xl_info, char *rec)
727712
xlrec->rootblk, xlrec->level);
728713
break;
729714
}
730-
case XLOG_BTREE_NEWMETA:
731-
{
732-
xl_btree_newmeta *xlrec = (xl_btree_newmeta *) rec;
733-
734-
appendStringInfo(buf, "newmeta: rel %u/%u/%u; root %u lev %u fast %u lev %u",
735-
xlrec->node.spcNode, xlrec->node.dbNode,
736-
xlrec->node.relNode,
737-
xlrec->meta.root, xlrec->meta.level,
738-
xlrec->meta.fastroot, xlrec->meta.fastlevel);
739-
break;
740-
}
741715
default:
742716
appendStringInfo(buf, "UNKNOWN");
743717
break;

src/include/access/nbtree.h

+1-15
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/access/nbtree.h,v 1.94 2006/03/31 23:32:06 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/access/nbtree.h,v 1.95 2006/04/01 03:03:37 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -176,7 +176,6 @@ typedef struct BTMetaPageData
176176
#define XLOG_BTREE_DELETE_PAGE 0x80 /* delete an entire page */
177177
#define XLOG_BTREE_DELETE_PAGE_META 0x90 /* same, plus update metapage */
178178
#define XLOG_BTREE_NEWROOT 0xA0 /* new root page */
179-
#define XLOG_BTREE_NEWMETA 0xB0 /* update metadata page */
180179

181180
/*
182181
* All that we need to find changed index tuple
@@ -291,18 +290,6 @@ typedef struct xl_btree_newroot
291290

292291
#define SizeOfBtreeNewroot (offsetof(xl_btree_newroot, level) + sizeof(uint32))
293292

294-
/*
295-
* New metapage log record. This is not issued during routine operations;
296-
* it's only used when initializing an empty index.
297-
*/
298-
typedef struct xl_btree_newmeta
299-
{
300-
RelFileNode node;
301-
xl_btree_metadata meta;
302-
} xl_btree_newmeta;
303-
304-
#define SizeOfBtreeNewmeta (sizeof(xl_btree_newmeta))
305-
306293

307294
/*
308295
* Operator strategy numbers for B-tree have been moved to access/skey.h,
@@ -410,7 +397,6 @@ extern void _bt_insert_parent(Relation rel, Buffer buf, Buffer rbuf,
410397
/*
411398
* prototypes for functions in nbtpage.c
412399
*/
413-
extern void _bt_metapinit(Relation rel);
414400
extern void _bt_initmetapage(Page page, BlockNumber rootbknum, uint32 level);
415401
extern Buffer _bt_getroot(Relation rel, int access);
416402
extern Buffer _bt_gettrueroot(Relation rel);

0 commit comments

Comments
 (0)