Path extraction with #> and #>> in PostgreSQL - Time & Space Complexity
We want to understand how the time to extract data from JSON grows as the JSON gets bigger.
Specifically, how does using #> and #>> operators affect performance when accessing nested data?
Analyze the time complexity of the following PostgreSQL JSON path extraction queries.
SELECT data #> '{a,b,c}' AS value1 FROM json_table;
SELECT data #>> '{a,b,c}' AS value2 FROM json_table;
These queries extract nested values from a JSON column using path operators.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing the JSON structure along the given path keys.
- How many times: Once per row, and once per key in the path (each key leads to next nested level).
The time grows with the length of the path and the size of the JSON at each level.
| Input Size (path length n) | Approx. Operations |
|---|---|
| 3 | 3 steps to reach nested value |
| 5 | 5 steps to reach nested value |
| 10 | 10 steps to reach nested value |
Pattern observation: The number of steps grows linearly with the path length.
Time Complexity: O(n)
This means the time to extract a value grows linearly with the number of keys in the path.
[X] Wrong: "Extracting a nested JSON value is always constant time regardless of path length."
[OK] Correct: Each key in the path requires looking inside the JSON at that level, so longer paths take more steps.
Understanding how JSON path extraction scales helps you write efficient queries and explain performance trade-offs clearly.
What if the JSON data is deeply nested but the path length is short? How would the time complexity change?
Practice
#> do when used with JSON data?Solution
Step 1: Understand the operator
This operator extracts JSON data from a JSON column or value at the given path as JSON type.#>Step 2: Compare with similar operator
The#>>#>>operator extracts text, not JSON, at the path.Final Answer:
Extracts JSON data at a specified path -> Option CQuick Check:
#>extracts JSON data [OK]
- Confusing #> with #>>
- Thinking #> extracts text
- Assuming #> modifies JSON
data using the #>> operator?Solution
Step 1: Understand
The#>>syntax#>>operator requires a text array path inside curly braces, e.g., '{name}', to extract text.Step 2: Compare with other operators
->>extracts text but uses a different syntax without braces;#>extracts JSON, not text.Final Answer:
SELECT data #>> '{name}' FROM table; -> Option BQuick Check:
Correct syntax for #>> uses braces and returns text [OK]
- Using #> instead of #>> for text extraction
- Omitting braces around path
- Confusing ->> with #>> syntax
info with value '{"person": {"name": "Alice", "age": 30}}', what is the result of the query:SELECT info #>> '{person,name}' FROM users;Solution
Step 1: Understand the JSON and path
The JSON has a nested object under 'person' with key 'name' having value "Alice".Step 2: Apply
#>>operator#>>extracts the text value at the path, so it returns the string without quotes.Final Answer:
Alice -> Option AQuick Check:
#>>returns text without quotes [OK]
- Expecting JSON with quotes returned
- Confusing #> and #>> output types
- Assuming NULL if nested path
SELECT data #>> '{address,city}' FROM customers;If this query returns NULL unexpectedly, what is the most likely cause?
Solution
Step 1: Understand NULL from JSON path extraction
If the specified path does not exist in the JSON data, the operator returns NULL.Step 2: Check other options
The syntax is correct; #>> is valid for text extraction; invalid JSON would cause errors, not NULL.Final Answer:
The path '{address,city}' does not exist in some JSON rows -> Option AQuick Check:
Missing path returns NULL [OK]
- Assuming syntax error causes NULL
- Confusing #> and #>> usage
- Ignoring missing keys in JSON
profile with nested data:{"user": {"details": {"email": "user@example.com", "phone": "12345"}}}Which query correctly extracts the email as text using the
#>> operator?Solution
Step 1: Understand the nested JSON path
The email is nested under keys user, details, then email.Step 2: Use correct operator and path syntax
#>>extracts text and requires path as an array in braces: '{user,details,email}'.Step 3: Evaluate other options
#>returns JSON, not text; SELECT profile->'user'->'details'->>'email' FROM accounts; uses different operators but is valid syntax; SELECT profile #>> 'user.details.email' FROM accounts; uses incorrect path format.Final Answer:
SELECT profile #>> '{user,details,email}' FROM accounts; -> Option DQuick Check:
Use #>> with array path in braces for text extraction [OK]
- Using dot notation inside quotes for path
- Using #> instead of #>> for text
- Confusing operator syntax with -> and ->>
Ready to prove it?
Pass the test and this pattern moves unknown
