On Fri, Sep 12, 2014 at 6:40 AM, Alexander Korotkov <aekorot...@gmail.com> wrote: > It's likely that "JB_ROOT_COUNT(val) < JB_ROOT_COUNT(tmpl)" should be > checked only for objects, not arrays. Also, should JsonbDeepContains does > same fast check when it deals with nested objects?
Attached patch implements something similar to what you describe here, fixing your example. I haven't added the optimization to JsonbDeepContains(). I think that if anything, we should remove the optimization entirely, which is what I've done -- an rhs "is it contained within?" value is hardly ever going to be an object that has more pairs than the object we're checking it is contained within. It's almost certainly going to have far fewer pairs. Apart from only applying to objects, that optimization just isn't an effective way of eliminating jsonb values from consideration quickly. I'd rather not bother at all, rather than having a complicated comment about why the optimization applies to objects and not arrays. -- Peter Geoghegan
diff --git a/src/backend/utils/adt/jsonb_op.c b/src/backend/utils/adt/jsonb_op.c new file mode 100644 index 2d071b2..6fcdbad *** a/src/backend/utils/adt/jsonb_op.c --- b/src/backend/utils/adt/jsonb_op.c *************** jsonb_contains(PG_FUNCTION_ARGS) *** 117,124 **** JsonbIterator *it1, *it2; ! if (JB_ROOT_COUNT(val) < JB_ROOT_COUNT(tmpl) || ! JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl)) PG_RETURN_BOOL(false); it1 = JsonbIteratorInit(&val->root); --- 117,123 ---- JsonbIterator *it1, *it2; ! if (JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl)) PG_RETURN_BOOL(false); it1 = JsonbIteratorInit(&val->root); *************** jsonb_contained(PG_FUNCTION_ARGS) *** 137,144 **** JsonbIterator *it1, *it2; ! if (JB_ROOT_COUNT(val) < JB_ROOT_COUNT(tmpl) || ! JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl)) PG_RETURN_BOOL(false); it1 = JsonbIteratorInit(&val->root); --- 136,142 ---- JsonbIterator *it1, *it2; ! if (JB_ROOT_IS_OBJECT(val) != JB_ROOT_IS_OBJECT(tmpl)) PG_RETURN_BOOL(false); it1 = JsonbIteratorInit(&val->root);
-- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers