pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Variable constants ?
12+ messages / 9 participants
[nested] [flat]

* Variable constants ?
@ 2019-08-15 20:56 stan <stanb@panix.com>
  2019-08-15 21:09 ` Re: Variable constants ? Adrian Klaver <adrian.klaver@aklaver.com>
  2019-08-15 21:27 ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-15 22:12 ` Re: Variable constants ? Tom Lane <tgl@sss.pgh.pa.us>
  2019-09-15 21:25 ` Re: Variable constants ? Peter J. Holzer <hjp-pgsql@hjp.at>
  0 siblings, 4 replies; 12+ messages in thread

From: stan @ 2019-08-15 20:56 UTC (permalink / raw)
  To: pgsql-general

I need to put a few bossiness constants, such as a labor rate multiplier in an
application. I am adverse to hard coding these things. The best plan i have
come up with so far is to store them in a table, which would have only 1
row, and a column for each needed constant.

Anyone have a better way to do this?

Failing a better way is there some way I can limit this table to only allow
one row to exist?


-- 
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
						-- Benjamin Franklin





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
@ 2019-08-15 21:09 ` Adrian Klaver <adrian.klaver@aklaver.com>
  3 siblings, 0 replies; 12+ messages in thread

From: Adrian Klaver @ 2019-08-15 21:09 UTC (permalink / raw)
  To: stan <stanb@panix.com>; pgsql-general

On 8/15/19 1:56 PM, stan wrote:
> I need to put a few bossiness constants, such as a labor rate multiplier in an
> application. I am adverse to hard coding these things. The best plan i have
> come up with so far is to store them in a table, which would have only 1
> row, and a column for each needed constant.
> 
> Anyone have a better way to do this?

Another way(better in eye of beholder) a conf file that can be written to.

> 
> Failing a better way is there some way I can limit this table to only allow
> one row to exist?

Yes add the row and REVOKE INSERT afterwards. Then you have a single row 
that can be updated/deleted(though you could revoke that also).

> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
@ 2019-08-15 21:27 ` Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-16 04:41   ` Re: Variable constants ? Charles Clavadetscher <clavadetscher@swisspug.org>
  2019-08-16 13:55   ` Re: Variable constants ? Luca Ferrari <fluca1978@gmail.com>
  2019-08-16 17:13   ` Re: Variable constants ? Gavin Flower <GavinFlower@archidevsys.co.nz>
  3 siblings, 3 replies; 12+ messages in thread

From: Rich Shepard @ 2019-08-15 21:27 UTC (permalink / raw)
  To: pgsql-general

On Thu, 15 Aug 2019, stan wrote:

> I need to put a few bossiness constants, such as a labor rate multiplier
> in an application. I am adverse to hard coding these things. The best plan
> i have come up with so far is to store them in a table, which would have
> only 1 row, and a column for each needed constant.
>
> Anyone have a better way to do this?
>
> Failing a better way is there some way I can limit this table to only
> allow one row to exist?

Stan,

I've resolved similar issues with changing regulatory agency staff. For your
application(s) I suggest a table like this:

create table labor_rate_mult (
   rate 		real primary_key,
   start_date  	date not null,
   end_date	date
)

This provides both a history of labor rate multipliers and the ability to
select either the most current one or a previous one.

If other factors affect the rate, add attribute columns for them.

Regards,

Rich





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
  2019-08-15 21:27 ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-08-16 04:41   ` Charles Clavadetscher <clavadetscher@swisspug.org>
  2019-08-16 12:50     ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
  2 siblings, 1 reply; 12+ messages in thread

From: Charles Clavadetscher @ 2019-08-16 04:41 UTC (permalink / raw)
  To: pgsql-general

On 2019-08-15 23:27, Rich Shepard wrote:
> On Thu, 15 Aug 2019, stan wrote:
> 
>> I need to put a few bossiness constants, such as a labor rate 
>> multiplier
>> in an application. I am adverse to hard coding these things. The best 
>> plan
>> i have come up with so far is to store them in a table, which would 
>> have
>> only 1 row, and a column for each needed constant.
>> 
>> Anyone have a better way to do this?
>> 
>> Failing a better way is there some way I can limit this table to only
>> allow one row to exist?
> 
> Stan,
> 
> I've resolved similar issues with changing regulatory agency staff. For 
> your
> application(s) I suggest a table like this:
> 
> create table labor_rate_mult (
>   rate 		real primary_key,
>   start_date  	date not null,
>   end_date	date
> )
> 
> This provides both a history of labor rate multipliers and the ability 
> to
> select either the most current one or a previous one.
> 
> If other factors affect the rate, add attribute columns for them.
> 
> Regards,
> 
> Rich

Another way to keep a history is using a daterange instead of two 
columns for start and end date. Something like

create table labor_rate_mult (
   rate 		real primary_key,
   validity	daterange not null
)

This makes it easier to manage and avoid e.g. overlappings.

Regards
Charles







^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
  2019-08-15 21:27 ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-16 04:41   ` Re: Variable constants ? Charles Clavadetscher <clavadetscher@swisspug.org>
@ 2019-08-16 12:50     ` Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-16 13:25       ` Re: Variable constants ? Charles Clavadetscher <clavadetscher@swisspug.org>
  0 siblings, 1 reply; 12+ messages in thread

From: Rich Shepard @ 2019-08-16 12:50 UTC (permalink / raw)
  To: pgsql-general

On Fri, 16 Aug 2019, Charles Clavadetscher wrote:

> Another way to keep a history is using a daterange instead of two columns for 
> start and end date. Something like
>
> create table labor_rate_mult (
>  rate 		real primary_key,
>  validity	daterange not null
> )

Charles,

Just out of curiosity, what is the range for a rate that is still current?
Does it change every day?

Regards,

Rich





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
  2019-08-15 21:27 ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-16 04:41   ` Re: Variable constants ? Charles Clavadetscher <clavadetscher@swisspug.org>
  2019-08-16 12:50     ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-08-16 13:25       ` Charles Clavadetscher <clavadetscher@swisspug.org>
  2019-08-16 14:27         ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 1 reply; 12+ messages in thread

From: Charles Clavadetscher @ 2019-08-16 13:25 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general

On 2019-08-16 14:50, Rich Shepard wrote:
> On Fri, 16 Aug 2019, Charles Clavadetscher wrote:
> 
>> Another way to keep a history is using a daterange instead of two 
>> columns for start and end date. Something like
>> 
>> create table labor_rate_mult (
>>  rate 		real primary_key,
>>  validity	daterange not null
>> )
> 
> Charles,
> 
> Just out of curiosity, what is the range for a rate that is still 
> current?
> Does it change every day?
> 
> Regards,
> 
> Rich

Hi Rich

That would be a range with an empty upper bound. Let's say that the rate 
is valid since 2019-08-14 then the range would look like

[2019-08-14,)

A query to find the current rate would look like:

SELECT rate
FROM labor_rate_mult
WHERE validity @> CURRENT_DATE;

Here you can find documentation on the range types (cool stuff I 
believe):

https://www.postgresql.org/docs/11/rangetypes.html

Regards
Charles

-- 
Charles Clavadetscher
Swiss PostgreSQL Users Group
Treasurer
Neugasse 84
CH – 8005 Zürich

http://www.swisspug.org

+---------------------------+
|     ____  ______  ___     |
|    /    )/      \/   \    |
|   (     / __    _\    )   |
|    \    (/ o)  ( o)   )   |
|     \_  (_  )   \ ) _/    |
|       \  /\_/    \)/      |
|        \/ <//|  |\\>      |
|             _|  |         |
|             \|_/          |
|                           |
|     Swiss PostgreSQL      |
|       Users Group         |
|                           |
+---------------------------+





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
  2019-08-15 21:27 ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-16 04:41   ` Re: Variable constants ? Charles Clavadetscher <clavadetscher@swisspug.org>
  2019-08-16 12:50     ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
  2019-08-16 13:25       ` Re: Variable constants ? Charles Clavadetscher <clavadetscher@swisspug.org>
@ 2019-08-16 14:27         ` Rich Shepard <rshepard@appl-ecosys.com>
  0 siblings, 0 replies; 12+ messages in thread

From: Rich Shepard @ 2019-08-16 14:27 UTC (permalink / raw)
  To: pgsql-general

On Fri, 16 Aug 2019, Charles Clavadetscher wrote:

> That would be a range with an empty upper bound. Let's say that the rate
> is valid since 2019-08-14 then the range would look like
> [2019-08-14,)
> A query to find the current rate would look like:
>
> SELECT rate
> FROM labor_rate_mult
> WHERE validity @> CURRENT_DATE;
>
> Here you can find documentation on the range types (cool stuff I believe):
> https://www.postgresql.org/docs/11/rangetypes.html

Charles,

Interesting. Certainly worth considering the next time an application needs
a range of dates.

Regards,

Rich





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
  2019-08-15 21:27 ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-08-16 13:55   ` Luca Ferrari <fluca1978@gmail.com>
  2 siblings, 0 replies; 12+ messages in thread

From: Luca Ferrari @ 2019-08-16 13:55 UTC (permalink / raw)
  To: Rich Shepard <rshepard@appl-ecosys.com>; +Cc: pgsql-general

On Thu, Aug 15, 2019 at 11:27 PM Rich Shepard <rshepard@appl-ecosys.com> wrote:
> create table labor_rate_mult (
>    rate                 real primary_key,
>    start_date   date not null,
>    end_date     date
> )

I think the rate should not be the primary key, since that would
prevent keeping the whole history when the value is resetted to a
previous one. Probably here a surrogate key will make the trick.

The range solution is probably a more elegant one.

I would also propose the over-complicated possibility of making an
extension wrapping functions that return each single constant value.
In this way, changing the value means upgrading the extension and is
another way to keep history of changes, but probably is because I
don't like one-raw tables so much.

Luca





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
  2019-08-15 21:27 ` Re: Variable constants ? Rich Shepard <rshepard@appl-ecosys.com>
@ 2019-08-16 17:13   ` Gavin Flower <GavinFlower@archidevsys.co.nz>
  2 siblings, 0 replies; 12+ messages in thread

From: Gavin Flower @ 2019-08-16 17:13 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 16/08/2019 09:27, Rich Shepard wrote:
> On Thu, 15 Aug 2019, stan wrote:
>
>> I need to put a few bossiness constants, such as a labor rate multiplier
>> in an application. I am adverse to hard coding these things. The best 
>> plan
>> i have come up with so far is to store them in a table, which would have
>> only 1 row, and a column for each needed constant.
>>
>> Anyone have a better way to do this?
>>
>> Failing a better way is there some way I can limit this table to only
>> allow one row to exist?
>
> Stan,
>
> I've resolved similar issues with changing regulatory agency staff. 
> For your
> application(s) I suggest a table like this:
>
> create table labor_rate_mult (
>   rate         real primary_key,
>   start_date      date not null,
>   end_date    date
> )
>
> This provides both a history of labor rate multipliers and the ability to
> select either the most current one or a previous one.
>
> If other factors affect the rate, add attribute columns for them.
>
> Regards,
>
> Rich
>
>
I think a better approach is to:

  * include time
  * store independent of timezone (avoids problems with daylight saving)
  * only have one timestamp

    DROP TABLE IF EXISTS labour_rate_mult;


    CREATE TABLE labour_rate_mult
    (
         rate_name         text,
         effective_start   timestamptz,
         rate_value        real,
         valid             boolean,
         PRIMARY KEY (rate_name, effective_start)
    );


    INSERT INTO labour_rate_mult
    (
         rate_name,
         effective_start,
         rate_value,
         valid
    )
    VALUES  -- test data omits time for clarity
         ('junior', '2001-02-01', 4.2, true),
         ('junior', '2008-11-16', 6, true),
         ('junior', '2012-07-23', 4.5, true),
         ('junior', '2019-09-11', 3.7, true),
         ('junior', '2030-12-31', 0, false),
         ('adult', '2001-01-01', 8.4, true),
         ('adult', '2012-07-23', 9.9, true),
         ('adult', '2030-05-03', 0, false)
    /**/;/**/


    SELECT
         rate_value
    FROM
         labour_rate_mult
    WHERE
             rate_name = 'junior'
         AND effective_start <= '2012-07-23' -- stand in for
    CURRENT_TIMESTAMP
         AND valid
    ORDER BY
         effective_start DESC
    LIMIT 1
    /**/;/**/


Cheers.
Gavin

P.S.
Previously, I accidentally just sent it to Rich!






^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
@ 2019-08-15 22:12 ` Tom Lane <tgl@sss.pgh.pa.us>
  2019-08-15 23:07   ` RE: Variable constants ? Igor Neyman <ineyman@perceptron.com>
  3 siblings, 1 reply; 12+ messages in thread

From: Tom Lane @ 2019-08-15 22:12 UTC (permalink / raw)
  To: stan <stanb@panix.com>; +Cc: pgsql-general

stan <stanb@panix.com> writes:
> Failing a better way is there some way I can limit this table to only allow
> one row to exist?

I was recently reminded of a cute trick for that: make a unique index
on a constant.

regression=# create table consts(f1 int, f2 int);
CREATE TABLE
regression=# create unique index consts_only_one on consts((1));
CREATE INDEX
regression=# insert into consts values(1,2);
INSERT 0 1
regression=# insert into consts values(3,4);
ERROR:  duplicate key value violates unique constraint "consts_only_one"
DETAIL:  Key ((1))=(1) already exists.

Shepard's nearby advice about keeping some history is probably a better
plan though.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* RE: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
  2019-08-15 22:12 ` Re: Variable constants ? Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-08-15 23:07   ` Igor Neyman <ineyman@perceptron.com>
  0 siblings, 0 replies; 12+ messages in thread

From: Igor Neyman @ 2019-08-15 23:07 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; stan <stanb@panix.com>; +Cc: pgsql-general

-----Original Message-----
From: Tom Lane <tgl@sss.pgh.pa.us> 
Sent: Thursday, August 15, 2019 6:13 PM
To: stan <stanb@panix.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Variable constants ?

stan <stanb@panix.com> writes:
> Failing a better way is there some way I can limit this table to only 
> allow one row to exist?

I was recently reminded of a cute trick for that: make a unique index on a constant.

regression=# create table consts(f1 int, f2 int); CREATE TABLE regression=# create unique index consts_only_one on consts((1)); CREATE INDEX regression=# insert into consts values(1,2); INSERT 0 1 regression=# insert into consts values(3,4);
ERROR:  duplicate key value violates unique constraint "consts_only_one"
DETAIL:  Key ((1))=(1) already exists.

Shepard's nearby advice about keeping some history is probably a better plan though.

                        regards, tom lane
____________________________________________________________________________

Here is another trick to keep a table to just one row:

reg#create table test(c1 int check(c1=1) default 1, c2 int);
CREATE TABLE
reg#create unique index one_row on test(c1);
CREATE INDEX
reg#insert into test (c2) values(3);
INSERT 01
reg# insert into test (c2) values(4);
ERROR:  duplicate key value violates unique constraint "one_row"
DETAIL:  Key (c1)=(1) already exists.
SQL state: 23505

Regards,
Igor Neyman






^ permalink  raw  reply  [nested|flat] 12+ messages in thread

* Re: Variable constants ?
  2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
@ 2019-09-15 21:25 ` Peter J. Holzer <hjp-pgsql@hjp.at>
  3 siblings, 0 replies; 12+ messages in thread

From: Peter J. Holzer @ 2019-09-15 21:25 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

On 2019-08-15 16:56:57 -0400, stan wrote:
> bossiness constants

On 2019-09-02 13:31:14 -0400, stan wrote:
> bossiness plan
> bossiness model

On 2019-09-13 05:57:33 -0400, stan wrote:
> bossiness work

I'm starting to think that this is not a typo :-)

    SCNR,
        hp

-- 
   _  | Peter J. Holzer    | we build much bigger, better disasters now
|_|_) |                    | because we have much more sophisticated
| |   | hjp@hjp.at         | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson <https://www.edge.org/;

Attachments:

  signature.asc (application/pgp-signature, 832B) download

^ permalink  raw  reply  [nested|flat] 12+ messages in thread


end of thread, other threads:[~2019-09-15 21:25 UTC | newest]

Thread overview: 12+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-15 20:56 Variable constants ? stan <stanb@panix.com>
2019-08-15 21:09 ` Adrian Klaver <adrian.klaver@aklaver.com>
2019-08-15 21:27 ` Rich Shepard <rshepard@appl-ecosys.com>
2019-08-16 04:41   ` Charles Clavadetscher <clavadetscher@swisspug.org>
2019-08-16 12:50     ` Rich Shepard <rshepard@appl-ecosys.com>
2019-08-16 13:25       ` Charles Clavadetscher <clavadetscher@swisspug.org>
2019-08-16 14:27         ` Rich Shepard <rshepard@appl-ecosys.com>
2019-08-16 13:55   ` Luca Ferrari <fluca1978@gmail.com>
2019-08-16 17:13   ` Gavin Flower <GavinFlower@archidevsys.co.nz>
2019-08-15 22:12 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-15 23:07   ` Igor Neyman <ineyman@perceptron.com>
2019-09-15 21:25 ` Peter J. Holzer <hjp-pgsql@hjp.at>

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