Temporal data storage
Temporal data storage
Hello,
I have person to group mapping in which I want to store the time, when this
mapping is valid. I can store the valid time in an array or in seperate rows.
in rows:
create table group_mapping (
group name not null,
person integer not null,
valid_since timestamptz not null,
valid_until timestamptz not null
);
insert into group_mapping values('test', 1, '2000-1-1', '2001-1-1');
insert into group_mapping values('test', 1, '2002-1-1', '2002-1-1');
or in an array:
create type period as (
since timestamptz,
"until" timestamptz
);
create table group_mapping_array (
group name not null,
person integer not null,
valid_time period[] not null
);
insert into group_mapping_array values('test', 1,
array[
('2000-1-1', '2001-1-1'),
('2002-1-1', '2003-1-1')
]::period[]
);
some advantages and disadvantages:
with rows:
+ a gist index already exists (must be modified)
+ can add other attributes to the valid time
- prevent overlapping is very complex
- binary operations like "and", "or" and "not" operates on multiple rows
with array:
+ overlapping can simply done with a constraint and a function
+ binary operations like "and", "or" and "not" are easy to implement
- a gist index must be written nearly from scratch
- adding other attributes is complex
Are there other (dis)advantages I have forgotten?
Which solution is better?
Are there any drawbacks by using arrays in the rows?
Thanks,
Gerhard
Re: Temporal data storage
От:
Jeff Davis <pgsql@j-davis.com>
Дата:
On Mon, 2010-04-05 at 18:28 +0200, Gerhard Heift wrote: > create type period as ( > since timestamptz, > "until" timestamptz > ); Please take a look at: http://pgfoundry.org/projects/temporal That may be a more useful type for you, and it's also called "PERIOD". > with rows: > + a gist index already exists (must be modified) > + can add other attributes to the valid time > - prevent overlapping is very complex See Exclusion Constraints (in upcoming 9.0 release): http://developer.postgresql.org/pgdocs/postgres/sql-createtable.html#SQL-CREATETABLE-EXCLUDE http://thoughts.j-davis.com/2009/11/08/temporal-keys-part-2/ > - binary operations like "and", "or" and "not" operates on multiple rows > > with array: > + overlapping can simply done with a constraint and a function I believe that you mean "preventing overlap within the array", and you're correct. However, if you want to prevent overlapping between two tuples, you need to use Exclusion Constraints. Regards, Jeff Davis