The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
# The query_execution table needs to be partitioned on postgres.
#
# Attach the query_execution_old table as a partition. This partition covers up
# to the current month, plus one more after, in order to handle the case where
# the migration runs at the end of the month and query executions need to be
# written to disk before the job to create the next partition gets a chance to
# run.
#
# The IDENTITY has to be dropped since this can't be done in a partition; it
# should be handled by the higher level table.
#
# The old query_execution table is now a partition of the new query_execution
# table, but the v_query_log view still references the old one. Re-creating it
# will point it at the new one.
#
# We should use sqlFile for this, but that is blocked because you can't mix sql
# and sqlFile in the same migration because mysql sucks, so sadly we have to
# copy/paste the entire contents of postgres-query_log.sql into here instead.
# Splitting into a separate file doesn't work because the rollback order would
# be wrong; it needs to be after this one for going forward but before this one
# for rollback.
databaseChangeLog:
- changeSet:
id: v64.2026-07-23T11:39:56
author: technomancy
comment: Create partitioned query_execution table and attach old one
# had to edit this after the fact due to issues with stats schema diverging
# from the schema in dev/test
validCheckSum: ANY
dbms: postgresql
changes:
- sql:
sql: >-
ALTER TABLE query_execution DROP CONSTRAINT query_execution_pkey;
ALTER TABLE query_execution RENAME TO query_execution_old;
CREATE TABLE query_execution (LIKE query_execution_old INCLUDING ALL)
PARTITION BY RANGE (started_at);
SELECT setval(pg_get_serial_sequence('query_execution', 'id'),
nextval(pg_get_serial_sequence('query_execution_old', 'id')));
ALTER TABLE query_execution_old ALTER COLUMN id DROP IDENTITY IF EXISTS;
ALTER TABLE query_execution ATTACH PARTITION query_execution_old
FOR VALUES FROM ('1970-01-01')
TO (date_trunc('month', current_timestamp + '2 months'::interval));
DROP VIEW IF EXISTS v_query_log;
CREATE OR REPLACE VIEW v_query_log AS
SELECT id AS entity_id,
started_at,
cast(running_time AS double precision) / 1000 AS running_time_seconds,
result_rows,
native AS is_native,
context AS query_source,
error,
coalesce(executor_id, 0) AS user_id,
card_id,
'card_' || card_id AS card_qualified_id,
dashboard_id,
'dashboard_' || dashboard_id AS dashboard_qualified_id,
pulse_id,
database_id,
'database_' || database_id AS database_qualified_id,
cache_hit,
action_id,
'action_' || action_id AS action_qualified_id,
transform_id,
'transform_' || transform_id AS transform_qualified_id,
lens_id,
lens_params,
query,
embedding_client,
embedding_client_identifier,
embedding_route,
CASE WHEN embedding_client LIKE '%-preview' THEN TRUE ELSE FALSE END AS is_preview,
embedding_sdk_version,
metabase_version,
auth_method,
is_sandboxed,
is_impersonated,
is_db_routed,
parameters,
tenant_id,
embedding_hostname,
embedding_path,
user_agent,
sanitized_user_agent,
ip_address
FROM query_execution
LEFT JOIN query ON query_execution.hash = query.query_hash;
rollback:
- sql:
sql: >-
DROP VIEW IF EXISTS v_query_log;
ALTER TABLE query_execution DETACH PARTITION query_execution_old;
ALTER TABLE query_execution_old ALTER COLUMN id
ADD GENERATED BY DEFAULT AS IDENTITY;
SELECT setval(pg_get_serial_sequence('query_execution_old', 'id'),
nextval(pg_get_serial_sequence('query_execution', 'id')));
DROP TABLE query_execution;
ALTER TABLE query_execution_old RENAME TO query_execution;
ALTER TABLE query_execution
ADD CONSTRAINT query_execution_pkey PRIMARY KEY(ID);
CREATE OR REPLACE VIEW v_query_log AS
SELECT id AS entity_id,
started_at,
cast(running_time AS double precision) / 1000 AS running_time_seconds,
result_rows,
native AS is_native,
context AS query_source,
error,
coalesce(executor_id, 0) AS user_id,
card_id,
'card_' || card_id AS card_qualified_id,
dashboard_id,
'dashboard_' || dashboard_id AS dashboard_qualified_id,
pulse_id,
database_id,
'database_' || database_id AS database_qualified_id,
cache_hit,
action_id,
'action_' || action_id AS action_qualified_id,
transform_id,
'transform_' || transform_id AS transform_qualified_id,
lens_id,
lens_params,
query,
embedding_client,
embedding_client_identifier,
embedding_route,
CASE WHEN embedding_client LIKE '%-preview' THEN TRUE ELSE FALSE END AS is_preview,
embedding_sdk_version,
metabase_version,
auth_method,
is_sandboxed,
is_impersonated,
is_db_routed,
parameters,
tenant_id,
embedding_hostname,
embedding_path,
user_agent,
sanitized_user_agent,
ip_address
FROM query_execution
LEFT JOIN query ON query_execution.hash = query.query_hash;