Is it somehow possible to extend processInstanceQueries with custom SQL code? I would use this for a custom where-condition.
You can make use of NativeQueries. They allow you to directly use SQL:
runtimeService.createNativeProcessInstanceQuery().sql("SELECT * FROM " + managementService.getTableName(ProcessInstance.class) WHERE <<YOUR CUSTOM WHERE HERE>>).list()
An example can be found here:
Alternatively, you can execute your own custom SQL by creating a MyBatis mapper. See here for details: https://www.flowable.org/docs/userguide/index.html#advanced.custom.sql.queries
Hope it helped!