pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedHow to get an inclusive interval when using daterange
6+ messages / 4 participants
[nested] [flat]
* How to get an inclusive interval when using daterange
@ 2018-04-02 18:09 hmidi slim <hmidi.slim2@gmail.com>
0 siblings, 2 replies; 6+ messages in thread
From: hmidi slim @ 2018-04-02 18:09 UTC (permalink / raw)
To: pgsql-general
Hi,
I have a table* availability* which contains 3 columns: * id, product_id
and period_availability(type daterange).*
When I insert a data into this table I use this query:
insert into availability values ('product x', daterange('2018-02-02',
'2018-03-01', '[]')
So I get a period like this: [2018-02-02, 2018-03-02)
In my app I tried to subtract a day from the period when I got it from
database.I'm using daterange and not tsrange because the daterange gives me
less execution time when I make tests with a huge number of data.So is
there any way to get an inclusive interval with daterange or I have to use
tsrange to get inclusive intervals?
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to get an inclusive interval when using daterange
@ 2018-04-02 19:13 Tom Lane <tgl@sss.pgh.pa.us>
parent: hmidi slim <hmidi.slim2@gmail.com>
1 sibling, 0 replies; 6+ messages in thread
From: Tom Lane @ 2018-04-02 19:13 UTC (permalink / raw)
To: hmidi slim <hmidi.slim2@gmail.com>; +Cc: pgsql-general
hmidi slim <hmidi.slim2@gmail.com> writes:
> When I insert a data into this table I use this query:
> insert into availability values ('product x', daterange('2018-02-02',
> '2018-03-01', '[]')
> So I get a period like this: [2018-02-02, 2018-03-02)
Yup.
> In my app I tried to subtract a day from the period when I got it from
> database.I'm using daterange and not tsrange because the daterange gives me
> less execution time when I make tests with a huge number of data.So is
> there any way to get an inclusive interval with daterange or I have to use
> tsrange to get inclusive intervals?
No, daterange will always canonicalize a range into '[)' format.
This is explained (perhaps not with adequate clarity) in
https://www.postgresql.org/docs/10/static/rangetypes.html#RANGETYPES-DISCRETE
The key reason why is to make it clearer which range specifications
are equal. For instance, it's not really clear whether
['2018-02-02','2018-03-01'] and ['2018-02-02','2018-03-02') represent
the same set of values --- they do if it's a daterange, but not if it's
a tsrange. Canonicalizing makes equal ranges look equal.
regards, tom lane
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to get an inclusive interval when using daterange
@ 2018-04-03 14:35 hmidi slim <hmidi.slim2@gmail.com>
parent: hmidi slim <hmidi.slim2@gmail.com>
1 sibling, 1 reply; 6+ messages in thread
From: hmidi slim @ 2018-04-03 14:35 UTC (permalink / raw)
To: Osvaldo Kussama <osvaldo.kussama@gmail.com>; +Cc: pgsql-general
I tried it and I got the same result.
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to get an inclusive interval when using daterange
@ 2018-04-03 14:37 Adrian Klaver <adrian.klaver@aklaver.com>
parent: hmidi slim <hmidi.slim2@gmail.com>
0 siblings, 1 reply; 6+ messages in thread
From: Adrian Klaver @ 2018-04-03 14:37 UTC (permalink / raw)
To: hmidi slim <hmidi.slim2@gmail.com>; Osvaldo Kussama <osvaldo.kussama@gmail.com>; +Cc: pgsql-general
On 04/03/2018 07:35 AM, hmidi slim wrote:
> I tried it and I got the same result.
Tried what?
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to get an inclusive interval when using daterange
@ 2018-04-03 16:40 hmidi slim <hmidi.slim2@gmail.com>
parent: Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 1 reply; 6+ messages in thread
From: hmidi slim @ 2018-04-03 16:40 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-general
HI,
I tried* insert into availability values ('product x',
'[2018-02-02,2018-03-01]'::daterange); *and I got the same result such as*
insert into availability values ('product x', daterange('2018-02-02',
'2018-03-01', '[]').*
^ permalink raw reply [nested|flat] 6+ messages in thread
* Re: How to get an inclusive interval when using daterange
@ 2018-04-03 17:12 Paul Jungwirth <pj@illuminatedcomputing.com>
parent: hmidi slim <hmidi.slim2@gmail.com>
0 siblings, 0 replies; 6+ messages in thread
From: Paul Jungwirth @ 2018-04-03 17:12 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
On 04/03/2018 09:40 AM, hmidi slim wrote:
> I tried insert into availability values ('product x',
> '[2018-02-02,2018-03-01]'::daterange); and I got the same result such
> as insert into availability values ('product x', daterange('2018-02-02',
> '2018-03-01', '[]').
Yes, those are equivalent ways of constructing the same daterange.
If you really want a closed/closed daterange, you'll need to create your
own type. I don't really recommend that, but you can do it. (Using
close/open is the standard because it's so convenient for
combining/comparing ranges.)
It's easy to create a type without a canonical function, e.g.:
CREATE FUNCTION date_minus(date1 date, date2 date)
RETURNS float AS $$
SELECT cast(date1 - date2 as float);
$$ LANGUAGE sql immutable;
CREATE TYPE daterange2 AS range
(subtype = date, subtype_diff = date_minus);
Then you can say:
SELECT daterange2('2018-01-01', '2018-03-01', '[]');
This is not great though, because without a canonical function Postgres
doesn't know that [x,y] is equal to [x,y+1). If you prefer to have a
canonical function, you'll need to write one in C. (It's easy but you
won't be able to install it on a managed service like AWS RDS.) It might
help to read these and the code they link to (The second one is by me.):
https://stackoverflow.com/questions/29895077/how-to-create-a-new-date-range-type-with-included-upper...
https://illuminatedcomputing.com/posts/2016/06/inet-range/
--
Paul ~{:-)
pj@illuminatedcomputing.com
^ permalink raw reply [nested|flat] 6+ messages in thread
end of thread, other threads:[~2018-04-03 17:12 UTC | newest]
Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-04-02 18:09 How to get an inclusive interval when using daterange hmidi slim <hmidi.slim2@gmail.com>
2018-04-02 19:13 ` Tom Lane <tgl@sss.pgh.pa.us>
2018-04-03 14:35 ` hmidi slim <hmidi.slim2@gmail.com>
2018-04-03 14:37 ` Adrian Klaver <adrian.klaver@aklaver.com>
2018-04-03 16:40 ` hmidi slim <hmidi.slim2@gmail.com>
2018-04-03 17:12 ` Paul Jungwirth <pj@illuminatedcomputing.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox