Skip to content

Commit

Permalink
workload/schemachange/mixed-version: avoid index visibility
Browse files Browse the repository at this point in the history
Fixes: #87768

Previously, we were generating statements with invisible indexes in mixed
version environments, which could lead to statements generated not
supported on older versions. To address this, this patch will prevent
the generation of create table/ create index statements with invisible
indexes.

Release note: None
  • Loading branch information
fqazi committed Sep 22, 2022
1 parent 87bd307 commit 3410251
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions pkg/workload/schemachange/operation_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,13 +934,22 @@ func (og *operationGenerator) createIndex(ctx context.Context, tx pgx.Tx) (*opSt
return nil, err
}

// Only generate invisible indexes when they are supported.
invisibleIndexesIsNotSupported, err := isClusterVersionLessThan(
ctx,
tx,
clusterversion.ByKey(clusterversion.Start22_2))
if err != nil {
return nil, err
}

def := &tree.CreateIndex{
Name: tree.Name(indexName),
Table: *tableName,
Unique: og.randIntn(4) == 0, // 25% UNIQUE
Inverted: og.randIntn(10) == 0, // 10% INVERTED
IfNotExists: og.randIntn(2) == 0, // 50% IF NOT EXISTS
NotVisible: og.randIntn(20) == 0, // 5% NOT VISIBLE
Unique: og.randIntn(4) == 0, // 25% UNIQUE
Inverted: og.randIntn(10) == 0, // 10% INVERTED
IfNotExists: og.randIntn(2) == 0, // 50% IF NOT EXISTS
NotVisible: og.randIntn(20) == 0 && !invisibleIndexesIsNotSupported, // 5% NOT VISIBLE
}

regionColumn := ""
Expand Down Expand Up @@ -1207,6 +1216,26 @@ func (og *operationGenerator) createTable(ctx context.Context, tx pgx.Tx) (*opSt
return false
}()

invisibleIndexesIsNotSupported, err := isClusterVersionLessThan(
ctx,
tx,
clusterversion.ByKey(clusterversion.Start22_2))
if err != nil {
return nil, err
}
hasInvisibleIndexesUnsupported := func() bool {
if !invisibleIndexesIsNotSupported {
return false
}
// Check if any of the indexes have trigrams involved.
for _, def := range stmt.Defs {
if idx, ok := def.(*tree.IndexTableDef); ok && idx.NotVisible {
return true
}
}
return false
}()

tableExists, err := og.tableExists(ctx, tx, tableName)
if err != nil {
return nil, err
Expand All @@ -1224,6 +1253,7 @@ func (og *operationGenerator) createTable(ctx context.Context, tx pgx.Tx) (*opSt
// fully transaction aware.
codesWithConditions{
{code: pgcode.FeatureNotSupported, condition: hasTrigramIdxUnsupported},
{code: pgcode.Syntax, condition: hasInvisibleIndexesUnsupported},
}.add(opStmt.potentialExecErrors)
opStmt.sql = tree.Serialize(stmt)
return opStmt, nil
Expand Down

0 comments on commit 3410251

Please sign in to comment.