pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedStoring a time interval
7+ messages / 5 participants
[nested] [flat]
* Storing a time interval
@ 2019-11-08 19:43 stan <stanb@panix.com>
2019-11-08 19:57 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
0 siblings, 1 reply; 7+ messages in thread
From: stan @ 2019-11-08 19:43 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
I need to create a table to store terms and conditions for purchase
orders.
Some of the attributes of a PO include payment terms. Quite often these will
be 2 periods associated with these, the first is a period on which if you
pay, you receive a discount, and the 2nd is when payment is due with no
discount. I am thinking of storing these two attributes as time internals.
What is the wisdom as to how to declare the type of these columns?
--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Storing a time interval
2019-11-08 19:43 Storing a time interval stan <stanb@panix.com>
@ 2019-11-08 19:57 ` Michael Lewis <mlewis@entrata.com>
2019-11-08 20:12 ` Re: Storing a time interval Adrian Klaver <adrian.klaver@aklaver.com>
0 siblings, 1 reply; 7+ messages in thread
From: Michael Lewis @ 2019-11-08 19:57 UTC (permalink / raw)
To: stan <stanb@panix.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
You certainly could choose to store as tstzrange, but why not use two
fields?
https://www.postgresql.org/docs/current/rangetypes.html
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Storing a time interval
2019-11-08 19:43 Storing a time interval stan <stanb@panix.com>
2019-11-08 19:57 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
@ 2019-11-08 20:12 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-11-08 20:38 ` Re: Storing a time interval stan <stanb@panix.com>
0 siblings, 1 reply; 7+ messages in thread
From: Adrian Klaver @ 2019-11-08 20:12 UTC (permalink / raw)
To: Michael Lewis <mlewis@entrata.com>; stan <stanb@panix.com>; +Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
On 11/8/19 11:57 AM, Michael Lewis wrote:
> You certainly could choose to store as tstzrange, but why not use two
> fields?
>
> https://www.postgresql.org/docs/current/rangetypes.html
I would lean more to a composite type:
https://www.postgresql.org/docs/11/rowtypes.html
Range types are built around looking for values within the range,
whereas the OP is looking for two discrete values. The two field method
you suggest above also encapsulates that.
--
Adrian Klaver
adrian.klaver@aklaver.com
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Storing a time interval
2019-11-08 19:43 Storing a time interval stan <stanb@panix.com>
2019-11-08 19:57 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
2019-11-08 20:12 ` Re: Storing a time interval Adrian Klaver <adrian.klaver@aklaver.com>
@ 2019-11-08 20:38 ` stan <stanb@panix.com>
2019-11-08 21:10 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
0 siblings, 1 reply; 7+ messages in thread
From: stan @ 2019-11-08 20:38 UTC (permalink / raw)
To: Adrian Klaver <adrian.klaver@aklaver.com>; +Cc: pgsql-general
On Fri, Nov 08, 2019 at 12:12:59PM -0800, Adrian Klaver wrote:
> On 11/8/19 11:57 AM, Michael Lewis wrote:
> > You certainly could choose to store as??tstzrange, but why not use two
> > fields?
> >
> > https://www.postgresql.org/docs/current/rangetypes.html
>
> I would lean more to a composite type:
>
> https://www.postgresql.org/docs/11/rowtypes.html
>
> Range types are built around looking for values within the range, whereas
> the OP is looking for two discrete values. The two field method you suggest
> above also encapsulates that.
>
Thanks folks.
After thinking over both your sugestions, I beleive teh optimum way to do this
may look like:
CREATE TYPE po_dates AS (
po_isssued_date timestamptz,
discount_last_date timestamptz,
net_date timestamptz
);
--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Storing a time interval
2019-11-08 19:43 Storing a time interval stan <stanb@panix.com>
2019-11-08 19:57 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
2019-11-08 20:12 ` Re: Storing a time interval Adrian Klaver <adrian.klaver@aklaver.com>
2019-11-08 20:38 ` Re: Storing a time interval stan <stanb@panix.com>
@ 2019-11-08 21:10 ` Michael Lewis <mlewis@entrata.com>
2019-11-08 21:20 ` Re: Storing a time interval Steve Baldwin <steve.baldwin@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Michael Lewis @ 2019-11-08 21:10 UTC (permalink / raw)
To: stan <stanb@panix.com>; +Cc: Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general
> CREATE TYPE po_dates AS (
> po_isssued_date timestamptz,
> discount_last_date timestamptz,
> net_date timestamptz
> );
>
What advantage does combining these three values into a custom composite
type give you rather than just storing directly? Are you going to reuse
this po_dates type on many tables?
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Storing a time interval
2019-11-08 19:43 Storing a time interval stan <stanb@panix.com>
2019-11-08 19:57 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
2019-11-08 20:12 ` Re: Storing a time interval Adrian Klaver <adrian.klaver@aklaver.com>
2019-11-08 20:38 ` Re: Storing a time interval stan <stanb@panix.com>
2019-11-08 21:10 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
@ 2019-11-08 21:20 ` Steve Baldwin <steve.baldwin@gmail.com>
2019-11-09 07:58 ` Re: Storing a time interval Pankaj Jangid <pankaj.jangid@gmail.com>
0 siblings, 1 reply; 7+ messages in thread
From: Steve Baldwin @ 2019-11-08 21:20 UTC (permalink / raw)
To: Michael Lewis <mlewis@entrata.com>; +Cc: stan <stanb@panix.com>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general
I agree with Michael. Another consideration is how the composite type is
going to be handled in the DB layer of your processing code (e.g. node,
python, ...). In the scenario you described it seems unlikely you will be
either having multiple columns of that type on your PO table, or using that
composite type on a different table, so apart from the 'interest' factor,
I'm not seeing any practical benefit. Composite types are also slightly
painful in the change they bring to the way you reference them. For example
typically you need to surround the outer column in brackets - e.g.
(dates).discount_last_date. If you are using an ORM library, does it know
how to deal with that?
Steve
On Sat, Nov 9, 2019 at 8:11 AM Michael Lewis <mlewis@entrata.com> wrote:
> CREATE TYPE po_dates AS (
>> po_isssued_date timestamptz,
>> discount_last_date timestamptz,
>> net_date timestamptz
>> );
>>
>
> What advantage does combining these three values into a custom composite
> type give you rather than just storing directly? Are you going to reuse
> this po_dates type on many tables?
>
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Storing a time interval
2019-11-08 19:43 Storing a time interval stan <stanb@panix.com>
2019-11-08 19:57 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
2019-11-08 20:12 ` Re: Storing a time interval Adrian Klaver <adrian.klaver@aklaver.com>
2019-11-08 20:38 ` Re: Storing a time interval stan <stanb@panix.com>
2019-11-08 21:10 ` Re: Storing a time interval Michael Lewis <mlewis@entrata.com>
2019-11-08 21:20 ` Re: Storing a time interval Steve Baldwin <steve.baldwin@gmail.com>
@ 2019-11-09 07:58 ` Pankaj Jangid <pankaj.jangid@gmail.com>
0 siblings, 0 replies; 7+ messages in thread
From: Pankaj Jangid @ 2019-11-09 07:58 UTC (permalink / raw)
To: Steve Baldwin <steve.baldwin@gmail.com>; +Cc: Michael Lewis <mlewis@entrata.com>; stan <stanb@panix.com>; Adrian Klaver <adrian.klaver@aklaver.com>; pgsql-general
Steve Baldwin <steve.baldwin@gmail.com> writes:
> I agree with Michael. Another consideration is how the composite type is
> going to be handled in the DB layer of your processing code (e.g. node,
> python, ...). In the scenario you described it seems unlikely you will be
> either having multiple columns of that type on your PO table, or using that
> composite type on a different table, so apart from the 'interest' factor,
> I'm not seeing any practical benefit. Composite types are also slightly
> painful in the change they bring to the way you reference them. For example
> typically you need to surround the outer column in brackets - e.g.
> (dates).discount_last_date. If you are using an ORM library, does it know
> how to deal with that?
>
I faced a similar issue when using Enums with with rust lang. The Diesel
ORM didn't support it directly. Had to struggle with custom code in the
Model layer.
--
Pankaj Jangid
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2019-11-09 07:58 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-11-08 19:43 Storing a time interval stan <stanb@panix.com>
2019-11-08 19:57 ` Michael Lewis <mlewis@entrata.com>
2019-11-08 20:12 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-11-08 20:38 ` stan <stanb@panix.com>
2019-11-08 21:10 ` Michael Lewis <mlewis@entrata.com>
2019-11-08 21:20 ` Steve Baldwin <steve.baldwin@gmail.com>
2019-11-09 07:58 ` Pankaj Jangid <pankaj.jangid@gmail.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