:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
perf(messaging): send transactional email with three queries (#125785)
Harley Alexanderba071dfd91e9
diff --git a/posthog/api/routing.py b/posthog/api/routing.pyindex 7925d5aa4e92..5d5b1da1b08c 100644--- a/posthog/api/routing.py+++ b/posthog/api/routing.py@@ -23,6 +23,7 @@ from posthog.auth import (JwtAuthentication,OAuthAccessTokenAuthentication,PersonalAPIKeyAuthentication,+ ProjectSecretAPIKeyAuthentication,SessionAuthentication,SharingAccessTokenAuthentication,SharingPasswordProtectedAuthentication,@@ -594,6 +595,8 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):def team(self) -> Team:if team_from_token := self._get_team_from_request():team = team_from_token+ elif psak_team := self._get_team_from_project_secret_api_key():+ team = psak_teamelif self._is_project_view:team = Team.objects.select_related("organization").get(id=self.project_id # KLUDGE: This is just for the period of transition to project environments@@ -616,6 +619,17 @@ class TeamAndOrgViewSetMixin(_GenericViewSet):tag_queries(**get_team_query_tags(team))return team+ def _get_team_from_project_secret_api_key(self) -> Optional[Team]:+ # The authenticator already loaded the key's team and organization. Reuse that row only when it is the+ # team the route resolves to, so a key for another team still fails the permission check.+ if not (self._is_team_view or self._is_project_view):+ return None+ authenticator = getattr(self.request, "successful_authenticator", None)+ if not isinstance(authenticator, ProjectSecretAPIKeyAuthentication):+ return None+ psak = authenticator.project_secret_api_key+ return psak.team if psak.team_id == self.team_id else None+@cached_propertydef project_id(self) -> int:if team_from_token := self._get_team_from_request():diff --git a/posthog/models/project_secret_api_key.py b/posthog/models/project_secret_api_key.pyindex 34b6160d971c..43d5f93ff423 100644--- a/posthog/models/project_secret_api_key.py+++ b/posthog/models/project_secret_api_key.py@@ -65,7 +65,8 @@ class ProjectSecretAPIKey(ModelActivityMixin, models.Model):def find_project_secret_api_key(token: str) -> Optional["ProjectSecretAPIKey"]:secure_value = hash_key_value(token)try:- return ProjectSecretAPIKey.objects.select_related("team").get(secure_value=secure_value)+ # The organization comes along so a view that resolves this team needs no second query for it.+ return ProjectSecretAPIKey.objects.select_related("team__organization").get(secure_value=secure_value)except ProjectSecretAPIKey.DoesNotExist:return Nonediff --git a/products/messaging/backend/facade/templates.py b/products/messaging/backend/facade/templates.pyindex 24033eb2ec79..d03ce3078fd7 100644--- a/products/messaging/backend/facade/templates.py+++ b/products/messaging/backend/facade/templates.py@@ -42,7 +42,7 @@ class MessageTemplateRow:tags: tuple[str, ...] = ()-def _to_contract(row: MessageTemplate) -> MessageTemplateRow:+def _to_contract(row: MessageTemplate, *, with_tags: bool = True) -> MessageTemplateRow:return MessageTemplateRow(id=row.id,name=row.name,@@ -57,7 +57,7 @@ def _to_contract(row: MessageTemplate) -> MessageTemplateRow:transactional_hog_flow_id=row.transactional_hog_flow_id,draft_content=row.draft_content,sensitive=row.sensitive,- tags=tuple(sorted(current_tag_names(row))),+ tags=tuple(sorted(current_tag_names(row))) if with_tags else (),)@@ -66,9 +66,10 @@ def list_templates(team_id: int) -> Sequence[MessageTemplateRow]:return LazyList(templates_service.team_templates(team_id), _to_contract)-def get_template(team_id: int, template_id: UUID | str) -> MessageTemplateRow:+def get_template(team_id: int, template_id: UUID | str, *, with_tags: bool = True) -> MessageTemplateRow:+ """The team's template that is not deleted. Without `with_tags`, `tags` stays empty and no tag query runs."""try:- return _to_contract(templates_service.team_template(team_id, template_id))+ return _to_contract(templates_service.team_template(team_id, template_id), with_tags=with_tags)except (MessageTemplate.DoesNotExist, ValidationError, ValueError, TypeError) as e:raise MessageTemplateMissing() from ediff --git a/products/messaging/backend/presentation/views/message_templates.py b/products/messaging/backend/presentation/views/message_templates.pyindex 4c4281df4467..31264ec77797 100644--- a/products/messaging/backend/presentation/views/message_templates.py+++ b/products/messaging/backend/presentation/views/message_templates.py@@ -628,7 +628,7 @@ class TransactionalEmailSendMixin:try:queued = send_transactional_email(self.team,- template.id,+ template,to=data["to"],distinct_id=data.get("distinct_id") or (data.get("identifiers") or {}).get("distinct_id") or None,message_data=data["message_data"],@@ -733,7 +733,8 @@ class MessageTemplatesViewSet(# Team scoping happens in the facade lookup, because the view has no queryset to filter.# DRF's detail OPTIONS metadata also calls get_object(), so the lookup must live here.try:- template = get_template(self.team_id, self.kwargs["pk"])+ # A send never reads the tags, so it skips their queries.+ template = get_template(self.team_id, self.kwargs["pk"], with_tags=self.action != "send")except MessageTemplateMissing:raise NotFound()self.check_object_permissions(self.request, template)@@ -1016,7 +1017,7 @@ class MessagingSendViewSet(TransactionalEmailSendMixin, TeamAndOrgViewSetMixin,self._require_transactional_email_flag()with transactional_email_stage("template_lookup"):try:- template = get_template(self.team_id, request.validated_data["template_id"])+ template = get_template(self.team_id, request.validated_data["template_id"], with_tags=False)except MessageTemplateMissing:raise NotFound()self.check_object_permissions(request, template)diff --git a/products/messaging/backend/services/transactional_email.py b/products/messaging/backend/services/transactional_email.pyindex d326b9e783c0..7a5d57debf9e 100644--- a/products/messaging/backend/services/transactional_email.py+++ b/products/messaging/backend/services/transactional_email.py@@ -5,7 +5,7 @@ import reimport timefrom collections.abc import Iteratorfrom contextlib import contextmanager-from typing import Any, Literal+from typing import Any, Literal, Protocolfrom uuid import UUIDfrom django.db import transaction@@ -331,8 +331,26 @@ def sync_transactional_workflow(team_id: int, template_id: UUID, *, user_id: intraise SenderUnavailable("No verified email sender. Verify a sending domain first.") from e-def _transactional_workflow_id(team_id: int, template_id: UUID) -> UUID:- template = MessageTemplate.objects.get(team_id=team_id, id=template_id, deleted=False)+class TransactionalTemplate(Protocol):+ """The template fields a send reads. A caller that already loaded the template passes it in."""++ @property+ def id(self) -> UUID: ...++ @property+ def type(self) -> str: ...++ @property+ def content(self) -> dict[str, Any]: ...++ @property+ def message_category_id(self) -> UUID | None: ...++ @property+ def transactional_hog_flow_id(self) -> UUID | None: ...+++def _transactional_workflow_id(team_id: int, template: TransactionalTemplate) -> UUID:if not template.transactional_hog_flow_id:raise TemplateNotTransactional("The template isn't transactional. Make it transactional to send it.")# A category's type can change after the template was made transactional.@@ -353,7 +371,7 @@ def _transactional_workflow_id(team_id: int, template_id: UUID) -> UUID:def send_transactional_email(team: Team,- template_id: UUID,+ template: UUID | TransactionalTemplate,*,to: str,distinct_id: str | None,@@ -370,7 +388,14 @@ def send_transactional_email(raise InvalidRecipient("The recipient address can't contain { or }.")with transactional_email_stage("workflow_lookup"):- hog_flow_id = _transactional_workflow_id(team.id, template_id)+ # A template passed in must be one of the team's templates that is not deleted, as this lookup returns.+ if isinstance(template, UUID):+ template_id = template+ model = MessageTemplate.objects.get(team_id=team.id, id=template_id, deleted=False)+ hog_flow_id = _transactional_workflow_id(team.id, model)+ else:+ template_id = template.id+ hog_flow_id = _transactional_workflow_id(team.id, template)payload = {"to": to,# The recipient is the person the send is tracked against. Without a distinct_id, the address is one.diff --git a/products/messaging/backend/tests/api/test_transactional_email.py b/products/messaging/backend/tests/api/test_transactional_email.pyindex 9fde817473e0..e9f7ea32e08f 100644--- a/products/messaging/backend/tests/api/test_transactional_email.py+++ b/products/messaging/backend/tests/api/test_transactional_email.py@@ -143,6 +143,21 @@ class TestTransactionalEmail(APIBaseTest):"variables": {"link": "https://example.com/reset", "nickname": None},}+ @parameterized.expand([("template",), ("send_email",)])+ def test_a_project_secret_api_key_send_reads_the_key_template_and_workflow_once(+ self, mock_post, _flag, _capture, route+ ):+ self._cdp_accepts(mock_post)+ client = self._psak_client(["hog_flow:write"])+ # The first send also records when the key was last used.+ assert self._send(client, route, self.body).status_code == status.HTTP_202_ACCEPTED++ # The key with its team and organization, the template, and the template's workflow.+ with self.assertNumQueries(3):+ response = self._send(client, route, self.body)++ assert response.status_code == status.HTTP_202_ACCEPTED, response.json()+def test_passes_the_idempotency_key_to_the_pipeline_and_reports_a_replay(self, mock_post, _flag, capture):mock_post.return_value = MagicMock(status_code=202, json=lambda: {"invocation_id": "first-send", "replayed": True}