I have several partitioned tables in PostgreSQL, and an ecto query that adds a filter to limit the number of partitions scanned. The filter on the top-level query successfully uses partition pruning. I notice that the preload queries use parent_id = ANY($1) as the filter, which results in all partitions being scanned. For other reasons I cannot yet rely on using a JOIN to preload these records, so the Query level preloads will not be helpful here yet.
While I have read and understand that one can do this through Preload Queries I wanted to know if there was an easier way as I have to potentially apply this pattern to many dynamically assembled queries. An ‘easier way’ being some keyword arg that I could have missed in the docs.
An alternative question is: I know I want to query 4 tables that are all partitioned the same way and all have the consistent prefix of table_X, is there a way to flag to the Ecto engine that instead of using the Table schema to instead resolve the target table as the partitioned key? Not automagically ofcourse but through something like
There are a few ways to do this, the most reliable one would be to use reflection to get the actual table name and add the suffix you need for the correct partition, then you can simply query it as Ecto supports querying by the table name as string:
Another nice feature in Ecto is that you can supply both a table name and a schema module as the “queryable” in the first parameter of most Ecto.Query functions. That lets you do things like:
from({"table_#{partition_key}", Table}, ...)
The results will be deserialized into Table schemas/structs, if that’s valuable to your situation.
While I very much like this solution and I think this is probably the most workable one so far, some of the libraries that this application uses are introspection heavy, any expect the atom of the schema. But its probably easier to remove those libraries in this case, or modify them to allow for this tuple-ized version.