0

I've been trying to understand the difference between the PostgreSQL @? and @@ JSONB operators. Why do these return different results?

david=#  SELECT '{ "email": { "main": "[email protected]" } }' @? '$ ?(@.email.main == "[email protected]")';
 ?column? 
----------
 t

david=#  SELECT '{ "email": { "main": "[email protected]" } }' @@ '$ ?(@.email.main == "[email protected]")';
 ?column? 
----------
 f

I thought maybe they're equivalent to the jsonb_path_exists() and jsonb_path_match() functions:

david=#  SELECT jsonb_path_exists('{ "email": { "main": "[email protected]" } }', '$ ?(@.email.main == "[email protected]")');
 jsonb_path_exists 
-------------------
 t

david=#  SELECT jsonb_path_match('{ "email": { "main": "[email protected]" } }', '$ ?(@.email.main == "[email protected]")');
ERROR:  single boolean result is expected

That's a bit more informative, and jsonb_path_match() example in the docs show the use of exists(), and that does seem to work:

david=#  SELECT jsonb_path_match('{ "email": { "main": "[email protected]" } }', 'exists($ ?(@.email.main == "[email protected]"))');
 jsonb_path_match 
------------------
 t

But the same is not true of @@:

david=#  SELECT '{ "email": { "main": "[email protected]" } }' @@ 'exists($ ?(@.email.main == "[email protected]"))';
 ?column? 
----------
 f

So color me confused. I do not understand the differences here.

2 Answers 2

2

With thanks to @jjanes for pointing me in the right direction, and to the denizens of the pgsql-hackers mail list, I believe I got the differences worked out. This blog post details figuring it out, but the conclusion is this:

  • Postgres supports a nonstandard syntax for SQL/JSON path queries where the entire query can be a boolean predicate, and the value returned by the query is the value returned by the predicate: true, false, or unknown (translated to SQL null).
  • @? (and jsonb_path_exists()) returns true if the path query returns any values --- even false or null --- and false if it returns no values. This operator should be used only with SQL-standard JSON path queries that select data from the JSONB. Do not use predicate-only JSON path expressions with @?.
  • @@ (and jsonb_path_match()) returns true if the path query returns the single boolean value true and false otherwise. This operator should be used only with Postgres-specific boolean predicate JSON path queries, that return data from the predicate expression. Do not use SQL-standard JSON path expressions with @@.
2
  • 1
    Great, this confirms my previous experiments, which also found what happens when you use a boolean predicate against multiple values (e.g. from an array)
    – Bergi
    Oct 15 at 0:52
  • Oh, very nice, @Bergi!
    – theory
    Oct 15 at 22:15
0

Try visualizing with this:

SELECT jsonb_path_query('{ "email": { "main": "[email protected]" } }', '$ ?(@.email.main == "[email protected]")');

The above does return something, which why is why @? is true. But what it returns is not the boolean value 'true', which is why @@ is false. And is not a boolean at all, which is why jsonb_path_match gives an error. That is my casual interpretation of it, anyway.

This is all very subtle, and I find the docs are hard to understand unless you already know what they are saying.

1
  • Ah, interesting. I can use $.email.main == "[email protected]" (PostgreSQL-specific syntax) to select a bool, and that does indeed work. Also exists($ ?(@.email.main == "[email protected]")). Thanks for the pointer to jsonb_path_query(), making a mental note to test paths with that function to make sure they return booleans to use with jsonb_path_match(). The docs could use some explication on the need for a boolean and typical path expressions to produce one.
    – theory
    Sep 6 at 21:17

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.