diff --git a/backend/windmill-api-workspaces/src/datatable_extensions.rs b/backend/windmill-api-workspaces/src/datatable_extensions.rs index 493c216c45..b51cd9ea4b 100644 --- a/backend/windmill-api-workspaces/src/datatable_extensions.rs +++ b/backend/windmill-api-workspaces/src/datatable_extensions.rs @@ -75,11 +75,12 @@ struct Dimension { time_interval: Option, integer_interval: Option, num_partitions: Option, - /// The function the column's values go through before they are partitioned, with its argument, - /// as the catalog names it without one and it may be overloaded. Every name is qualified: the - /// copy resolves it after copying rows narrows its search path. None for - /// TimescaleDB's own hash, whose schema depends on the extension's version. - partitioning_func: Option, + /// The function the column's values go through before they are partitioned, as each immutable + /// overload taking the column's type or `anyelement`: the catalog names it without its + /// argument, and which overload TimescaleDB accepts is left to it. Every name is qualified, as + /// the copy resolves them after copying rows narrows its search path. Empty for TimescaleDB's + /// own hash, whose schema depends on the extension's version. + partitioning_funcs: Vec, } impl Hypertable { @@ -361,7 +362,9 @@ async fn read_hypertables( .query( "SELECT d.hypertable_schema::text, d.hypertable_name::text, d.column_name::text, d.time_interval::text, d.integer_interval::bigint, d.num_partitions::smallint, - (SELECT format('%I.%I(%I.%I)', n.nspname, p.proname, tn.nspname, t.typname) + (SELECT array_agg( + format('%I.%I(%I.%I)', n.nspname, p.proname, tn.nspname, t.typname) + ORDER BY p.proargtypes[0] = c.column_type DESC) FROM pg_proc p JOIN pg_namespace n ON n.oid = p.pronamespace JOIN pg_type t ON t.oid = p.proargtypes[0] JOIN pg_namespace tn ON tn.oid = t.typnamespace @@ -369,13 +372,9 @@ async fn read_hypertables( AND p.proname = c.partitioning_func AND p.pronargs = 1 AND p.proargtypes[0] IN (c.column_type, 'anyelement'::regtype) - -- TimescaleDB takes only an immutable one. Two immutable overloads - -- cannot share an argument type, so its return type need not be checked. AND p.provolatile = 'i' AND NOT (p.proname = 'get_partition_hash' - AND n.nspname IN ('_timescaledb_internal', '_timescaledb_functions')) - ORDER BY p.proargtypes[0] = c.column_type DESC - LIMIT 1) + AND n.nspname IN ('_timescaledb_internal', '_timescaledb_functions'))) FROM timescaledb_information.dimensions d JOIN timescaledb_information.hypertables h ON h.hypertable_schema = d.hypertable_schema @@ -396,7 +395,7 @@ async fn read_hypertables( time_interval: row.get(3), integer_interval: row.get(4), num_partitions: row.get(5), - partitioning_func: row.get(6), + partitioning_funcs: row.get::<_, Option>>(6).unwrap_or_default(), }; match plan.hypertables.last_mut() { Some(last) if last.schema == schema && last.name == name => { @@ -888,28 +887,49 @@ impl ExtensionPlan { }; let mut params: Vec<&(dyn ToSql + Sync)> = vec![&relation, &dimension.column]; params.extend(value); - // A column TimescaleDB cannot partition by itself, such as a time inside a jsonb, - // goes through its function. `create_hypertable` names the first one apart. - let partitioning = match &dimension.partitioning_func { - Some(func) => { - params.push(func); - format!( - ", {}partitioning_func => ${}::text::regprocedure::oid::regproc", - if i == 0 { "time_" } else { "" }, - params.len() - ) - } - None => String::new(), - }; - tx.execute( - &format!( + let call = |partitioning: &str| { + format!( "SELECT {timescaledb}.{function}($1::text::regclass, \ $2::text::name{argument}{partitioning}{extra})" - ), - ¶ms, - ) - .await - .map_err(|e| format!("{relation}: {}", pg_error_message(&e)))?; + ) + }; + if dimension.partitioning_funcs.is_empty() { + tx.execute(&call(""), ¶ms) + .await + .map_err(|e| format!("{relation}: {}", pg_error_message(&e)))?; + continue; + } + // A column TimescaleDB cannot partition by itself, such as a time inside a jsonb, + // goes through its function. Each overload is tried until TimescaleDB takes one. + // `create_hypertable` names the first dimension's apart. + let partitioning = format!( + ", {}partitioning_func => ${}::text::regprocedure::oid::regproc", + if i == 0 { "time_" } else { "" }, + params.len() + 1 + ); + let mut refused = None; + for func in &dimension.partitioning_funcs { + let mut with_func = params.clone(); + with_func.push(func); + tx.batch_execute("SAVEPOINT partitioning") + .await + .map_err(pg)?; + match tx.execute(&call(&partitioning), &with_func).await { + Ok(_) => { + refused = None; + break; + } + Err(e) => { + tx.batch_execute("ROLLBACK TO SAVEPOINT partitioning") + .await + .map_err(pg)?; + refused = Some(format!("{relation}: {}", pg_error_message(&e))); + } + } + } + if let Some(refused) = refused { + return Err(refused); + } } if let Some(source) = rows { copy_rows(source, &tx, hypertable)