Skip to content

Commit b2e3187

Browse files
craig[bot]rafiss
andcommitted
Merge #119029
119029: workload/schemachange: fix and re-enable ALTER FUNCTION r=rafiss a=rafiss A few fixes were needed: - Include UDF params when generating ALTER FUNCTION statement. - Changed an errors.Is call to errors.HasType. - Avoid only using the public schema when creating functions. fixes #116794 Release note: None Co-authored-by: Rafi Shamim <[email protected]>
2 parents 2420e5c + 8fb47cd commit b2e3187

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

pkg/workload/schemachange/generate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@ func Generate[T tree.Statement](
187187
aIsSuccess := a.Code == pgcode.SuccessfulCompletion
188188
bIsSuccess := b.Code == pgcode.SuccessfulCompletion
189189

190+
if aIsSuccess && bIsSuccess {
191+
// If both are valid, choose randomly.
192+
return rng.Float64() < 0.5
193+
}
190194
return aIsSuccess && !bIsSuccess
191195
})
192196

pkg/workload/schemachange/operation_generator.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ func (og *operationGenerator) randOp(
198198
// We can only ignore this error, if no other PgErrors
199199
// were set in the clean up process.
200200
if errors.Is(err, pgx.ErrNoRows) &&
201-
!errors.Is(err, &pgconn.PgError{}) {
201+
!errors.HasType(err, &pgconn.PgError{}) {
202202
continue
203203
}
204204
// Table select had a primary key swap, so no statement
@@ -3878,11 +3878,18 @@ func (og *operationGenerator) createFunction(ctx context.Context, tx pgx.Tx) (*o
38783878
COALESCE(member->>'direction' = 'REMOVE', false) AS dropping
38793879
FROM enum_members
38803880
`)
3881+
schemasQuery := With([]CTE{
3882+
{"descriptors", descJSONQuery},
3883+
}, `SELECT quote_ident(name) FROM descriptors WHERE descriptor ? 'schema'`)
38813884

38823885
enums, err := Collect(ctx, og, tx, pgx.RowToMap, enumQuery)
38833886
if err != nil {
38843887
return nil, err
38853888
}
3889+
schemas, err := Collect(ctx, og, tx, pgx.RowTo[string], schemasQuery)
3890+
if err != nil {
3891+
return nil, err
3892+
}
38863893

38873894
// Roll some variables to ensure we have variance in the types of references
38883895
// that we aside from being bound by what we could make references to.
@@ -3916,6 +3923,9 @@ func (og *operationGenerator) createFunction(ctx context.Context, tx pgx.Tx) (*o
39163923
name := tree.Name(fmt.Sprintf("udf_%s", og.newUniqueSeqNumSuffix()))
39173924
return &name
39183925
},
3926+
"Schema": func() (string, error) {
3927+
return PickOne(og.params.rng, schemas)
3928+
},
39193929
"DroppingEnum": func() (string, error) {
39203930
return PickOne(og.params.rng, droppingEnums)
39213931
},
@@ -3947,9 +3957,9 @@ func (og *operationGenerator) createFunction(ctx context.Context, tx pgx.Tx) (*o
39473957
// to the schema workload but may become a nice to have.
39483958
stmt, expectedCode, err := Generate[*tree.CreateRoutine](og.params.rng, og.produceError(), []GenerationCase{
39493959
// 1. Nothing special, fully self contained function.
3950-
{pgcode.SuccessfulCompletion, `CREATE FUNCTION { UniqueName } (i int, j int) RETURNS VOID LANGUAGE SQL AS $$ SELECT NULL $$`},
3960+
{pgcode.SuccessfulCompletion, `CREATE FUNCTION { Schema } . { UniqueName } (i int, j int) RETURNS VOID LANGUAGE SQL AS $$ SELECT NULL $$`},
39513961
// 2. 1 or more table or type references spread across parameters, return types, or the function body.
3952-
{pgcode.SuccessfulCompletion, `CREATE FUNCTION { UniqueName } ({ ParamRefs }) RETURNS { ReturnRefs } LANGUAGE SQL AS $$ SELECT NULL WHERE { BodyRefs } $$`},
3962+
{pgcode.SuccessfulCompletion, `CREATE FUNCTION { Schema } . { UniqueName } ({ ParamRefs }) RETURNS { ReturnRefs } LANGUAGE SQL AS $$ SELECT NULL WHERE { BodyRefs } $$`},
39533963
// 3. Reference a table that does not exist.
39543964
{pgcode.UndefinedTable, `CREATE FUNCTION { UniqueName } () RETURNS VOID LANGUAGE SQL AS $$ SELECT * FROM "ThisTableDoesNotExist" $$`},
39553965
// 4. Reference a UDT that does not exist.
@@ -4092,7 +4102,19 @@ func (og *operationGenerator) alterFunctionSetSchema(
40924102
functionsQuery := With([]CTE{
40934103
{"descriptors", descJSONQuery},
40944104
{"functions", functionDescsQuery},
4095-
}, `SELECT quote_ident(schema_id::REGNAMESPACE::TEXT) || '.' || quote_ident(name) FROM functions`)
4105+
}, `SELECT
4106+
quote_ident(schema_id::REGNAMESPACE::TEXT) || '.' || quote_ident(name) || '(' || array_to_string(funcargs, ', ') || ')'
4107+
FROM functions
4108+
JOIN LATERAL (
4109+
SELECT
4110+
COALESCE(array_agg(quote_ident(typnamespace::REGNAMESPACE::TEXT) || '.' || quote_ident(typname)), '{}') AS funcargs
4111+
FROM pg_catalog.pg_type
4112+
JOIN LATERAL (
4113+
SELECT unnest(proargtypes) AS oid FROM pg_catalog.pg_proc WHERE oid = (id + 100000)
4114+
) args ON args.oid = pg_type.oid
4115+
) funcargs ON TRUE
4116+
`,
4117+
)
40964118

40974119
schemasQuery := With([]CTE{
40984120
{"descriptors", descJSONQuery},

pkg/workload/schemachange/optype.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,10 +262,10 @@ var opWeights = []int{
262262
alterDatabaseDropSuperRegion: 0, // Disabled and tracked with #111299
263263
alterDatabasePrimaryRegion: 0, // Disabled and tracked with #83831
264264
alterDatabaseSurvivalGoal: 0, // Disabled and tracked with #83831
265-
alterFunctionRename: 0, // Disabled and tracked with #116794.
266-
alterFunctionSetSchema: 0, // Disabled and tracked with #116794.
265+
alterFunctionRename: 1,
266+
alterFunctionSetSchema: 1,
267267
alterTableAddColumn: 1,
268-
alterTableAddConstraintForeignKey: 1, // Tentatively re-enabled, see #91195.
268+
alterTableAddConstraintForeignKey: 1,
269269
alterTableAddConstraintUnique: 0,
270270
alterTableAlterColumnType: 0, // Disabled and tracked with #66662.
271271
alterTableAlterPrimaryKey: 1,
@@ -288,7 +288,7 @@ var opWeights = []int{
288288
createTableAs: 1,
289289
createTypeEnum: 1,
290290
createView: 1,
291-
dropFunction: 0, // Disabled and tracked with #116794.
291+
dropFunction: 1,
292292
dropIndex: 1,
293293
dropSchema: 0, // Disabled and tracked with 116792.
294294
dropSequence: 1,

0 commit comments

Comments
 (0)