% CREATE EXTENSION pair; CREATE EXTENSION % SELECT pair('foo', 'bar'); pair ------------ (foo,bar) % SELECT 'foo' ~> 'bar'; pair ------------ (foo,bar)
This library contains a single PostgreSQL extension, a key/value pair data
pair, along with a convenience function for constructing
key/value pairs. It's just a simple thing, really: a two-value composite type
that can store any type of value in its slots, which are named "k" and "v".
So what's it good for? Well, the main idea is if you have a custom function to which you'd like to be able to pass any number of key/value pairs. You could use hstore of course, but maybe it's overkill, or you need to guarantee the order in which the pairs are passed. If so, then this extension is for you.
pair data type was created as an inspiration, as documented in
this blog post.
Give it a read if you're interested in the context of its creation.
There are two ways to construct key/value pairs: Via the
% SELECT pair('foo', 'bar'); pair ------------ (foo,bar)
Or by using the
% SELECT 'foo' ~> 'bar'; pair ------------ (foo,bar)
To access the values, just use the
v column names:
SELECT ('foo' ~> 'bar').k; k ----- foo (1 row) SELECT ('foo' ~> 'bar').v; v ----- bar
Kind of ugly, huh? Well pairs aren't very useful on their own. Where they really come into their own is when used as the last parameter to a variadic function.
For example, say you wanted a function to store any number of key/value pairs in a table. Here's what it might look like:
CREATE OR REPLACE FUNCTION store( params variadic pair ) RETURNS VOID LANGUAGE plpgsql AS $$ DECLARE param pair; BEGIN FOR param IN SELECT * FROM unnest(params) LOOP UPDATE kvstore SET value = param.v, WHERE key = param.k; CONTINUE WHEN FOUND; INSERT INTO kvstore (key, value) VALUES (param.k, param.v); END LOOP; END; $$;
And to use it, pass in any number of pairs you like:
SELECT store( 'foo' ~> 'bar', 'baz' ~> 1 );
Copyright and License
Copyright (c) 2010-2011 David E. Wheeler.
This module is free software; you can redistribute it and/or modify it under the PostgreSQL License.
Permission to use, copy, modify, and distribute this software and its documentation for any purpose, without fee, and without a written agreement is hereby granted, provided that the above copyright notice and this paragraph and the following two paragraphs appear in all copies.
In no event shall David E. Wheeler be liable to any party for direct, indirect, special, incidental, or consequential damages, including lost profits, arising out of the use of this software and its documentation, even if David E. Wheeler has been advised of the possibility of such damage.
David E. Wheeler specifically disclaims any warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose. The software provided hereunder is on an "as is" basis, and David E. Wheeler has no obligations to provide maintenance, support, updates, enhancements, or modifications.