Skip to content

Commit

Permalink
Document cagg improvements (github#1529)
Browse files Browse the repository at this point in the history
* Document cagg improvements

Indexes can now be created directly on caggs by using the view name.
`ORDER BY` is supported in general, not just in the ordered-set
aggregates.

* Apply suggestions from code review

Co-authored-by: Lana Brindley <[email protected]>
  • Loading branch information
charislam and Loquacity authored Sep 6, 2022
1 parent 7641afe commit b54ecbe
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 23 deletions.
10 changes: 2 additions & 8 deletions _partials/_caggs-function-support.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ This table summarizes aggregate function support in continuous aggregates:
|-|-|-|
|Parallelizable aggregate functions|||
|Non-parallelizable aggregate functions|||
|`ORDER BY`|||
|Ordered-set aggregates|||
|Hypothetical-set aggregates|||
|`DISTINCT` in aggregate functions|||
|`FILTER` in aggregate functions|||
|`ORDER BY` in aggregate functions|||

<highlight type="note">
Note that `ORDER BY` is supported within the PostgreSQL
[ordered-set aggregates](https://www.postgresql.org/docs/current/functions-aggregate.html).
It is not yet supported in general.
</highlight>
|`FILTER` in aggregate functions|||
38 changes: 23 additions & 15 deletions timescaledb/how-to-guides/continuous-aggregates/create-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ keywords: [continuous aggregates, indexes]
---

# Create an index on a continuous aggregate

By default, some indexes are automatically created when you create a continuous
aggregate. You can change this behavior. You can also manually create and drop
indexes.

## Automatically created indexes

When you create a continuous aggregate, an index is automatically created for
each `GROUP BY` column. The index is a composite index, combining the `GROUP BY`
column with the `time_bucket` column.
Expand All @@ -19,10 +21,12 @@ location, bucket`, two composite indexes are created: one on `{device, bucket}`
and one on `{location, bucket}`.

### Turn off automatic index creation

To turn off automatic index creation, set `timescaledb.create_group_indexes` to
`false` when you create the continuous aggregate.

For example:

```sql
CREATE MATERIALIZED VIEW conditions_daily
WITH (timescaledb.continuous, timescaledb.create_group_indexes=false)
Expand All @@ -31,27 +35,31 @@ CREATE MATERIALIZED VIEW conditions_daily
```

## Manually create and drop indexes
You can manually create and drop indexes. To do so, you need to know the name of
your materialized hypertable. To find the name, see the instructions in the
[managing materialized hypertables][materialized-hypertable-name] section.

<highlight type="note">
The name you give when you run `CREATE MATERIALIZED VIEW` is the view name. The
continuous aggregate's data is stored in a materialized hypertable, which is
automatically created and named.
</highlight>

You can then use a regular PostgreSQL statement to create or drop an index on
the hypertable. For example, to create an index on `avg_temp` for a materialized
hypertable named `_timescaledb_internal._materialized_hypertable_2`:

You can use a regular PostgreSQL statement to create or drop an index on a
continuous aggregate.

For example, to create an index on `avg_temp` for a materialized hypertable
named `weather_daily`:

```sql
CREATE INDEX avg_temp_idx ON weather_daily (avg_temp);
```

Indexes are created under the `_timescaledb_internal` schema, where the
continuous aggregate data is stored. To drop the index, specify the schema. For
example, to drop the index `avg_temp_idx`, run:

```sql
CREATE INDEX avg_temp_idx ON _timescaledb_internal._materialized_hypertable_2 (avg_temp);
DROP INDEX _timescaledb_internal.avg_temp_idx
```

### Limitations on created indexes

In TimescaleDB 2.7 and above, you can create an index on any column in the
materialized view. This includes aggregated columns, such as those storing sums
and averages. In earlier versions of TimescaleDB, you can't create an index on
an aggregated column.

[materialized-hypertable-name]: /timescaledb/:currentVersion:/how-to-guides/continuous-aggregates/materialized-hypertables/#discover-the-name-of-a-materialized-hypertable
You can't create unique indexes on a continuous aggregate, in any version
of TimescaleDB.

0 comments on commit b54ecbe

Please sign in to comment.