Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite33f205

Browse files
committed
Adjust btree index build procedure so that the btree metapage looks
invalid (has the wrong magic number) until the build is entirelycomplete. This turns out to cost no additional writes in the normalcase, since we were rewriting the metapage at the end of the processanyway. In normal scenarios there's no real gain in security, becausea failed index build would roll back the transaction leaving an unusedindex file, but for rebuilding shared system indexes this seems to addsome useful protection.
1 parent6b4caf5 commite33f205

File tree

6 files changed

+71
-28
lines changed

6 files changed

+71
-28
lines changed

‎doc/src/sgml/ref/reindex.sgml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/reindex.sgml,v 1.21 2003/09/24 18:54:01 tgl Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/reindex.sgml,v 1.22 2003/09/29 23:40:26 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -180,9 +180,10 @@ REINDEX { DATABASE | TABLE | INDEX } <replaceable class="PARAMETER">name</replac
180180
is crash-safe and transaction-safe. <command>REINDEX</> is not
181181
crash-safe for shared indexes, which is why this case is disallowed
182182
during normal operation. If a failure occurs while reindexing one
183-
of these catalogs in standalone mode, it is important that the failure
184-
be rectified and the <command>REINDEX</> operation redone
185-
before attempting to restart the regular server.
183+
of these catalogs in standalone mode, it will not be possible to
184+
restart the regular server until the problem is rectified. (The
185+
typical symptom of a partially rebuilt shared index is <quote>index is not
186+
a btree</> errors.)
186187
</para>
187188

188189
<para>

‎src/backend/access/nbtree/nbtpage.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtpage.c,v 1.71 2003/09/25 06:57:57 petere Exp $
12+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtpage.c,v 1.72 2003/09/29 23:40:26 tgl Exp $
1313
*
1414
*NOTES
1515
* Postgres btree pages look like ordinary relation pages.The opaque
@@ -31,12 +31,16 @@
3131
/*
3232
*_bt_metapinit() -- Initialize the metadata page of a new btree.
3333
*
34+
* If markvalid is true, the index is immediately marked valid, else it
35+
* will be invalid until _bt_metaproot() is called.
36+
*
3437
* Note: there's no real need for any locking here. Since the transaction
3538
* creating the index hasn't committed yet, no one else can even see the index
36-
* much less be trying to use it.
39+
* much less be trying to use it. (In a REINDEX-in-place scenario, that's
40+
* not true, but we assume the caller holds sufficient locks on the index.)
3741
*/
3842
void
39-
_bt_metapinit(Relationrel)
43+
_bt_metapinit(Relationrel,boolmarkvalid)
4044
{
4145
Bufferbuf;
4246
Pagepg;
@@ -57,7 +61,7 @@ _bt_metapinit(Relation rel)
5761
_bt_pageinit(pg,BufferGetPageSize(buf));
5862

5963
metad=BTPageGetMeta(pg);
60-
metad->btm_magic=BTREE_MAGIC;
64+
metad->btm_magic=markvalid ?BTREE_MAGIC :0;
6165
metad->btm_version=BTREE_VERSION;
6266
metad->btm_root=P_NONE;
6367
metad->btm_level=0;
@@ -85,7 +89,9 @@ _bt_metapinit(Relation rel)
8589
rdata[0].len=SizeOfBtreeNewmeta;
8690
rdata[0].next=NULL;
8791

88-
recptr=XLogInsert(RM_BTREE_ID,XLOG_BTREE_NEWMETA,rdata);
92+
recptr=XLogInsert(RM_BTREE_ID,
93+
markvalid ?XLOG_BTREE_NEWMETA :XLOG_BTREE_INVALIDMETA,
94+
rdata);
8995

9096
PageSetLSN(pg,recptr);
9197
PageSetSUI(pg,ThisStartUpID);
@@ -611,6 +617,8 @@ _bt_metaproot(Relation rel, BlockNumber rootbknum, uint32 level)
611617
START_CRIT_SECTION();
612618

613619
metad=BTPageGetMeta(metap);
620+
Assert(metad->btm_magic==BTREE_MAGIC||metad->btm_magic==0);
621+
metad->btm_magic=BTREE_MAGIC;/* it's valid now for sure */
614622
metad->btm_root=rootbknum;
615623
metad->btm_level=level;
616624
metad->btm_fastroot=rootbknum;

‎src/backend/access/nbtree/nbtree.c

Lines changed: 3 additions & 2 deletions
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-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtree.c,v 1.105 2003/08/04 02:39:57 momjian Exp $
15+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtree.c,v 1.106 2003/09/29 23:40:26 tgl Exp $
1616
*
1717
*-------------------------------------------------------------------------
1818
*/
@@ -112,7 +112,8 @@ btbuild(PG_FUNCTION_ARGS)
112112
RelationGetRelationName(index));
113113

114114
/* initialize the btree index metadata page */
115-
_bt_metapinit(index);
115+
/* mark it valid right away only if using slow build */
116+
_bt_metapinit(index, !buildstate.usefast);
116117

117118
if (buildstate.usefast)
118119
{

‎src/backend/access/nbtree/nbtsort.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* Portions Copyright (c) 1994, Regents of the University of California
3737
*
3838
* IDENTIFICATION
39-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsort.c,v 1.76 2003/09/25 06:57:57 petere Exp $
39+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtsort.c,v 1.77 2003/09/29 23:40:26 tgl Exp $
4040
*
4141
*-------------------------------------------------------------------------
4242
*/
@@ -514,6 +514,8 @@ static void
514514
_bt_uppershutdown(Relationindex,BTPageState*state)
515515
{
516516
BTPageState*s;
517+
BlockNumberrootblkno=P_NONE;
518+
uint32rootlevel=0;
517519

518520
/*
519521
* Each iteration of this loop completes one more level of the tree.
@@ -537,7 +539,8 @@ _bt_uppershutdown(Relation index, BTPageState *state)
537539
if (s->btps_next== (BTPageState*)NULL)
538540
{
539541
opaque->btpo_flags |=BTP_ROOT;
540-
_bt_metaproot(index,blkno,s->btps_level);
542+
rootblkno=blkno;
543+
rootlevel=s->btps_level;
541544
}
542545
else
543546
{
@@ -556,6 +559,14 @@ _bt_uppershutdown(Relation index, BTPageState *state)
556559
_bt_slideleft(index,s->btps_buf,s->btps_page);
557560
_bt_blwritepage(index,s->btps_buf);
558561
}
562+
563+
/*
564+
* As the last step in the process, update the metapage to point to
565+
* the new root (unless we had no data at all, in which case it's
566+
* left pointing to "P_NONE"). This changes the index to the "valid"
567+
* state by updating its magic number.
568+
*/
569+
_bt_metaproot(index,rootblkno,rootlevel);
559570
}
560571

561572
/*
@@ -672,7 +683,6 @@ _bt_load(Relation index, BTSpool *btspool, BTSpool *btspool2)
672683
}
673684
}
674685

675-
/* Close down final pages, if we had any data at all */
676-
if (state!=NULL)
677-
_bt_uppershutdown(index,state);
686+
/* Close down final pages and rewrite the metapage */
687+
_bt_uppershutdown(index,state);
678688
}

‎src/backend/access/nbtree/nbtxlog.c

Lines changed: 32 additions & 10 deletions
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-
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtxlog.c,v 1.6 2003/08/08 21:41:27 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/access/nbtree/nbtxlog.c,v 1.7 2003/09/29 23:40:26 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -109,7 +109,8 @@ _bt_restore_page(Page page, char *from, int len)
109109
staticvoid
110110
_bt_restore_meta(Relationreln,XLogRecPtrlsn,
111111
BlockNumberroot,uint32level,
112-
BlockNumberfastroot,uint32fastlevel)
112+
BlockNumberfastroot,uint32fastlevel,
113+
boolmarkvalid)
113114
{
114115
Buffermetabuf;
115116
Pagemetapg;
@@ -124,7 +125,7 @@ _bt_restore_meta(Relation reln, XLogRecPtr lsn,
124125
_bt_pageinit(metapg,BufferGetPageSize(metabuf));
125126

126127
md=BTPageGetMeta(metapg);
127-
md->btm_magic=BTREE_MAGIC;
128+
md->btm_magic=markvalid ?BTREE_MAGIC :0;
128129
md->btm_version=BTREE_VERSION;
129130
md->btm_root=root;
130131
md->btm_level=level;
@@ -213,7 +214,8 @@ btree_xlog_insert(bool redo, bool isleaf, bool ismeta,
213214
if (ismeta)
214215
_bt_restore_meta(reln,lsn,
215216
md.root,md.level,
216-
md.fastroot,md.fastlevel);
217+
md.fastroot,md.fastlevel,
218+
true);
217219
}
218220

219221
/* Forget any split this insertion completes */
@@ -562,7 +564,8 @@ btree_xlog_delete_page(bool redo, bool ismeta,
562564
sizeof(xl_btree_metadata));
563565
_bt_restore_meta(reln,lsn,
564566
md.root,md.level,
565-
md.fastroot,md.fastlevel);
567+
md.fastroot,md.fastlevel,
568+
true);
566569
}
567570
}
568571
}
@@ -607,7 +610,8 @@ btree_xlog_newroot(bool redo, XLogRecPtr lsn, XLogRecord *record)
607610

608611
_bt_restore_meta(reln,lsn,
609612
xlrec->rootblk,xlrec->level,
610-
xlrec->rootblk,xlrec->level);
613+
xlrec->rootblk,xlrec->level,
614+
true);
611615

612616
/* Check to see if this satisfies any incomplete insertions */
613617
if (record->xl_len>SizeOfBtreeNewroot&&
@@ -621,7 +625,8 @@ btree_xlog_newroot(bool redo, XLogRecPtr lsn, XLogRecord *record)
621625
}
622626

623627
staticvoid
624-
btree_xlog_newmeta(boolredo,XLogRecPtrlsn,XLogRecord*record)
628+
btree_xlog_newmeta(boolredo,XLogRecPtrlsn,XLogRecord*record,
629+
boolmarkvalid)
625630
{
626631
xl_btree_newmeta*xlrec= (xl_btree_newmeta*)XLogRecGetData(record);
627632
Relationreln;
@@ -635,7 +640,8 @@ btree_xlog_newmeta(bool redo, XLogRecPtr lsn, XLogRecord *record)
635640

636641
_bt_restore_meta(reln,lsn,
637642
xlrec->meta.root,xlrec->meta.level,
638-
xlrec->meta.fastroot,xlrec->meta.fastlevel);
643+
xlrec->meta.fastroot,xlrec->meta.fastlevel,
644+
markvalid);
639645
}
640646

641647
staticvoid
@@ -707,11 +713,14 @@ btree_redo(XLogRecPtr lsn, XLogRecord *record)
707713
btree_xlog_newroot(true,lsn,record);
708714
break;
709715
caseXLOG_BTREE_NEWMETA:
710-
btree_xlog_newmeta(true,lsn,record);
716+
btree_xlog_newmeta(true,lsn,record, true);
711717
break;
712718
caseXLOG_BTREE_NEWPAGE:
713719
btree_xlog_newpage(true,lsn,record);
714720
break;
721+
caseXLOG_BTREE_INVALIDMETA:
722+
btree_xlog_newmeta(true,lsn,record, false);
723+
break;
715724
default:
716725
elog(PANIC,"btree_redo: unknown op code %u",info);
717726
}
@@ -758,11 +767,14 @@ btree_undo(XLogRecPtr lsn, XLogRecord *record)
758767
btree_xlog_newroot(false,lsn,record);
759768
break;
760769
caseXLOG_BTREE_NEWMETA:
761-
btree_xlog_newmeta(false,lsn,record);
770+
btree_xlog_newmeta(false,lsn,record, true);
762771
break;
763772
caseXLOG_BTREE_NEWPAGE:
764773
btree_xlog_newpage(false,lsn,record);
765774
break;
775+
caseXLOG_BTREE_INVALIDMETA:
776+
btree_xlog_newmeta(false,lsn,record, false);
777+
break;
766778
default:
767779
elog(PANIC,"btree_undo: unknown op code %u",info);
768780
}
@@ -895,6 +907,16 @@ btree_desc(char *buf, uint8 xl_info, char *rec)
895907
xlrec->blkno);
896908
break;
897909
}
910+
caseXLOG_BTREE_INVALIDMETA:
911+
{
912+
xl_btree_newmeta*xlrec= (xl_btree_newmeta*)rec;
913+
914+
sprintf(buf+strlen(buf),"invalidmeta: node %u/%u; root %u lev %u fast %u lev %u",
915+
xlrec->node.tblNode,xlrec->node.relNode,
916+
xlrec->meta.root,xlrec->meta.level,
917+
xlrec->meta.fastroot,xlrec->meta.fastlevel);
918+
break;
919+
}
898920
default:
899921
strcat(buf,"UNKNOWN");
900922
break;

‎src/include/access/nbtree.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: nbtree.h,v 1.70 2003/08/08 21:42:32 momjian Exp $
10+
* $Id: nbtree.h,v 1.71 2003/09/29 23:40:26 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -198,6 +198,7 @@ typedef BTItemData *BTItem;
198198
#defineXLOG_BTREE_NEWROOT0xA0/* new root page */
199199
#defineXLOG_BTREE_NEWMETA0xB0/* update metadata page */
200200
#defineXLOG_BTREE_NEWPAGE0xC0/* new index page during build */
201+
#defineXLOG_BTREE_INVALIDMETA0xD0/* new metadata, temp. invalid */
201202

202203
/*
203204
* All that we need to find changed index tuple
@@ -448,7 +449,7 @@ extern void _bt_insert_parent(Relation rel, Buffer buf, Buffer rbuf,
448449
/*
449450
* prototypes for functions in nbtpage.c
450451
*/
451-
externvoid_bt_metapinit(Relationrel);
452+
externvoid_bt_metapinit(Relationrel,boolmarkvalid);
452453
externBuffer_bt_getroot(Relationrel,intaccess);
453454
externBuffer_bt_gettrueroot(Relationrel);
454455
externBuffer_bt_getbuf(Relationrel,BlockNumberblkno,intaccess);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp