Skip to content

Commit

Permalink
Fix partial view definition on CAggs
Browse files Browse the repository at this point in the history
When creating a CAgg using a column on the projection that is not part
of the `GROUP BY` clause but is functionally dependent of the primary
key of the referenced table is leading to a problem in dump/restore
because the wrong dependencies created changing the order and way dump
is generated.

Fixed it by copying the `Query` data structure of the `direct view` and
changing the necessary properties instead of creating it from scratch.
  • Loading branch information
fabriziomello committed Jul 25, 2024
1 parent d2b0213 commit bdfac29
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 17 deletions.
6 changes: 4 additions & 2 deletions scripts/test_pg_upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ def initialize_node(working_dir, prefix, port, bin_dir, base_dir):
node_old.safe_psql(dbname=pg_database_test, query="CREATE EXTENSION timescaledb;")
node_old.safe_psql(dbname=pg_database_test, filename="test/sql/updates/pre.testing.sql")
node_old.safe_psql(
dbname=pg_database_test,
filename=f"test/sql/updates/setup.{test_version}.sql",
dbname=pg_database_test, filename=f"test/sql/updates/setup.{test_version}.sql"
)
node_old.safe_psql(
dbname=pg_database_test, filename="test/sql/updates/setup.pg_upgrade.sql"
)
node_old.safe_psql(dbname=pg_database_test, query="CHECKPOINT")
node_old.safe_psql(dbname=pg_database_test, filename="test/sql/updates/setup.check.sql")
Expand Down
2 changes: 1 addition & 1 deletion scripts/test_update_from_version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ initdb > "${OUTPUT_DIR}/initdb.log" 2>&1
pg_ctl -l "${OUTPUT_DIR}/postgres.log" start -o "-c unix_socket_directories='${UNIX_SOCKET_DIR}' -c timezone=GMT -c client_min_messages=warning -c port=${PGPORT} -c max_prepared_transactions=100 -c shared_preload_libraries=timescaledb -c timescaledb.telemetry_level=off -c max_worker_processes=0"
pg_isready -t 30 > /dev/null

echo -e "\nUpdate test for ${FROM_VERSION} -> ${TO_VERSION}\n"
echo -e "\nUpdate test version ${TEST_VERSION} for ${FROM_VERSION} -> ${TO_VERSION}\n"

# caller should ensure that the versions are available
check_version "${FROM_VERSION}"
Expand Down
2 changes: 2 additions & 0 deletions test/sql/updates/post.pg_upgrade.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
\ir post.catalog.sql
\ir post.policies.sql
\ir post.functions.sql

SELECT * FROM cagg_join.measurement_daily ORDER BY 1, 2, 3, 4, 5, 6;
26 changes: 26 additions & 0 deletions test/sql/updates/setup.pg_upgrade.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.

CREATE SCHEMA cagg_join;
CREATE TABLE cagg_join.sensor(
id SERIAL PRIMARY KEY,
name TEXT,
enabled BOOLEAN
);

CREATE TABLE cagg_join.measurement(
sensor_id INTEGER REFERENCES cagg_join.sensor(id),
observed TIMESTAMPTZ,
value FLOAT
);

SELECT create_hypertable('cagg_join.measurement', 'observed');

CREATE MATERIALIZED VIEW cagg_join.measurement_daily
WITH (timescaledb.continuous) AS
-- Column s.name is functionally dependent on s.id (primary key)
SELECT s.id, s.name, time_bucket(interval '1 day', observed) as bucket, avg(value), min(value), max(value)
FROM cagg_join.sensor s
JOIN cagg_join.measurement m on (s.id = m.sensor_id)
GROUP BY s.id, bucket;
28 changes: 14 additions & 14 deletions tsl/src/continuous_aggs/create.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,25 +498,25 @@ mattablecolumninfo_get_partial_select_query(MatTableColumnInfo *mattblinfo, Quer
{
Query *partial_selquery = NULL;

CAGG_MAKEQUERY(partial_selquery, userview_query);
partial_selquery->rtable = copyObject(userview_query->rtable);
partial_selquery->jointree = copyObject(userview_query->jointree);
if (!finalized)
{
CAGG_MAKEQUERY(partial_selquery, userview_query);
partial_selquery->rtable = copyObject(userview_query->rtable);
partial_selquery->jointree = copyObject(userview_query->jointree);
#if PG16_GE
partial_selquery->rteperminfos = copyObject(userview_query->rteperminfos);
partial_selquery->rteperminfos = copyObject(userview_query->rteperminfos);
#endif

partial_selquery->targetList = mattblinfo->partial_seltlist;
partial_selquery->groupClause = mattblinfo->partial_grouplist;

if (finalized)
{
partial_selquery->havingQual = copyObject(userview_query->havingQual);
partial_selquery->sortClause = copyObject(userview_query->sortClause);
partial_selquery->targetList = mattblinfo->partial_seltlist;
partial_selquery->groupClause = mattblinfo->partial_grouplist;
partial_selquery->havingQual = NULL;
partial_selquery->sortClause = NULL;
}
else
{
partial_selquery->havingQual = NULL;
partial_selquery->sortClause = NULL;
partial_selquery = copyObject(userview_query);
/* Partial view should always include the time dimension column */
partial_selquery->targetList = mattblinfo->partial_seltlist;
partial_selquery->groupClause = mattblinfo->partial_grouplist;
}

return partial_selquery;
Expand Down

0 comments on commit bdfac29

Please sign in to comment.