Possible to use a field within a query in PostgreSQL? I want to query all events that are going on right now. Each events record has at and minutes fields for the time range of the event. I might query the events table like this:

SELECT * FROM events WHERE at &gt;= NOW() - INTERVAL '2 hours' AND at &lt;= NOW()


This assumes the event is 2 hours long. But I don't know that. The actual length is stored in the minutes field. How can I use that field in my query? Or is there a better mechanism for me? I've unsuccessfully tried things like this:

SELECT * FROM events WHERE at &gt;= NOW() - INTERVAL minutes ' minutes' AND at &lt;= NOW()


One little caveat is that performance does matter, somewhat. There is a large number of events in our database and that is growing fast.
You can use:

WHERE at &gt;= NOW() - minute * INTERVAL '1 minute' AND at &lt;= NOW()


The Postgres documentation page has similar examples: Date/Time Functions and Operators

The above is not going to be very efficient but you could add an index on at + minute * INTERVAL '1 minute' and then modify the query condition:

WHERE at &lt;= NOW() AND (at + minute * INTERVAL '1 minute') &gt;= NOW()


But it would probably be better if you used a gist index on the range (from when the event starts until it finishes), with something like this:

CREATE INDEX event_start_end_idx ON events 
  USING gist (tsrange(at, at + minute * INTERVAL '1 minute'));


and then use the "contains" (@&gt;) range operator:

WHERE tsrange(at, at + minute * INTERVAL '1 minute'))  @&gt;  NOW()