
create temporary table dt_example (dt_field timestamp with time zone);
insert into dt_example
    select * from generate_series( '2010-01-01'::timestamp with time zone
                                 , current_date::timestamp with time zone
                                 , '1 minute'::interval);
create index brin_idx on dt_example(dt_field);

create temporary view date_with_barrier with (security_barrier) as select * from dt_example;
create temporary view date_without_barrier  as select * from dt_example;

explain analyze
select * from date_with_barrier
  where dt_field >= current_date::timestamp with time zone - interval '1 day';

/*
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                          QUERY PLAN                                                          │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Bitmap Heap Scan on dt_example  (cost=61953.84..172078.84 rows=3309600 width=8) (actual time=0.540..1.357 rows=1441 loops=1) │
│   Recheck Cond: (dt_field >= ((CURRENT_DATE)::timestamp with time zone - '1 day'::interval))                                 │
│   Heap Blocks: exact=7                                                                                                       │
│   ->  Bitmap Index Scan on brin_idx  (cost=0.00..61126.44 rows=3309600 width=0) (actual time=0.476..0.476 rows=1441 loops=1) │
│         Index Cond: (dt_field >= ((CURRENT_DATE)::timestamp with time zone - '1 day'::interval))                             │
│ Planning Time: 0.445 ms                                                                                                      │
│ Execution Time: 1.733 ms                                                                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
*/

explain analyze
select * from date_with_barrier
  where dt_field >= current_date - interval '1 day';

/*
┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                             QUERY PLAN                                                             │
├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Subquery Scan on date_with_barrier  (cost=0.00..316975.03 rows=3309600 width=8) (actual time=4728.391..4729.531 rows=1441 loops=1) │
│   Filter: (date_with_barrier.dt_field >= (CURRENT_DATE - '1 day'::interval))                                                       │
│   Rows Removed by Filter: 9927360                                                                                                  │
│   ->  Seq Scan on dt_example  (cost=0.00..143221.01 rows=9928801 width=8) (actual time=0.045..972.893 rows=9928801 loops=1)        │
│ Planning Time: 0.197 ms                                                                                                            │
│ Execution Time: 4729.658 ms                                                                                                        │
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
*/

explain analyze
select * from date_without_barrier
  where dt_field >= current_date - interval '1 day';

/*
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│                                                          QUERY PLAN                                                          │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ Bitmap Heap Scan on dt_example  (cost=61953.84..163804.84 rows=3309600 width=8) (actual time=1.505..2.783 rows=1441 loops=1) │
│   Recheck Cond: (dt_field >= (CURRENT_DATE - '1 day'::interval))                                                             │
│   Heap Blocks: exact=7                                                                                                       │
│   ->  Bitmap Index Scan on brin_idx  (cost=0.00..61126.44 rows=3309600 width=0) (actual time=1.434..1.435 rows=1441 loops=1) │
│         Index Cond: (dt_field >= (CURRENT_DATE - '1 day'::interval))                                                         │
│ Planning Time: 2.348 ms                                                                                                      │
│ Execution Time: 3.350 ms                                                                                                     │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
*/
