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

Commit1c399de

Browse files
knizhnikkelvich
authored andcommitted
ALTER INDEX fix locking issues
1 parent4d8aa16 commit1c399de

File tree

4 files changed

+55
-16
lines changed

4 files changed

+55
-16
lines changed

‎src/backend/commands/indexcmds.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,15 @@ AlterIndex(Oid indexRelationId, IndexStmt *stmt)
309309
Assert(stmt->whereClause);
310310
CheckPredicate((Expr*)stmt->whereClause);
311311

312-
/* Open the target index relation */
313-
indexRelation=index_open(indexRelationId,RowExclusiveLock);
314-
namespaceId=RelationGetNamespace(indexRelation);
315-
316312
/* Open and lock the parent heap relation */
317313
heapRelationId=IndexGetRelation(indexRelationId, false);
318-
heapRelation=heap_open(heapRelationId,ShareLock);
314+
heapRelation=heap_open(heapRelationId,AccessShareLock);
315+
316+
/* Open the target index relation */
317+
/*indexRelation = index_open(indexRelationId, RowExclusiveLock); */
318+
//indexRelation = index_open(indexRelationId, ShareUpdateExclusiveLock);
319+
indexRelation=index_open(indexRelationId,AccessShareLock);
320+
namespaceId=RelationGetNamespace(indexRelation);
319321

320322
pg_index=heap_open(IndexRelationId,RowExclusiveLock);
321323

@@ -343,6 +345,7 @@ AlterIndex(Oid indexRelationId, IndexStmt *stmt)
343345
CatalogUpdateIndexes(pg_index,updatedTuple);
344346
heap_freetuple(updatedTuple);
345347
heap_freetuple(tuple);
348+
heap_close(pg_index,NoLock);
346349

347350
slot=MakeSingleTupleTableSlot(RelationGetDescr(heapRelation));
348351

@@ -396,7 +399,6 @@ AlterIndex(Oid indexRelationId, IndexStmt *stmt)
396399
ExecDropSingleTupleTableSlot(slot);
397400
FreeExecutorState(estate);
398401

399-
heap_close(pg_index,NoLock);
400402
heap_close(heapRelation,NoLock);
401403
index_close(indexRelation,NoLock);
402404
}

‎src/backend/tcop/utility.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1244,7 +1244,7 @@ ProcessUtilitySlow(Node *parsetree,
12441244
* eventually be needed here, so the lockmode calculation
12451245
* needs to match what DefineIndex() does.
12461246
*/
1247-
lockmode=stmt->concurrent ?ShareUpdateExclusiveLock
1247+
lockmode=stmt->is_alter ?AccessShareLock :stmt->concurrent ?ShareUpdateExclusiveLock
12481248
:ShareLock;
12491249
relid=
12501250
RangeVarGetRelidExtended(stmt->relation,lockmode,

‎src/bin/insbench/insbench.cpp‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include<inttypes.h>
66
#include<sys/time.h>
77
#include<pthread.h>
8+
#include<unistd.h>
89

910
#include<string>
1011
#include<vector>
@@ -13,6 +14,7 @@
1314
#include<pqxx/transaction>
1415
#include<pqxx/nontransaction>
1516
#include<pqxx/pipeline>
17+
#include<pqxx/version>
1618

1719
usingnamespacestd;
1820
usingnamespacepqxx;
@@ -39,6 +41,8 @@ struct config
3941
int nIndexes;
4042
int nIterations;
4143
int transactionSize;
44+
bool useSystemTime;
45+
bool noPK;
4246
string connection;
4347

4448
config() {
@@ -47,6 +51,8 @@ struct config
4751
nIndexes =8;
4852
nIterations =10000;
4953
transactionSize =100;
54+
useSystemTime =false;
55+
noPK =false;
5056
}
5157
};
5258

@@ -55,6 +61,7 @@ bool running;
5561
int nIndexUpdates;
5662
time_t maxIndexUpdateTime;
5763
time_t totalIndexUpdateTime;
64+
time_t currTimestamp;
5865

5966
#defineUSEC1000000
6067

@@ -79,14 +86,30 @@ void exec(transaction_base& txn, char const* sql, ...)
7986
void*inserter(void* arg)
8087
{
8188
connectioncon(cfg.connection);
82-
con.prepare("insert","insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)")("bigint")("bigint")("bigint")("bigint")("bigint")("bigint")("bigint")("bigint")("bigint");
89+
if (cfg.useSystemTime)
90+
{
91+
#if PQXX_VERSION_MAJOR >= 4
92+
con.prepare("insert","insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)");
93+
#else
94+
con.prepare("insert","insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)")("bigint")("bigint")("bigint")("bigint")("bigint")("bigint")("bigint")("bigint")("bigint");
95+
#endif
96+
}else {
97+
con.prepare("insert","insert into t (select generate_series($1::integer,$2::integer),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000))");
98+
}
99+
83100
for (int i =0; i < cfg.nIterations; i++)
84101
{
85102
worktxn(con);
86-
for (int j =0; j < cfg.transactionSize; j++)
87-
{
88-
txn.prepared("insert")(getCurrentTime())(random())(random())(random())(random())(random())(random())(random())(random()).exec();
89-
}
103+
if (cfg.useSystemTime)
104+
{
105+
for (int j =0; j < cfg.transactionSize; j++)
106+
{
107+
txn.prepared("insert")(getCurrentTime())(random())(random())(random())(random())(random())(random())(random())(random()).exec();
108+
}
109+
}else {
110+
currTimestamp = i*cfg.transactionSize;
111+
txn.prepared("insert")(i*cfg.transactionSize)((i+1)*cfg.transactionSize-1).exec();
112+
}
90113
txn.commit();
91114
}
92115
returnNULL;
@@ -97,14 +120,16 @@ void* indexUpdater(void* arg)
97120
connectioncon(cfg.connection);
98121
while (running) {
99122
sleep(cfg.indexUpdateInterval);
123+
printf("Alter indexes\n");
100124
time_t now =getCurrentTime();
101125
{
102126
worktxn(con);
103127
for (int i =0; i < cfg.nIndexes; i++) {
104-
exec(txn,"alter index idx%d where pk<%lu", i, now);
128+
exec(txn,"alter index idx%d where pk<%lu", i,cfg.useSystemTime ?now : currTimestamp);
105129
}
106130
txn.commit();
107131
}
132+
printf("End alter indexes\n");
108133
nIndexUpdates +=1;
109134
time_t elapsed =getCurrentTime() - now;
110135
totalIndexUpdateTime += elapsed;
@@ -122,12 +147,16 @@ void initializeDatabase()
122147
time_t now =getCurrentTime();
123148
exec(txn,"drop table if exists t");
124149
exec(txn,"create table t (pk bigint, k1 bigint, k2 bigint, k3 bigint, k4 bigint, k5 bigint, k6 bigint, k7 bigint, k8 bigint)");
125-
exec(txn,"create index pk on t(pk)");
150+
if (!cfg.noPK) {
151+
exec(txn,"create index pk on t(pk)");
152+
}
126153
for (int i =0; i < cfg.nIndexes; i++) {
127154
if (cfg.indexUpdateInterval ==0) {
128155
exec(txn,"create index idx%d on t(k%d)", i, i+1);
129-
}else {
156+
}elseif (cfg.useSystemTime){
130157
exec(txn,"create index idx%d on t(k%d) where pk<%ld", i, i+1, now);
158+
}else {
159+
exec(txn,"create index idx%d on t(k%d) where pk<%ld", i, i+1,0);
131160
}
132161
}
133162
txn.commit();
@@ -162,6 +191,12 @@ int main (int argc, char* argv[])
162191
case'c':
163192
cfg.connection =string(argv[++i]);
164193
continue;
194+
case'q':
195+
cfg.useSystemTime =true;
196+
continue;
197+
case'p':
198+
cfg.noPK =true;
199+
continue;
165200
}
166201
}
167202
printf("Options:\n"
@@ -170,6 +205,8 @@ int main (int argc, char* argv[])
170205
"\t-u N\tindex update interval (0)\n"
171206
"\t-n N\tnumber of iterations (10000)\n"
172207
"\t-i N\tnumber of indexes (8)\n"
208+
"\t-q\tuse system time and libpq\n"
209+
"\t-p\tno primary key\n"
173210
"\t-c STR\tdatabase connection string\n");
174211
return1;
175212
}

‎src/bin/insbench/makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ insbench: insbench.cpp
77
$(CXX)$(CXXFLAGS) -o insbench insbench.cpp -lpqxx
88

99
clean:
10-
rm -f insbench
10+
rm -f insbench

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp