Skip to content

Commit 0c84c13

Browse files
committed
workload/schemachange: only allow PK change in single statement transactions
This will avoid errors of the form: "cannot perform other schema changes in the same transaction as a primary key change." Release note: None
1 parent 92203cc commit 0c84c13

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

pkg/workload/schemachange/operation_generator.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ func (og *operationGenerator) getSupportedDeclarativeOp(
182182
// stochastic attempts and if verbosity is >= 2 the unsuccessful attempts are
183183
// recorded in `log` to help with debugging of the workload.
184184
func (og *operationGenerator) randOp(
185-
ctx context.Context, tx pgx.Tx, useDeclarativeSchemaChanger, allowDML bool,
185+
ctx context.Context, tx pgx.Tx, useDeclarativeSchemaChanger bool, numOpsInTxn int,
186186
) (stmt *opStmt, err error) {
187187
for {
188188
var op opType
@@ -195,8 +195,11 @@ func (og *operationGenerator) randOp(
195195
} else {
196196
op = opType(og.params.ops.Int())
197197
}
198-
if !allowDML && isDMLOpType(op) {
199-
continue
198+
if numOpsInTxn != 1 {
199+
// DML and legacy PK changes are only allowed in single-statement transactions.
200+
if isDMLOpType(op) || (op == alterTableAlterPrimaryKey && !useDeclarativeSchemaChanger) {
201+
continue
202+
}
200203
}
201204

202205
og.resetOpState(useDeclarativeSchemaChanger)

pkg/workload/schemachange/schemachange.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -456,12 +456,12 @@ func (w *schemaChangeWorker) runInTxn(
456456
) error {
457457
w.logger.startLog(w.id)
458458
w.logger.writeLog("BEGIN")
459-
opsNum := 1 + w.opGen.randIntn(w.maxOpsPerWorker)
460-
if useDeclarativeSchemaChanger && opsNum > w.workload.declarativeSchemaMaxStmtsPerTxn {
461-
opsNum = w.workload.declarativeSchemaMaxStmtsPerTxn
459+
numOps := 1 + w.opGen.randIntn(w.maxOpsPerWorker)
460+
if useDeclarativeSchemaChanger && numOps > w.workload.declarativeSchemaMaxStmtsPerTxn {
461+
numOps = w.workload.declarativeSchemaMaxStmtsPerTxn
462462
}
463463

464-
for i := 0; i < opsNum; i++ {
464+
for i := 0; i < numOps; i++ {
465465
// Terminating this loop early if there are expected commit errors prevents unexpected commit behavior from being
466466
// hidden by subsequent operations. Consider the case where there are expected commit errors.
467467
// It is possible that committing the transaction now will fail the workload because the error does not occur
@@ -474,8 +474,7 @@ func (w *schemaChangeWorker) runInTxn(
474474
break
475475
}
476476

477-
// Only allow DML for single-statement transactions.
478-
op, err := w.opGen.randOp(ctx, tx, useDeclarativeSchemaChanger, opsNum == 1)
477+
op, err := w.opGen.randOp(ctx, tx, useDeclarativeSchemaChanger, numOps)
479478
if pgErr := new(pgconn.PgError); errors.As(err, &pgErr) &&
480479
pgcode.MakeCode(pgErr.Code) == pgcode.SerializationFailure {
481480
return errors.Mark(err, errRunInTxnRbkSentinel)

0 commit comments

Comments
 (0)