Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Index on aggregation matview ignore schema namespace #7232

Open
stalkerg opened this issue Sep 4, 2024 · 4 comments
Open

[Bug]: Index on aggregation matview ignore schema namespace #7232

stalkerg opened this issue Sep 4, 2024 · 4 comments

Comments

@stalkerg
Copy link

stalkerg commented Sep 4, 2024

What type of bug is this?

Incorrect result, Other

What subsystems and features are affected?

Continuous aggregate

What happened?

You can't create two indexes with same name for two different continuous aggregation in two different schemas.

TimescaleDB version affected

2.15.3

PostgreSQL version used

16.3

What operating system did you use?

Cloud

What installation method did you use?

Not applicable

What platform did you run on?

Timescale Cloud

Relevant log output and stack trace

No response

How can we reproduce the bug?

Just create 2 continuous aggregations in two schemas and try to add indexes with the same name. 
It's happened because actual schema name for a such indexes is `_timescaledb_internal`.
@stalkerg stalkerg added the bug label Sep 4, 2024
@stalkerg
Copy link
Author

stalkerg commented Sep 4, 2024

It's basically bug in #4430 implementation.

@mkindahl
Copy link
Contributor

mkindahl commented Sep 4, 2024

@stalkerg Thanks for the bug report. Trivial to reproduce:

create table conditions(
    time timestamptz not null,
    location_id integer,
    device_id integer,
    temperature numeric,
    humidity numeric
);

select * from create_hypertable('conditions', 'time', migrate_data => true);

insert into conditions
select time, (random()*10 + 1)::int, (random()*10 + 1)::int, random()*80 - 40, random()*100
from generate_series(now() - interval '28 days', now(), '1 hour') as time;

create materialized view hourly.conditions_summary
with (timescaledb.continuous) as
select device_id,
       time_bucket('1 hour'::interval, "time") as bucket,
       avg(temperature),
       max(temperature),
       min(temperature)
from conditions
group by device_id, bucket;

create materialized view daily.conditions_summary
with (timescaledb.continuous) as
select device_id,
       time_bucket(interval '1 day', "time") as bucket,
       avg(temperature),
       max(temperature),
       min(temperature)
from conditions
group by device_id, bucket;

create index my_index on hourly.conditions_summary (device_id);
create index my_index on daily.conditions_summary (device_id);

@fabriziomello
Copy link
Contributor

It is not a but, it's a design decision because the underlying materialization hypertable is always created in the internal _timescaledb_internal schema and Postgres always create the index in the same index of the table.

But you can change the underlying materialization schema to another by doing something like:

ALTER TABLE _timescaledb_internal._materialized_hypertable_2 SET SCHEMA hourly;
ALTER TABLE _timescaledb_internal._materialized_hypertable_3 SET SCHEMA daily;

Then you can safely create the indexes with the same name but in different schemas.

@stalkerg
Copy link
Author

stalkerg commented Sep 9, 2024

Firstly, thank you for the extra workaround. Second, I understand the current design and why it's happened, but it still has bad UX and unexpected/undocumented behavior.
I suppose a materialization hypertable should be created in the specific schema or should be a way to set it during creation.
I have an idea why it was difficult to do it as a PG extension, but still.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants