Skip to content

Instantly share code, notes, and snippets.

@rafaelveloso
Created June 11, 2015 10:30
Show Gist options
  • Save rafaelveloso/67b4d86e7cc799cf359d to your computer and use it in GitHub Desktop.
Save rafaelveloso/67b4d86e7cc799cf359d to your computer and use it in GitHub Desktop.
JSONB Equality Operator
CREATE OR REPLACE FUNCTION jsonb_equality(left_arg jsonb, right_arg jsonb)
RETURNS bool AS
$BODY$
SELECT left_arg @> right_arg AND left_arg <@ right_arg;
$BODY$
LANGUAGE SQL IMMUTABLE STRICT;
CREATE OPERATOR = (
LEFTARG = jsonb,
RIGHTARG = jsonb,
PROCEDURE = jsonb_equality,
NEGATOR = <>
);
SELECT '{}'::jsonb = '{}'::jsonb
$ TRUE
SELECT '{"a": "string", "b":2, "c": true}'::jsonb <> '{"c": false, "a": "string", "b":2}'::jsonb
$ TRUE
SELECT '{"a": "string", "b":2, "c": false}'::jsonb = '{"c": false, "a": "string", "b":2}'::jsonb
$ TRUE
SELECT '{"c":{"d": true}, "a":2}'::jsonb = '{"a":2, "c":{"d": true}}'::jsonb
$ TRUE
SELECT '{"c":{"d": true}, "a":2}'::jsonb <> '{"a":2, "c":{"d": true}}'::jsonb
$ FALSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment