range-types

PostgreSQL tsrange: is it correct for lower_inf('(-infinity,today)'::tsrange) to be false?

∥☆過路亽.° 提交于 2020-01-10 02:10:49
问题 In the course of writing a program that accepts tsrange literals from the user, which are then plugged into various SQL queries, today I was testing some tsranges to see how they are interpreted by PostgreSQL 9.3.5. This one, in particular, behaves strangely: '(-infinity,today)'::tsrange The lower_inf function says the lower bound is not infinite (!) test=> SELECT lower_inf('(-infinity,today)'::tsrange); lower_inf ----------- f (1 row) Yet PostgreSQL reports that this tsrange contains a

How to use the NOW() function as upper bound of a range?

无人久伴 提交于 2019-12-11 12:39:39
问题 I have a table with a column of type tstzrange in a Postgres 10.6 database. I need to insert / update rows with a defined lower bound but the value of the current time for the upper bound of the range, so NOW() as upper value. Have tried things like: UPDATE table_name SET date_range = ['2018-03-23 00:00:00-05', now()) WHERE id = 3; Is there a way to use a built-in function or a subquery maybe? 回答1: Use a range constructor . The manual: Each range type has a constructor function with the same

Best way to join on a range?

孤街醉人 提交于 2019-12-11 02:26:45
问题 I think this may be a common problem that may not have an answer for every tool. Right now we are trying to use amazons Redshift. The only problem we have now is we are trying to do a look up of zip code for an IP address. The table we have that connects IP to city is a range by IP converted to an integer. Example: Start IP | End IP | City | 123123 | 123129 | Rancho Cucamonga| I have tried the obvious inner join on intip >= startip and intip < endip. Does anyone know a good way to do this?

Fast table indexing for range lookup

你。 提交于 2019-12-10 19:13:58
问题 I have a Postgres table with about 4.5 million rows. The columns are basically just low BIGINT, high BIGINT, data1, data2, ... When you query this table, you have a long integer, and want to find the data corresponding to the range between low and high that includes that value. What would be the best way to index this table for fast lookups? 回答1: A multi-column index with reversed sort order: CREATE INDEX tbl_low_high_idx on tbl(low, high DESC); This way, the index can be scanned forward to

Non-overlap, continuous timestamp ranges (tstzrange) for opening hours

不问归期 提交于 2019-12-06 09:27:34
问题 CREATE TABLE operating_period ( id SERIAL NOT NULL PRIMARY KEY, during TSTZRANGE NOT NULL, -- other meta fields ); Requirements: 1. No operating period can overlap with each other Question: How do I add a constraint to make sure that there is no overlap in the operating hours? In terms of query speed, am I better off with two columns (start_at, end_at) or is GIST index fast for tstzrange ? In schema design, is tstzrange commonly used? Or am I better of with two columns? 回答1: The answer to 1.

Non-overlap, continuous timestamp ranges (tstzrange) for opening hours

一笑奈何 提交于 2019-12-04 16:45:24
CREATE TABLE operating_period ( id SERIAL NOT NULL PRIMARY KEY, during TSTZRANGE NOT NULL, -- other meta fields ); Requirements: 1. No operating period can overlap with each other Question: How do I add a constraint to make sure that there is no overlap in the operating hours? In terms of query speed, am I better off with two columns (start_at, end_at) or is GIST index fast for tstzrange ? In schema design, is tstzrange commonly used? Or am I better of with two columns? Erwin Brandstetter The answer to 1. is clear. To make sure there is no overlap use an exclusion constraint : CREATE TABLE

Perform this hours of operation query in PostgreSQL

喜欢而已 提交于 2019-11-27 01:04:57
I'm in the RoR stack and I had to write some actual SQL to complete this query for all records that are "open", meaning that the current time is within the specified hours of operation. In the hours_of_operations table two integer columns opens_on and closes_on store a weekday, and two time fields opens_at and closes_at store the respective time of the day. I made a query that compares the current date and time to the stored values but I'm wondering if there is a way to cast to some sort of date type and have PostgreSQL do the rest? The meat of the query is: WHERE ( ( /* Opens in Future */

Preventing adjacent/overlapping entries with EXCLUDE in PostgreSQL

前提是你 提交于 2019-11-26 18:59:48
I am creating a database which stores arbitrary date/time ranges in PostgreSQL 9.2.4. I want to place a constraint on this database which forces the date/time ranges to be non-overlapping, and non-adjacent (since two adjacent ranges can be expressed as a single continuous range). To do this, I am using an EXCLUDE constraint with a GiST index. Here is the constraint I have currently: ADD CONSTRAINT overlap_exclude EXCLUDE USING GIST ( box( point ( extract(EPOCH FROM "from") - 1, extract(EPOCH FROM "from") - 1 ), point ( extract(EPOCH FROM "to"), extract(EPOCH FROM "to") ) ) WITH && ); The

Calculate working hours between 2 dates in PostgreSQL

瘦欲@ 提交于 2019-11-26 16:33:21
I am developing an algorithm with Postgres (PL/pgSQL) and I need to calculate the number of working hours between 2 timestamps, taking into account that weekends are not working and the rest of the days are counted only from 8am to 15pm. Examples: From Dec 3rd at 14pm to Dec 4th at 9am should count 2 hours: 3rd = 1, 4th = 1 From Dec 3rd at 15pm to Dec 7th at 8am should count 8 hours: 3rd = 0, 4th = 8, 5th = 0, 6th = 0, 7th = 0 It would be great to consider hour fractions as well. Erwin Brandstetter According to your question working hours are: Mo–Fr, 08:00–15:00 . Rounded results For just two

Perform this hours of operation query in PostgreSQL

守給你的承諾、 提交于 2019-11-26 09:34:16
问题 I\'m in the RoR stack and I had to write some actual SQL to complete this query for all records that are \"open\", meaning that the current time is within the specified hours of operation. In the hours_of_operations table two integer columns opens_on and closes_on store a weekday, and two time fields opens_at and closes_at store the respective time of the day. I made a query that compares the current date and time to the stored values but I\'m wondering if there is a way to cast to some sort