Creating views

Last updated:

|Edit this page

In the PostHog data warehouse, you can save your most used queries as views and reference them in subsequent queries.

Creating a view

Query views are created directly inside SQL insights. If the query has valid view characteristics, the "Save as view" button is enabled. When clicked, you are prompted to give the view a name which can then be referenced in other queries.

For a query to be a valid view, all fields being accessed must be aliased (with the SQL as keyword). The alias names are how you access the fields on the view. This also means that we cannot use SELECT *... type SQL syntax for a query we would like to be a view since none of the columns are aliased.

Note: Nested views are supported. You can create a view based on other views.

valid view

Materializing and scheduling a view

Views can be materialized and stored in the PostHog data warehouse. This means that the view is precomputed, which can significantly improve query performance.

After you create a view, you can also schedule it to be updated at a specific interval. This is useful when you have a view that is used frequently, and you want to ensure that the data synced at a specified cadence. For example, a materialized view can be scheduled for an expensive query that is used in multiple dashboards.

To materialize a view, go to the SQL editor, select the Materialize option or the lighting bolt tab, and click Save and materialize. Materialized views are reversible, so you can revert back to a normal view at any time.

materialized view

Tips for Materialization

  • The purpose of materialization is to speed up queries, so you don't need to materialize views that are already fast.

  • You can materialize only the slow part of a larger query, like a with expression or a subquery. Often times, materializing a subset of a larger query is a good way to create a resource that's usable for various different places within PostHog, including insights and other data warehouse views.

  • Datasets generated from materialized views are only going to update at the specified intervals and not in real-time. This means that if you have a view that is used in a dashboard or relied upon for another query, the dashboard will not update until the materialized view is updated. We offer a 5-minute refresh interval, but if the query takes longer than that to execute, it will only be updated once the query is finished and rerun at the next 5-minute interval.

  • Materialization runs have more compute and memory resources allocated to them than standard queries, but they still can timeout for inefficient queries. We time out after 1 hour of processing time.

Extending PostHog models with views

Views are a powerful tool for extending existing PostHog models for easier data access. For example, if you wanted to associate your Stripe customer data with product usage data of your users, you would normally need to manually set up a join. With views, you can attach views to PostHog models so that you can directly access those fields on the PostHog table.

To link a view to a PostHog table, go to the data warehouse section, select the PostHog tab, and click "Link table to view." Select your tables, keys to join, and press save. Once done, when you query that PostHog table, you can access the data from your view.

view link

Questions? Ask Max AI.

It's easier than reading through 642 pages of documentation

Community questions

Was this page useful?

Next article

Under the hood

There are two types of data sources we are able to connect data from—integrations we've built and custom sources you can link. Connectors Connectors use a combination of Temporal and the open source data loading tool, dlt to sync data from your source to PostHog's storage in S3. You can think of this as a complete extract and load tool where you simply provide the credentials and we will take care of the rest of the data flow. Custom sources The second option are custom sources. With custom…

Read next article