Skip to content

Instantly share code, notes, and snippets.

@robshep
Last active August 29, 2015 14:22
Show Gist options
  • Select an option

  • Save robshep/d8e803759392ea2d2fa2 to your computer and use it in GitHub Desktop.

Select an option

Save robshep/d8e803759392ea2d2fa2 to your computer and use it in GitHub Desktop.
Build an HStore-based query index for EAV attributes
CREATE OR REPLACE FUNCTION delete_thing_att_map_hstore()
RETURNS trigger AS
$BODY$BEGIN
update
thing
set
att_map = ( att_map - OLD.def_id::text )
WHERE
id = NEW.thing_id;
RETURN NEW;
END;$BODY$
LANGUAGE plpgsql VOLATILE
CREATE OR REPLACE FUNCTION update_thing_att_map_hstore()
RETURNS trigger AS
$BODY$BEGIN
update
thing
set
att_map = coalesce(att_map, hstore(array[]::varchar[]))
||
hstore(NEW.def_id::text, NEW.value::text)
WHERE
id = NEW.thing_id;
RETURN NEW;
END;$BODY$
LANGUAGE plpgsql VOLATILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment