@@ -122,7 +122,7 @@ create table my_table (
122
122
);
123
123
```
124
124
125
- The default value` 00000000-...00 ` for"id" is "fake" – it will always be replaced in trigger, based on the timestamp:
125
+ The default value` 00000000-...00 ` for` id ` is "fake" – it will always be replaced in trigger, based on the timestamp:
126
126
127
127
``` sql
128
128
create or replace function t_update_uuid () returns trigger
@@ -191,29 +191,33 @@ Child tables: _timescaledb_internal._hyper_2_3_chunk,
191
191
192
192
##Test queries – partition pruning
193
193
194
- Now we just need to remember that` created_at ` should always participate in queries, to let planner deal with as few
195
- partitions as possible – but knowing the` id ` values, we can always reconstruct the` created_at ` values, using
196
- ` uuid_v7_to_ts() ` :
194
+ Now we just need to remember that` uuid_ts ` should always participate in queries, to let planner deal with as few
195
+ partitions as possible – but knowing the` id ` values, we can always reconstruct the` uuid_ts ` values, using
196
+ ` uuid_v7_to_ts() ` . Note that I first disabled` seqscan ` as the table` my_table ` has too few rows, otherwise PostgreSQL
197
+ may decide on preferring` seqscan ` over index scan:
197
198
198
199
``` sql
199
- test= # explain select * from my_table where created_at = uuid_v7_to_ts('00dc6ad0-9660-7b92-a95e-1d7afdaae659');
200
+ test# set enable_seqscan = off;
201
+ SET
202
+
203
+ test= # explain select * from my_table where uuid_ts = uuid_v7_to_ts('00dc6ad0-9660-7b92-a95e-1d7afdaae659');
200
204
QUERY PLAN
201
205
-- ------------------------------------------------------------------------------------------------------------------------
202
206
Append (cost= 0 .14 ..8 .16 rows= 1 width= 41 )
203
- - > Index Scan using_hyper_5_11_chunk_my_table_created_at_idx on _hyper_5_11_chunk (cost= 0 .14 ..8 .15 rows= 1 width= 41 )
204
- Index Cond: (created_at = ' 2000-01-01 00:01:00+00' ::timestamp with time zone )
207
+ - > Index Scan using_hyper_5_11_chunk_my_table_uuid_ts_idx on _hyper_5_11_chunk (cost= 0 .14 ..8 .15 rows= 1 width= 41 )
208
+ Index Cond: (uuid_ts = ' 2000-01-01 00:01:00+00' ::timestamp with time zone )
205
209
(3 rows)
206
210
207
211
test= # explain select * from my_table
208
- where created_at >= uuid_v7_to_ts(' 018c1ecb-d3b7-75b1-add9-62878b5152c7' )
209
- order by created_at desc limit 10 ;
212
+ where uuid_ts >= uuid_v7_to_ts(' 018c1ecb-d3b7-75b1-add9-62878b5152c7' )
213
+ order by uuid_ts desc limit 10 ;
210
214
QUERY PLAN
211
215
-- ---------------------------------------------------------------------------------------------------------------------------------
212
216
Limit (cost= 0 .29 ..1 .17 rows= 10 width= 41 )
213
217
- > Custom Scan (ChunkAppend)on my_table (cost= 0 .29 ..11 .49 rows= 126 width= 41 )
214
- Order:my_table .created_at DESC
215
- - > Index Scan using_hyper_5_16_chunk_my_table_created_at_idx on _hyper_5_16_chunk (cost= 0 .29 ..11 .49 rows= 126 width= 41 )
216
- Index Cond: (created_at >= ' 2023-11-30 05:55:23.703+00' ::timestamp with time zone )
218
+ Order:my_table .uuid_ts DESC
219
+ - > Index Scan using_hyper_5_16_chunk_my_table_uuid_ts_idx on _hyper_5_16_chunk (cost= 0 .29 ..11 .49 rows= 126 width= 41 )
220
+ Index Cond: (uuid_ts >= ' 2023-11-30 05:55:23.703+00' ::timestamp with time zone )
217
221
(5 rows)
218
222
```
219
223