Thread: Asssociative Arrays: Best practices / snippets?

Asssociative Arrays: Best practices / snippets?

From
Stefan Keller
Date:
I have a use case where the I want to put an unforeseable number of key/value pairs in a column.
Now, PostgreSQL has arrays as first class types.
Are there any best practices and snippets (preferrably in plpgsql) for handling key/value pairs?
-- S.

Re: Asssociative Arrays: Best practices / snippets?

From
"Leif B. Kristensen"
Date:
On Saturday 23. May 2009, Stefan Keller wrote:
>I have a use case where the I want to put an unforeseable number of
>key/value pairs in a column.
>Now, PostgreSQL has arrays as first class types.
>Are there any best practices and snippets (preferrably in plpgsql) for
>handling key/value pairs?
>-- S.

The «best practice» here is probably rethinking your design. Storing
an «unforeseeable number of key/value pairs in a column» is a blatant
violation of good database design principles.

Rodrigo posted this link a few hours ago:

http://joecelkothesqlapprentice.blogspot.com/2006/04/using-one-table-vs-many.html
--
Leif Biberg Kristensen | Registered Linux User #338009
Me And My Database: http://solumslekt.org/blog/

Re: Asssociative Arrays: Best practices / snippets?

From
Filip Rembiałkowski
Date:


2009/5/23 Stefan Keller <sfkeller@gmail.com>
I have a use case where the I want to put an unforeseable number of key/value pairs in a column.
Now, PostgreSQL has arrays as first class types.
Are there any best practices and snippets (preferrably in plpgsql) for handling key/value pairs?
-- S.


common relational practice is to use entity-attribute-value model to attach any number of key/value pairs to any object, for example:
object( object_id, ... )
attribute ( attr_id, attr_name )
object_attribute ( object_id, attr_id, attr_value )


if your a lazy DBA and text key/values are all you need, did you look at hstore? it might be enough.

my personal hstore experience is:
1) keep your key/values reasonably small; if you need structured data - do not put it into hstore.
2) keep in mind that hstore indexing is limited, many search operations require full table scan




--
Filip Rembiałkowski
JID,mailto:filip.rembialkowski@gmail.com
http://filip.rembialkowski.net/

Re: Asssociative Arrays: Best practices / snippets?

From
Steve Atkins
Date:
On May 23, 2009, at 2:25 AM, Stefan Keller wrote:

> I have a use case where the I want to put an unforeseable number of
> key/value pairs in a column.
> Now, PostgreSQL has arrays as first class types.
> Are there any best practices and snippets (preferrably in plpgsql)
> for handling key/value pairs?

Take a look at "hstore". It's a data type in contrib that's intended
to store a set of key, value
pairs in a single column.

It's well suited for the case where you're not searching on the keys
and values. If you do
need to search on them then you can either index the hstore field with
a gin or gist index,
or use a separate EAV table for the keys and values.

Cheers,
   Steve


Re: Asssociative Arrays: Best practices / snippets?

From
Stefan Keller
Date:
Steve, Filip: Many thanks for your patient answers and concerns. Did'nt know hstore!
 
=> Is it enough to run the hstore.sql (found in ..\8.3\share\contrib\ plus .dll) in order to install this contrib type under Windows?
 
=> What's the status of this contribution? Is it going to be still in 8.4? (else I would still consider Arrays).
 
As for the background: I'm importing data from OpenStreetMap - "the Wikipedia of Maps" - actuall by applying "good" database design principles. This entity-attribute-value model get's mapped to a relational one. Now I won't and can't map all attributes (called tags). That's where the idea about associative arrays came in. The KVPs would be an ANDed in a search with "regular" columns.

So, my answer to Leif's hot-blooded judgment about KVPs could be: "Know when to break the rules" ;->
-S.
 
2009/5/23 Steve Atkins <steve@blighty.com>

On May 23, 2009, at 2:25 AM, Stefan Keller wrote:

I have a use case where the I want to put an unforeseable number of key/value pairs in a column.
Now, PostgreSQL has arrays as first class types.
Are there any best practices and snippets (preferrably in plpgsql) for handling key/value pairs?

Take a look at "hstore". It's a data type in contrib that's intended to store a set of key, value
pairs in a single column.

It's well suited for the case where you're not searching on the keys and values. If you do
need to search on them then you can either index the hstore field with a gin or gist index,
or use a separate EAV table for the keys and values.

Cheers,
 Steve



--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general