Optimize repeated validation with Compiled Schemas
masterIf you are validating many rows against the same schema (e.g., in a large table scan or bulk insert), you should use the compiled schema type.
By casting your schema to the jsonschema type once, the validator is compiled and cached per callsite. This avoids the overhead of recompiling the schema for every single row, providing significant performance gains (e.g., ~1.8x speedup in benchmarks).
Compiled Functions
json_matches_compiled_schema(schema jsonschema, instance json)jsonb_matches_compiled_schema(schema jsonschema, instance jsonb)jsonschema_validation_errors_compiled(schema jsonschema, instance json)jsonb_validation_errors_compiled(schema jsonschema, instance jsonb)
-- Cast schema to jsonschema once to benefit from caching
SELECT jsonb_matches_compiled_schema(
'{"type": "object"}'::jsonschema,
'{"key": "value"}'::jsonb
);