Prerequisites
To follow the steps on this page:- Create a target with Real-time analytics enabled. You need your connection details. This procedure also works for .
Hypertable recipes
This section contains recipes about hypertables.Remove duplicates from an existing hypertable
Looking to remove duplicates from an existing hypertable? One method is to run aPARTITION BY query to get
ROW_NUMBER() and then the ctid of rows where row_number>1. You then delete these rows. However,
you need to check tableoid and ctid. This is because ctid is not unique and might be duplicated in
different chunks. The following code example took 17 hours to process a table with 40 million rows:
Get faster JOIN queries with Common Table Expressions
Imagine there is a query that joins a hypertable to another table on a shared key:EXPLAIN on this query, you see that the query planner performs a NestedJoin between these two tables, which means querying the hypertable multiple times. Even if the hypertable is well indexed, if it is also large, the query will be slow. How do you force a once-only lookup? Use materialized Common Table Expressions (CTEs).
If you split the query into two parts using CTEs, you can materialize the hypertable lookup and force to perform it only once.
EXPLAIN once again, you see that this query performs only one lookup. Depending on the size of your hypertable, this could result in a multi-hour query taking mere seconds.
Shoutout to Rowan Molony for this recipe.
IoT recipes
This section contains recipes for IoT issues:Work with columnar IoT data
Narrow and medium width tables are a great way to store IoT data. A lot of reasons are outlined in Designing Your Database Schema: Wide vs. Narrow Postgres Tables. One of the key advantages of narrow tables is that the schema does not have to change when you add new sensors. Another big advantage is that each sensor can sample at different rates and times. This helps support things like hysteresis, where new values are written infrequently unless the value changes by a certain amount.Narrow table format example
Working with narrow table data structures presents a few challenges. In the IoT world one concern is that many data analysis approaches - including machine learning as well as more traditional data analysis - require that your data is resampled and synchronized to a common time basis. Fortunately, provides you with hyperfunctions and other tools to help you work with this data. An example of a narrow table format is:
Typically you would couple this with a sensor table:
A medium table retains the generic structure but adds columns of various types so that you can
use the same table to store float, int, bool, or even JSON (jsonb) data:
To remove all-null entries, use an optional constraint such as:
Get the last value of every sensor
There are several ways to get the latest value of every sensor. The following examples use the structure defined in Narrow table format example as a referenceSELECT DISTINCT ON
If you have a list of sensors, the easy way to get the latest value of every sensor is to useSELECT DISTINCT ON:
JOIN LATERAL
An alternative to SELECT DISTINCT ON is to use aJOIN LATERAL. By selecting your entire
sensor list from the sensors table rather than pulling the IDs out using SELECT DISTINCT, JOIN LATERAL can offer
some improvements in performance: