From 02c7709961a63834b0971cc2b22b2ff2e70abb4c Mon Sep 17 00:00:00 2001 From: Paul Zumbrun Date: Wed, 9 Aug 2017 11:00:09 -0700 Subject: [PATCH] Add indices on history table foreign keys --- temporal_sqlalchemy/clock.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/temporal_sqlalchemy/clock.py b/temporal_sqlalchemy/clock.py index 0d03cb4..aaa6141 100644 --- a/temporal_sqlalchemy/clock.py +++ b/temporal_sqlalchemy/clock.py @@ -269,7 +269,11 @@ def _exclusion_in(type_, name) -> typing.Tuple: @_exclusion_in.register(sap.UUID) -def _(type_, name): +def _exclusion_in_uuid(type_, name): + """ + Cast UUIDs to text for our exclusion index because postgres doesn't + currently allow GiST indices on UUIDs. + """ return sa.cast(sa.text(name), sap.TEXT), '=' @@ -287,7 +291,9 @@ def build_history_table( table_name = truncate_identifier( _generate_history_table_name(local_table, columns) ) - entity_foreign_keys = list(util.foreign_key_to(local_table)) + # Build the foreign key(s), specifically adding an index since we may use + # a casted foreign key in our constraints. See _exclusion_in_uuid + entity_foreign_keys = list(util.foreign_key_to(local_table, index=True)) entity_constraints = [ _exclusion_in(fk.type, fk.key) for fk in entity_foreign_keys