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

Commit cd1aae5

Browse files
committed
Allow CREATE INDEX CONCURRENTLY to disregard transactions in other
databases, per gripe from hubert depesz lubaczewski. Patch from Simon Riggs.
1 parent f8942f4 commit cd1aae5

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

doc/src/sgml/ref/create_index.sgml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/ref/create_index.sgml,v 1.63 2007/06/03 17:05:53 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/ref/create_index.sgml,v 1.64 2007/09/07 00:58:56 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -308,7 +308,7 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] <replaceable class="parameter">name</re
308308
table. Other transactions can still read the table, but if they try to
309309
insert, update, or delete rows in the table they will block until the
310310
index build is finished. This could have a severe effect if the system is
311-
a live production database. Large tables can take many hours to be
311+
a live production database. Very large tables can take many hours to be
312312
indexed, and even for smaller tables, an index build can lock out writers
313313
for periods that are unacceptably long for a production system.
314314
</para>
@@ -319,7 +319,8 @@ CREATE [ UNIQUE ] INDEX [ CONCURRENTLY ] <replaceable class="parameter">name</re
319319
<literal>CONCURRENTLY</> option of <command>CREATE INDEX</>.
320320
When this option is used,
321321
<productname>PostgreSQL</> must perform two scans of the table, and in
322-
addition it must wait for all existing transactions to terminate. Thus
322+
addition it must wait for all existing transactions that could potentially
323+
use the index to terminate. Thus
323324
this method requires more total work than a standard index build and takes
324325
significantly longer to complete. However, since it allows normal
325326
operations to continue while the index is built, this method is useful for

src/backend/commands/indexcmds.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.163 2007/09/05 18:10:47 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/commands/indexcmds.c,v 1.164 2007/09/07 00:58:56 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -535,10 +535,12 @@ DefineIndex(RangeVar *heapRelation,
535535
*
536536
* We can exclude any running transactions that have xmin >= the xmax of
537537
* our reference snapshot, since they are clearly not interested in any
538-
* missing older tuples. Also, GetCurrentVirtualXIDs never reports our
539-
* own vxid, so we need not check for that.
538+
* missing older tuples. Transactions in other DBs aren't a problem
539+
* either, since they'll never even be able to see this index.
540+
* Also, GetCurrentVirtualXIDs never reports our own vxid, so we
541+
* need not check for that.
540542
*/
541-
old_snapshots = GetCurrentVirtualXIDs(ActiveSnapshot->xmax);
543+
old_snapshots = GetCurrentVirtualXIDs(ActiveSnapshot->xmax, false);
542544

543545
while (VirtualTransactionIdIsValid(*old_snapshots))
544546
{

src/backend/storage/ipc/procarray.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
*
2424
*
2525
* IDENTIFICATION
26-
* $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.30 2007/09/05 21:11:19 tgl Exp $
26+
* $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.31 2007/09/07 00:58:56 tgl Exp $
2727
*
2828
*-------------------------------------------------------------------------
2929
*/
@@ -891,10 +891,11 @@ IsBackendPid(int pid)
891891
* The array is palloc'd and is terminated with an invalid VXID.
892892
*
893893
* If limitXmin is not InvalidTransactionId, we skip any backends
894-
* with xmin >= limitXmin. Also, our own process is always skipped.
894+
* with xmin >= limitXmin. If allDbs is false, we skip backends attached
895+
* to other databases. Also, our own process is always skipped.
895896
*/
896897
VirtualTransactionId *
897-
GetCurrentVirtualXIDs(TransactionId limitXmin)
898+
GetCurrentVirtualXIDs(TransactionId limitXmin, bool allDbs)
898899
{
899900
VirtualTransactionId *vxids;
900901
ProcArrayStruct *arrayP = procArray;
@@ -910,24 +911,28 @@ GetCurrentVirtualXIDs(TransactionId limitXmin)
910911
for (index = 0; index < arrayP->numProcs; index++)
911912
{
912913
volatile PGPROC *proc = arrayP->procs[index];
913-
/* Fetch xmin just once - might change on us? */
914-
TransactionId pxmin = proc->xmin;
915914

916915
if (proc == MyProc)
917916
continue;
918917

919-
/*
920-
* Note that InvalidTransactionId precedes all other XIDs, so a
921-
* proc that hasn't set xmin yet will always be included.
922-
*/
923-
if (!TransactionIdIsValid(limitXmin) ||
924-
TransactionIdPrecedes(pxmin, limitXmin))
918+
if (allDbs || proc->databaseId == MyDatabaseId)
925919
{
926-
VirtualTransactionId vxid;
920+
/* Fetch xmin just once - might change on us? */
921+
TransactionId pxmin = proc->xmin;
922+
923+
/*
924+
* Note that InvalidTransactionId precedes all other XIDs, so a
925+
* proc that hasn't set xmin yet will always be included.
926+
*/
927+
if (!TransactionIdIsValid(limitXmin) ||
928+
TransactionIdPrecedes(pxmin, limitXmin))
929+
{
930+
VirtualTransactionId vxid;
927931

928-
GET_VXID_FROM_PGPROC(vxid, *proc);
929-
if (VirtualTransactionIdIsValid(vxid))
930-
vxids[count++] = vxid;
932+
GET_VXID_FROM_PGPROC(vxid, *proc);
933+
if (VirtualTransactionIdIsValid(vxid))
934+
vxids[count++] = vxid;
935+
}
931936
}
932937
}
933938

src/include/storage/procarray.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.15 2007/09/05 18:10:48 tgl Exp $
10+
* $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.16 2007/09/07 00:58:57 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -33,7 +33,8 @@ extern PGPROC *BackendPidGetProc(int pid);
3333
extern int BackendXidGetPid(TransactionId xid);
3434
extern bool IsBackendPid(int pid);
3535

36-
extern VirtualTransactionId *GetCurrentVirtualXIDs(TransactionId limitXmin);
36+
extern VirtualTransactionId *GetCurrentVirtualXIDs(TransactionId limitXmin,
37+
bool allDbs);
3738
extern int CountActiveBackends(void);
3839
extern int CountDBBackends(Oid databaseid);
3940
extern int CountUserBackends(Oid roleid);

0 commit comments

Comments
 (0)