Containment check tstzrange @> timestamptz not using btree or gist index
The question is what is the reason the index is not used with the tstzrange containment operator and if there is a way to make it work.


The reason is quite trivial. B-tree indexes do no support the containment operator @&gt;. Neither for range types like tstzrange nor for any other type (including array types).

The manual:


  ... a btree operator class must provide five comparison operators, &lt;, &lt;=,
  =, &gt;= and &gt;.


And GiST indexes do not support &lt;, &lt;=, > =, &gt;= and &gt;. See these chapters of the manual:


Built-in Operator Classes for GiST indexes
Behavior of B-Tree Operator Classes


In Postgres indexes are bound to operators (which are implemented for certain types), not data types alone or functions or anything else. Related:


Optimizing queries on a range of timestamps (two columns)
Can PostgreSQL index array columns?


The GiST index event_seating_lookup_created_at_idx2 you have is pointless. It is created on the timestamptz column created_at. Such a GiST index would be useful for a range type (the opposite direction of logic).

Creating a GiST index on a timestamptz column is only possible since you installed the additional btree_gist extension to allow such useless indexes. (There are useful applications for multicolumn indexes or exclusion constraints ...) In stock Postgres you'd get an error:


  ERROR: data type timestamp with time zone has no default operator class for access method "gist"


So while it's logically valid and technically possible to use a btree index (or a GiST index) for the query, the case is not implemented: no index support for timestamptz &lt;@ tstzrange (where timestamptz would be the indexed expression!). It can be solved with &lt;, &lt;=, &gt;, &gt;= more efficiently. So I guess no developer felt (or will feel) the need to implement it.

Function to rewrite expression with favorable operators

Your implementation makes sense to me. It does what you want and due to function inlining makes use of a plain btree index on the timestamp column - event_seating_lookup_created_at_idx in your example. For calls with a constant range (like in a single function call) I suggest this modified version:

CREATE OR REPLACE FUNCTION in_range(timestamptz, tstzrange)
  RETURNS boolean AS
$func$
SELECT CASE WHEN lower_inc($2) THEN $1 &gt;= lower($2) ELSE $1 &gt; lower($2) END
   AND CASE WHEN upper_inc($2) THEN $1 &lt;= upper($2) ELSE $1 &lt; upper($2) END
$func$  LANGUAGE sql IMMUTABLE;


Declare it IMMUTABLE, because it actually is. Not to help function inlining (can even prevent it if the declaration is false) but for other gains. Related:


Can declaring function volatility IMMUTABLE harm performance?
PostgreSQL Inline Function Behavior


It can be inlined and uses the index just like your version. The difference: this one suppresses a redundant index condition for exclusive bounds. Fortunately, your consideration in this regard is slightly off target:


  The reason for first matching on the $1 &gt;= lower($2) AND $1 &lt;= upper($2)
  condition and then checking the upper_inc and lower_inc
  constraints is to benefit from range scan first and then filter the result.


Postgres 11 (at least) is even smarter than that. I see no Filter step for your version. For default [) bounds (like in your example), I get this query plan (line breaks in conditions added by me):

  -&gt;  Index Only Scan using foo_idx on foo (actual rows=5206 loops=1)
        Index Cond: ((datetime &gt;= '2018-09-05 22:00:00+00'::timestamp with time zone)
                 AND (datetime &lt;= '2018-09-05 22:30:00+00'::timestamp with time zone)
                 AND (datetime &lt; '2018-09-05 22:30:00+00'::timestamp with time zone))


An actual Filter step could add a more substantial cost for corner cases with many hits on an excluded bound. Those would be fetched from the index and then discarded. Quite relevant for time ranges where values often end up on bounds - like timestamps on the full hour.

The actual difference is see is minor, but why not take it?

  -&gt;  Index Only Scan using foo_idx on foo (actual rows=5206 loops=1)
        Index Cond: ((datetime &gt;= '2018-09-05 22:00:00+00'::timestamp with time zone)
                 AND (datetime &lt; '2018-09-05 22:30:00+00'::timestamp with time zone))