pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
OT: using column in an interval
3+ messages / 3 participants
[nested] [flat]

* OT: using column in an interval
@ 2006-03-23 13:23  Bradley Kieser <brad@sms-speedway.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Bradley Kieser @ 2006-03-23 13:23 UTC (permalink / raw)
  To: pgsql-admin

All,

I know that this isn't strictly an admin question but please forgive me 
for asking.
I am writing a (admin) job which does a select off a PG database based 
on three columns:

last_backup: timestamp
backup_unit: integer - Represents day, week, quarter, annual, etc. The 
text is stored in backup_code (e.g. 'days', 'months')
backup_period: integer - Represents the skip factor.

i.e. if backup_unit is 1 (days) and period is 3, then together they 
represent "every three days".

Obviously I want to do something like:

select id, client
from backupSchedule
where last_backup + interval backup_period backup_code <= CURRENT_DATE


However, interval seems to only take text such as
inverval '3 days'

and I get an error even with this:

select id, client
from backupSchedule
where last_backup + interval backup_period::text || backup_code <= 
CURRENT_DATE

Can someone please point me to the right statement to use for 
column-based interval arithmetic?
The docs all give hardcoded text strings in examples. Not found one yet 
with a proper column-based query!




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

* Re: OT: using column in an interval
@ 2006-03-24 22:09  Stephan Szabo <sszabo@megazone.bigpanda.com>
  parent: Bradley Kieser <brad@sms-speedway.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Stephan Szabo @ 2006-03-24 22:09 UTC (permalink / raw)
  To: Bradley Kieser <brad@sms-speedway.com>; +Cc: pgsql-admin


On Thu, 23 Mar 2006, Bradley Kieser wrote:

> All,
>
> I know that this isn't strictly an admin question but please forgive me
> for asking.
> I am writing a (admin) job which does a select off a PG database based
> on three columns:
>
> last_backup: timestamp
> backup_unit: integer - Represents day, week, quarter, annual, etc. The
> text is stored in backup_code (e.g. 'days', 'months')
> backup_period: integer - Represents the skip factor.
>
> i.e. if backup_unit is 1 (days) and period is 3, then together they
> represent "every three days".
>
> Obviously I want to do something like:
>
> select id, client
> from backupSchedule
> where last_backup + interval backup_period backup_code <= CURRENT_DATE
>
>
> However, interval seems to only take text such as
> inverval '3 days'
>
> and I get an error even with this:
>
> select id, client
> from backupSchedule
> where last_backup + interval backup_period::text || backup_code <=
> CURRENT_DATE
>
> Can someone please point me to the right statement to use for
> column-based interval arithmetic?
> The docs all give hardcoded text strings in examples. Not found one yet
> with a proper column-based query!

Well, that's because the interval <blah> syntax is for interval literals.

CAST( backup_period || ' ' || backup_code AS interval) should give you an
interval. If the units were constant, I'd say that using integer *
interval is a better idea, but I think you'd need a function that say took
backup_unit and gave back an interval of 1 <unit> to make that work, but
that would possibly be cleaner overall.




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

* Re: OT: using column in an interval
@ 2006-03-24 23:41  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Stephan Szabo <sszabo@megazone.bigpanda.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2006-03-24 23:41 UTC (permalink / raw)
  To: Stephan Szabo <sszabo@megazone.bigpanda.com>; +Cc: Bradley Kieser <brad@sms-speedway.com>; pgsql-admin

Stephan Szabo <sszabo@megazone.bigpanda.com> writes:
> On Thu, 23 Mar 2006, Bradley Kieser wrote:
>> last_backup: timestamp
>> backup_unit: integer - Represents day, week, quarter, annual, etc. The
>> text is stored in backup_code (e.g. 'days', 'months')
>> backup_period: integer - Represents the skip factor.

> CAST( backup_period || ' ' || backup_code AS interval) should give you an
> interval. If the units were constant, I'd say that using integer *
> interval is a better idea, but I think you'd need a function that say took
> backup_unit and gave back an interval of 1 <unit> to make that work, but
> that would possibly be cleaner overall.

This really seems like a case of a poorly chosen representation.  Why
not just have the backup interval as an interval column, ie

last_backup: timestamptz (not timestamp, btw)
backup_interval: interval, can be eg '3 days' or '1 month' or whatever.

Then you can do

WHERE last_backup + backup_interval <= current_timestamp

			regards, tom lane



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


end of thread, other threads:[~2006-03-24 23:41 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-03-23 13:23 OT: using column in an interval Bradley Kieser <brad@sms-speedway.com>
2006-03-24 22:09 ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2006-03-24 23:41   ` Tom Lane <tgl@sss.pgh.pa.us>

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