Skip to content

Commit 4dc6463

Browse files
committed
workload/schemachanger: apply statement_timeout out to CTAS
Previously, the schema changer workload never used statement_timeouts since schema changes are normally not predictable operations. However, since we added CREATE TABLE AS support, its possible to have multiple joins where a CREATE TABLE AS can take more then 5 minutes. This patch modifies the operation generator support adding statement timeouts for individual statements. Fixes: #148342 Release note: None
1 parent 4bbdd4d commit 4dc6463

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pkg/workload/schemachange/operation_generator.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,10 @@ func (og *operationGenerator) createTableAs(ctx context.Context, tx pgx.Tx) (*op
14971497
if opStmt.expectedExecErrors.empty() {
14981498
opStmt.potentialExecErrors.merge(getValidGenerationErrors())
14991499
}
1500+
// Limit any CTAS statements to a maximum time of 1 minute.
1501+
opStmt.statementTimeout = time.Minute
1502+
opStmt.potentialExecErrors.add(pgcode.QueryCanceled)
1503+
og.potentialCommitErrors.add(pgcode.QueryCanceled)
15001504

15011505
opStmt.sql = fmt.Sprintf(`CREATE TABLE %s AS %s FETCH FIRST %d ROWS ONLY`,
15021506
destTableName, selectStatement.String(), MaxRowsToConsume)
@@ -3091,6 +3095,8 @@ type opStmt struct {
30913095
// potentialExecErrors errors that could be potentially seen on execution.
30923096
potentialExecErrors errorCodeSet
30933097
queryResultCallback opStmtQueryResultCallback
3098+
// statementTimeout if this statement has a timeout.
3099+
statementTimeout time.Duration
30943100
}
30953101

30963102
// String implements Stringer
@@ -3207,6 +3213,16 @@ func (og *operationGenerator) WrapWithErrorState(err error, op *opStmt) error {
32073213
func (s *opStmt) executeStmt(ctx context.Context, tx pgx.Tx, og *operationGenerator) error {
32083214
var err error
32093215
var rows pgx.Rows
3216+
// Apply any timeout for this statement
3217+
if s.statementTimeout > 0 {
3218+
_, err = tx.Exec(ctx, fmt.Sprintf("SET LOCAL statement_timeout='%s'", s.statementTimeout.String()))
3219+
if err != nil {
3220+
return errors.Mark(
3221+
og.WrapWithErrorState(errors.Wrap(err, "***UNEXPECTED ERROR; Unable to set statement timeout."), s),
3222+
errRunInTxnFatalSentinel,
3223+
)
3224+
}
3225+
}
32103226
// Statement doesn't produce any result set that needs to be validated.
32113227
if s.queryResultCallback == nil {
32123228
_, err = tx.Exec(ctx, s.sql)
@@ -3280,6 +3296,16 @@ func (s *opStmt) executeStmt(ctx context.Context, tx pgx.Tx, og *operationGenera
32803296
return err
32813297
}
32823298
}
3299+
// Reset any timeout for this statement
3300+
if s.statementTimeout > 0 {
3301+
_, err = tx.Exec(ctx, "SET LOCAL statement_timeout=0")
3302+
if err != nil {
3303+
return errors.Mark(
3304+
og.WrapWithErrorState(errors.Wrap(err, "***UNEXPECTED ERROR; Unable to reset statement timeout."), s),
3305+
errRunInTxnFatalSentinel,
3306+
)
3307+
}
3308+
}
32833309
return nil
32843310
}
32853311

0 commit comments

Comments
 (0)