Live data from Hacker News

PostgreSQL views and materialized views and how they influenced TimescaleDB

timescale.com

21–30 of 32 posts

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#21
post #20

This is an excellent article. I like the way the author builds up in steps to eventual consistency between source tables and their materialized views. It was fun to guess the next step. I do have one question: how does the algorithm described in the article work when the source table is spread across multiple servers, say in multiple shards? Can TimescaleDB maintain materialized views on each shard and then run a que…

NB: Post author So we thought about doing something like that with multinode where each of the nodes would maintain their own materialization but abandoned it for that very reason it’s very, very difficult to maintain any sort of consistency guarantees in that case, or even to reason about it. Instead we use the access nodes as coordinators to do the materialization. right now the materialization only exists on the a…

And glad you liked the article!

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#22
post #21
post #20

Earlier quoted context omitted.

NB: Post author So we thought about doing something like that with multinode where each of the nodes would maintain their own materialization but abandoned it for that very reason it’s very, very difficult to maintain any sort of consistency guarantees in that case, or even to reason about it. Instead we use the access nodes as coordinators to do the materialization. right now the materialization only exists on the a…

And glad you liked the article!

It was excellent. I work on ClickHouse and wrote something similar a while back for ClickHouse mat views. [0] It was not nearly as good, hence the appreciation.

Don't know if you do talks but I have a couple opportunities coming up at open source events. We like hearing about what people are doing outside of ClickHouse.

[0] https://altinity.com/blog/clickhouse-materialized-views-illu...

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#23
post #9

Earlier quoted context omitted.

> commercial engines will use the presence of fk constraints What, postgres doesn't do FK join removals? Like I tried it right now and it didn't remove the hash-join http://sqlfiddle.com/#!17/073747/2 Should've been just a count off the PK Employees table. Apache Calcite in Hive had to bake in this specific optimization because there were a ton of deep join views (coming off Teradata) where someone would run somethin…

How would that work? Your schema allows an employee to be assigned to multiple departments, and the query must count them.

It is also called Table Elimination if you want a google-able term.

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#24

Postgres is an amazing database. It’s only significant weakness now is in Materialized views, with their lack of incremental refresh. Was disappointing to see there was no progress towards this in v15.

I wanted incremental refresh in Postgres as well and found that you can manage your own table to get something close.

Basically you create a regular table in place of a materialised one, only aggregate data newer than what's currently in the table then store the new aggregates in table. Repeat this an interval.

https://github.com/cadbox1/prawn-stack/blob/master/src/backe...

I use this to show page view data aggregated by hour without calculating it on each request using Lambda

https://prawn.cadell.dev/

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#25
post #9
post #2

> If I’d written out the query, I might have seen that I didn’t need the JOIN (or never written it in the first place). Whereas the view hides that complexity. So they can make things easier, but that can lead to performance pitfalls if we’re not careful. you can avoid this particular pitfall by using left joins for views like this (that join stuff in for convenience that you might not select) - postgres will elimina…

> commercial engines will use the presence of fk constraints What, postgres doesn't do FK join removals? Like I tried it right now and it didn't remove the hash-join http://sqlfiddle.com/#!17/073747/2 Should've been just a count off the PK Employees table. Apache Calcite in Hive had to bake in this specific optimization because there were a ton of deep join views (coming off Teradata) where someone would run somethin…

I would try running that fiddle on something more recent than Postgres 9.6

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#26
post #9
post #2

> If I’d written out the query, I might have seen that I didn’t need the JOIN (or never written it in the first place). Whereas the view hides that complexity. So they can make things easier, but that can lead to performance pitfalls if we’re not careful. you can avoid this particular pitfall by using left joins for views like this (that join stuff in for convenience that you might not select) - postgres will elimina…

> commercial engines will use the presence of fk constraints What, postgres doesn't do FK join removals? Like I tried it right now and it didn't remove the hash-join http://sqlfiddle.com/#!17/073747/2 Should've been just a count off the PK Employees table. Apache Calcite in Hive had to bake in this specific optimization because there were a ton of deep join views (coming off Teradata) where someone would run somethin…

[deleted]

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#28
post #9

Earlier quoted context omitted.

> commercial engines will use the presence of fk constraints What, postgres doesn't do FK join removals? Like I tried it right now and it didn't remove the hash-join http://sqlfiddle.com/#!17/073747/2 Should've been just a count off the PK Employees table. Apache Calcite in Hive had to bake in this specific optimization because there were a ton of deep join views (coming off Teradata) where someone would run somethin…

I would try running that fiddle on something more recent than Postgres 9.6

I ran into this recently on a newer version. Postgres will not use the presence of a foreign key (referencing a unique index) to skip unnecessary inner joins.

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#29
post #28

Earlier quoted context omitted.

I would try running that fiddle on something more recent than Postgres 9.6

I ran into this recently on a newer version. Postgres will not use the presence of a foreign key (referencing a unique index) to skip unnecessary inner joins.

I have done my own (simple) testing on Postgres 13 and was unable to make Postgres elide the join.

Re: PostgreSQL views and materialized views and how they influenced TimescaleDB

#30

Earlier quoted context omitted.

I also wish this were in core posgres, but if you use a build tool like DBT you can enable this type of thing.

Interestingly, DBT does not support creating materialized views.

What's the effective difference between a matview and just a table?
Post reply on HN