Adjusted construction of statement name aliases, to be thread-safe.
@@ -379,6 +379,7 @@ class PostgresAggregateRecorder(AggregateRecorder):
events_table_name: str,
):
self.statement_name_aliases: Dict[str, str] = {}
+ self.statement_name_aliases_lock = Lock()
self.check_table_name_length(events_table_name, datastore.schema)
self.datastore = datastore
self.events_table_name = events_table_name
@@ -415,6 +416,10 @@ class PostgresAggregateRecorder(AggregateRecorder):
raise ProgrammingError(f"Table name too long: {unqualified_table_name}")
def get_statement_alias(self, statement_name: str) -> str:
+ try:
+ alias = self.statement_name_aliases[statement_name]
+ except KeyError:
+ with self.statement_name_aliases_lock:
try:
alias = self.statement_name_aliases[statement_name]
except KeyError:
@@ -426,11 +431,15 @@ class PostgresAggregateRecorder(AggregateRecorder):
alias = statement_name
self.statement_name_aliases[statement_name] = alias
else:
- uid = uuid5(NAMESPACE_URL, f"/statement_names/{statement_name}").hex
+ uid = uuid5(
+ NAMESPACE_URL, f"/statement_names/{statement_name}"
+ ).hex
alias = uid
for i in range(len(uid)): # pragma: no cover
preserve_end = 21
- preserve_start = PG_IDENTIFIER_MAX_LEN - preserve_end - i - 2
+ preserve_start = (
+ PG_IDENTIFIER_MAX_LEN - preserve_end - i - 2
+ )
uuid5_tail = i
candidate = (
statement_name[:preserve_start]
