The error you're encountering:
psycopg2.errors.InvalidParameterValue: cannot call jsonb_each on a non-object
indicates that the jsonb_each function is being invoked on a JSONB value that is not an object. Even though your WHERE clause includes:
AND jsonb_typeof({board}.voll::jsonb->'posts') = 'object'
the error persists because the CROSS JOIN LATERAL jsonb_each(...) is executed before the WHERE clause filters out the non-object rows. This means that jsonb_each still attempts to process rows where 'posts' might not be an object, leading to the error.
To resolve this issue, you need to ensure that jsonb_each is only called on JSONB values that are guaranteed to be objects. One effective way to achieve this is by restructuring your query to filter the rows before performing the CROSS JOIN LATERAL. Here's how you can modify your query:
Solution: Use a Subquery to Pre-filter Rows
By using a subquery, you can filter out any rows where 'posts' is not an object before applying jsonb_each. Here's the revised query:
cursor.execute(f'''
INSERT INTO messages_fts (id1, id2, board, message, subject, files, date)
SELECT
b.id AS id1,
(p.value::jsonb->>'postId')::BIGINT AS id2,
%s AS board,
p.value::jsonb->>'message' AS message,
p.value::jsonb->>'subject' AS subject,
p.value::jsonb->>'files' AS files,
to_timestamp(split_part(b.zeit, 'Z', 1), 'YYYY-MM-DD"T"HH24:MI:SS.MS') AS date
FROM (
SELECT *
FROM {board}
WHERE {board}.voll ~ '^\\s*\\{'
AND jsonb_typeof({board}.voll::jsonb) = 'object'
AND jsonb_typeof({board}.voll::jsonb->'posts') = 'object'
) AS b
CROSS JOIN LATERAL jsonb_each(b.voll::jsonb->'posts') AS p(key, value)
WHERE
p.value::jsonb->>'message' IS NOT NULL
OR p.value::jsonb->>'subject' IS NOT NULL;
''', (board,))
Explanation of Changes
-
Subquery (FROM Clause):
- Purpose: Filters the
{board} table before performing the CROSS JOIN LATERAL.
- Conditions Applied:
{board}.voll ~ '^\\s*\\{': Ensures the voll column starts with a curly brace after any whitespace.
jsonb_typeof({board}.voll::jsonb) = 'object': Ensures that voll is a JSON object.
jsonb_typeof({board}.voll::jsonb->'posts') = 'object': Ensures that the 'posts' key within voll is also a JSON object.
-
CROSS JOIN LATERAL:
- Usage: Applies
jsonb_each only to the already filtered rows where 'posts' is guaranteed to be an object.
- Alias: Renamed the subquery as
b for clarity.
-
SELECT Clause Adjustments:
- Alias Usage: Prefixed column references with
b. to refer to the subquery's alias.
- Date Conversion: Ensured that
split_part(b.zeit, 'Z', 1) correctly references the zeit column from the subquery.
-
WHERE Clause:
- Moved the condition to filter rows based on the presence of
'message' or 'subject' after the CROSS JOIN LATERAL.
Alternative Approach: Use Conditional Lateral Join
If you prefer not to use a subquery, another approach is to conditionally apply the CROSS JOIN LATERAL only when 'posts' is an object. This can be achieved using a CASE statement or by leveraging jsonb_typeof directly within the CROSS JOIN LATERAL. However, the subquery method is generally more straightforward and readable.
Final Recommendations
-
Data Validation: Ensure that the data in the {board} table adheres to the expected JSON structure. If 'posts' can sometimes be an array or another type, consider adjusting your data model or handling those cases explicitly.
-
Error Handling: Implement error handling in your Python code to catch such exceptions and possibly log the offending rows for further inspection.
-
Performance Considerations: Using subqueries can sometimes impact performance. Make sure to analyze the query plan and add appropriate indexes if necessary, especially on JSONB fields if you frequently query them.
By restructuring your query to filter out non-object 'posts' before applying jsonb_each, you should eliminate the InvalidParameterValue error and ensure that only valid JSON objects are processed.