We've wanted to do this but the last I checked, Citus, the software we use to shard our postgres databases, isn't able to handle setting configs in a query.
We've wanted to do this but the last I checked, Citus, the software we use to shard our postgres databases, isn't able to handle setting configs in a query.
We use inheritance (with check constraints) [1] for both time partitioning as well as for custom (aka unstructured) events. Most events have several (100s in our case) common columns. When you see a new custom event with custom fields you want to separate out you can create a subtable and have it inherit from the base event table.
Now querying for those custom events is extremely fast and you can query from the base event type or the subtable directly.
[1]: https://www.postgresql.org/docs/current/static/ddl-partition...
However if you add columns to your subtables (which is usually the case for adding events by type) you will need to insert into the correct table.
You can enforce this by making it so you can never insert into the parent table
-- FORCE programmatic dispatch to table (v9.2).
ALTER TABLE event ADD CONSTRAINT event_no_insert CHECK (false) no inherit;
Now you have to insert in the chid tables. This requires business logic on your end.As far as refactoring goes the typically approach is to create the new tables and insert the data into them and then do a name swap and then add the check constraints.