: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(replay): drop redundant playlist list COUNT query (#63882)
Paul D'Ambra1a28ea7be5f3
diff --git a/posthog/session_recordings/session_recording_playlist_api.py b/posthog/session_recordings/session_recording_playlist_api.pyindex 35185da8090e..b7707fff7956 100644--- a/posthog/session_recordings/session_recording_playlist_api.py+++ b/posthog/session_recordings/session_recording_playlist_api.py@@ -591,11 +591,11 @@ class SessionRecordingPlaylistViewSet(# Get regular DB playlistsqueryset = self.safely_get_queryset(self.get_queryset())- # Get the total count of DB playlists before pagination- db_count = queryset.count()-- # Apply pagination to DB playlists+ # Apply pagination to DB playlists. The paginator computes the DB count+ # internally, so reuse it rather than issuing a second COUNT(*).page = self.paginate_queryset(queryset)+ paginator = cast(LimitOffsetPagination, self.paginator)+ db_count = paginator.count if page is not None and paginator.count is not None else queryset.count()# Check if we're on the first page by looking at offset parameter# Synthetic playlists should only appear on the first page to avoid duplicatesdiff --git a/posthog/session_recordings/test/__snapshots__/test_session_recording_playlist.ambr b/posthog/session_recordings/test/__snapshots__/test_session_recording_playlist.ambrindex cf2d2218e6b4..e6f0896073c5 100644--- a/posthog/session_recordings/test/__snapshots__/test_session_recording_playlist.ambr+++ b/posthog/session_recordings/test/__snapshots__/test_session_recording_playlist.ambr@@ -50,19 +50,6 @@'''# ---# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.10- '''- SELECT COUNT(*) AS "__count"- FROM "posthog_sessionrecordingplaylist"- WHERE (NOT "posthog_sessionrecordingplaylist"."deleted"- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND "posthog_sessionrecordingplaylist"."type" = 'filters'- AND "posthog_sessionrecordingplaylist"."team_id" = 99999- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND "posthog_sessionrecordingplaylist"."type" = 'filters')- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.11'''SELECT "posthog_sessionrecordingplaylist"."id","posthog_sessionrecordingplaylist"."short_id",@@ -274,7 +261,7 @@LIMIT 100'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.12+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.11'''SELECT "posthog_sessionrecordingplaylistitem"."playlist_id" AS "playlist_id",COUNT("posthog_sessionrecordingplaylistitem"."id") AS "c"@@ -291,7 +278,7 @@GROUP BY 1'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.13+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.12'''SELECT "posthog_sessionrecordingplaylistitem"."playlist_id" AS "playlist_id","posthog_sessionrecordingplaylistitem"."recording_id" AS "recording_id"@@ -305,7 +292,7 @@5 /* ... */))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.14+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.13'''SELECT "posthog_user"."id","posthog_user"."password",@@ -350,7 +337,7 @@AND "posthog_user"."id" = 99999)'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.15+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.14'''SELECT "posthog_organization"."id","posthog_organization"."name",@@ -392,7 +379,7 @@LIMIT 21'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.16+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.15'''SELECT "posthog_team"."id","posthog_team"."uuid",@@ -529,7 +516,7 @@LIMIT 21'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.17+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.16'''SELECT "posthog_organizationmembership"."id","posthog_organizationmembership"."organization_id",@@ -580,7 +567,7 @@LIMIT 21'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.18+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.17'''SELECT "ee_accesscontrol"."id","ee_accesscontrol"."team_id",@@ -626,7 +613,7 @@AND "ee_accesscontrol"."team_id" = 99999))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.19+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.18'''SELECT "posthog_organizationmembership"."id","posthog_organizationmembership"."organization_id",@@ -675,6 +662,19 @@WHERE "posthog_organizationmembership"."user_id" = 99999'''# ---+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.19+ '''+ SELECT COUNT(*) AS "__count"+ FROM "posthog_sessionrecordingplaylist"+ WHERE (NOT "posthog_sessionrecordingplaylist"."deleted"+ AND NOT "posthog_sessionrecordingplaylist"."deleted"+ AND "posthog_sessionrecordingplaylist"."type" = 'collection'+ AND "posthog_sessionrecordingplaylist"."team_id" = 99999+ AND NOT "posthog_sessionrecordingplaylist"."deleted"+ AND NOT "posthog_sessionrecordingplaylist"."deleted"+ AND "posthog_sessionrecordingplaylist"."type" = 'collection')+ '''+# ---# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.2'''SELECT "posthog_filesystem"."team_id",@@ -701,32 +701,6 @@'''# ---# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.20- '''- SELECT COUNT(*) AS "__count"- FROM "posthog_sessionrecordingplaylist"- WHERE (NOT "posthog_sessionrecordingplaylist"."deleted"- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND "posthog_sessionrecordingplaylist"."type" = 'collection'- AND "posthog_sessionrecordingplaylist"."team_id" = 99999- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND "posthog_sessionrecordingplaylist"."type" = 'collection')- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.21- '''- SELECT COUNT(*) AS "__count"- FROM "posthog_sessionrecordingplaylist"- WHERE (NOT "posthog_sessionrecordingplaylist"."deleted"- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND "posthog_sessionrecordingplaylist"."type" = 'collection'- AND "posthog_sessionrecordingplaylist"."team_id" = 99999- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND "posthog_sessionrecordingplaylist"."type" = 'collection')- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.22'''SELECT "posthog_sessionrecordingplaylist"."id","posthog_sessionrecordingplaylist"."short_id",@@ -938,7 +912,7 @@LIMIT 100'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.23+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.21'''SELECT "posthog_sessionrecordingplaylistitem"."playlist_id" AS "playlist_id",COUNT("posthog_sessionrecordingplaylistitem"."id") AS "c"@@ -955,7 +929,7 @@GROUP BY 1'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.24+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.22'''SELECT "posthog_sessionrecordingplaylistitem"."playlist_id" AS "playlist_id","posthog_sessionrecordingplaylistitem"."recording_id" AS "recording_id"@@ -969,7 +943,7 @@5 /* ... */))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.25+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.23'''SELECT "posthog_sessionrecordingviewed"."session_id" AS "session_id"FROM "posthog_sessionrecordingviewed"@@ -978,7 +952,7 @@AND "posthog_sessionrecordingviewed"."session_id" IN ('00000000-0000-0000-0000-000000000000'))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.26+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.24'''SELECT COUNT(*) AS "__count"FROM "posthog_sessionrecordingviewed"@@ -986,7 +960,7 @@AND "posthog_sessionrecordingviewed"."user_id" = 99999)'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.27+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.25'''SELECT COUNT(*)FROM@@ -998,7 +972,7 @@AND NOT ("posthog_comment"."item_id" IS NULL))) subquery'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.28+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.26'''SELECT COUNT(*)FROM@@ -1009,7 +983,7 @@AND NOT ("posthog_sharingconfiguration"."recording_id" IS NULL))) subquery'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.29+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.27'''SELECT COUNT(*)FROM@@ -1024,52 +998,7 @@AND "posthog_exportedasset"."export_context" IS NOT NULL))) subquery'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.3- '''- SELECT "posthog_user"."id",- "posthog_user"."password",- "posthog_user"."last_login",- "posthog_user"."first_name",- "posthog_user"."last_name",- "posthog_user"."is_staff",- "posthog_user"."date_joined",- "posthog_user"."uuid",- "posthog_user"."current_organization_id",- "posthog_user"."current_team_id",- "posthog_user"."email",- "posthog_user"."pending_email",- "posthog_user"."distinct_id",- "posthog_user"."is_email_verified",- "posthog_user"."credentials_reviewed_at",- "posthog_user"."requested_2fa_reset_at",- "posthog_user"."has_seen_product_intro_for",- "posthog_user"."strapi_id",- "posthog_user"."is_active",- "posthog_user"."role_at_organization",- "posthog_user"."theme_mode",- "posthog_user"."partial_notification_settings",- "posthog_user"."anonymize_data",- "posthog_user"."allow_impersonation",- "posthog_user"."toolbar_mode",- "posthog_user"."hedgehog_config",- "posthog_user"."allow_sidebar_suggestions",- "posthog_user"."shortcut_position",- "posthog_user"."passkeys_enabled_for_2fa",- "posthog_user"."hide_mcp_hints",- "posthog_user"."onboarding_skipped_at",- "posthog_user"."onboarding_skipped_reason",- "posthog_user"."onboarding_skipped_organization_id",- "posthog_user"."onboarding_delegated_to_invite_id",- "posthog_user"."onboarding_delegated_to_organization_id",- "posthog_user"."onboarding_delegation_accepted_at",- "posthog_user"."events_column_config",- "posthog_user"."email_opt_in"- FROM "posthog_user"- WHERE ("posthog_user"."is_active"- AND "posthog_user"."id" = 99999)- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.30+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.28'''SELECT "posthog_datawarehousesavedquery"."created_by_id","posthog_datawarehousesavedquery"."created_at",@@ -1127,7 +1056,7 @@ORDER BY "posthog_datawarehousesavedquery"."name" ASC'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.31+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.29'''SELECT "posthog_datawarehousesavedquery"."created_by_id","posthog_datawarehousesavedquery"."created_at",@@ -1177,7 +1106,52 @@AND "posthog_datawarehousesavedquery"."deleted" IS NOT NULL))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.32+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.3+ '''+ SELECT "posthog_user"."id",+ "posthog_user"."password",+ "posthog_user"."last_login",+ "posthog_user"."first_name",+ "posthog_user"."last_name",+ "posthog_user"."is_staff",+ "posthog_user"."date_joined",+ "posthog_user"."uuid",+ "posthog_user"."current_organization_id",+ "posthog_user"."current_team_id",+ "posthog_user"."email",+ "posthog_user"."pending_email",+ "posthog_user"."distinct_id",+ "posthog_user"."is_email_verified",+ "posthog_user"."credentials_reviewed_at",+ "posthog_user"."requested_2fa_reset_at",+ "posthog_user"."has_seen_product_intro_for",+ "posthog_user"."strapi_id",+ "posthog_user"."is_active",+ "posthog_user"."role_at_organization",+ "posthog_user"."theme_mode",+ "posthog_user"."partial_notification_settings",+ "posthog_user"."anonymize_data",+ "posthog_user"."allow_impersonation",+ "posthog_user"."toolbar_mode",+ "posthog_user"."hedgehog_config",+ "posthog_user"."allow_sidebar_suggestions",+ "posthog_user"."shortcut_position",+ "posthog_user"."passkeys_enabled_for_2fa",+ "posthog_user"."hide_mcp_hints",+ "posthog_user"."onboarding_skipped_at",+ "posthog_user"."onboarding_skipped_reason",+ "posthog_user"."onboarding_skipped_organization_id",+ "posthog_user"."onboarding_delegated_to_invite_id",+ "posthog_user"."onboarding_delegated_to_organization_id",+ "posthog_user"."onboarding_delegation_accepted_at",+ "posthog_user"."events_column_config",+ "posthog_user"."email_opt_in"+ FROM "posthog_user"+ WHERE ("posthog_user"."is_active"+ AND "posthog_user"."id" = 99999)+ '''+# ---+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.30'''SELECT "posthog_externaldatasource"."created_by_id","posthog_externaldatasource"."created_at",@@ -1211,7 +1185,7 @@AND "posthog_externaldatasource"."deleted" IS NOT NULL))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.33+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.31'''SELECT "posthog_datawarehousetable"."created_by_id","posthog_datawarehousetable"."created_at",@@ -1242,7 +1216,7 @@ORDER BY "posthog_datawarehousetable"."created_at" DESC'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.34+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.32'''SELECT "posthog_datawarehousejoin"."created_by_id","posthog_datawarehousejoin"."created_at",@@ -1262,7 +1236,7 @@AND "posthog_datawarehousejoin"."deleted" IS NOT NULL))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.35+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.33'''SELECT "posthog_team"."id","posthog_team"."uuid",@@ -1400,7 +1374,7 @@LIMIT 1'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.36+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.34'''SELECT COUNT(*)FROM@@ -1409,7 +1383,7 @@WHERE "ee_single_session_summary"."team_id" = 99999) subquery'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.37+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.35'''SELECT "posthog_user"."id","posthog_user"."password",@@ -1454,7 +1428,7 @@AND "posthog_user"."id" = 99999)'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.38+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.36'''SELECT "posthog_organization"."id","posthog_organization"."name",@@ -1496,7 +1470,7 @@LIMIT 21'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.39+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.37'''SELECT "posthog_team"."id","posthog_team"."uuid",@@ -1633,49 +1607,7 @@LIMIT 21'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.4- '''- SELECT "posthog_organization"."id",- "posthog_organization"."name",- "posthog_organization"."slug",- "posthog_organization"."logo_media_id",- "posthog_organization"."created_at",- "posthog_organization"."updated_at",- "posthog_organization"."is_active",- "posthog_organization"."is_not_active_reason",- "posthog_organization"."session_cookie_age",- "posthog_organization"."is_member_join_email_enabled",- "posthog_organization"."is_ai_data_processing_approved",- "posthog_organization"."is_ai_training_opted_in",- "posthog_organization"."is_ai_training_locked",- "posthog_organization"."is_ai_training_cta_shown",- "posthog_organization"."enforce_2fa",- "posthog_organization"."members_can_invite",- "posthog_organization"."members_can_create_projects",- "posthog_organization"."members_can_use_personal_api_keys",- "posthog_organization"."allow_publicly_shared_resources",- "posthog_organization"."default_role_id",- "posthog_organization"."plugins_access_level",- "posthog_organization"."for_internal_metrics",- "posthog_organization"."default_experiment_stats_method",- "posthog_organization"."default_anonymize_ips",- "posthog_organization"."is_hipaa",- "posthog_organization"."is_pending_deletion",- "posthog_organization"."customer_id",- "posthog_organization"."available_product_features",- "posthog_organization"."usage",- "posthog_organization"."never_drop_data",- "posthog_organization"."customer_trust_scores",- "posthog_organization"."setup_section_2_completed",- "posthog_organization"."personalization",- "posthog_organization"."domain_whitelist",- "posthog_organization"."is_platform"- FROM "posthog_organization"- WHERE "posthog_organization"."id" = '00000000-0000-0000-0000-000000000000'::uuid- LIMIT 21- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.40+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.38'''SELECT "posthog_organizationmembership"."id","posthog_organizationmembership"."organization_id",@@ -1726,7 +1658,7 @@LIMIT 21'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.41+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.39'''SELECT "ee_accesscontrol"."id","ee_accesscontrol"."team_id",@@ -1772,7 +1704,49 @@AND "ee_accesscontrol"."team_id" = 99999))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.42+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.4+ '''+ SELECT "posthog_organization"."id",+ "posthog_organization"."name",+ "posthog_organization"."slug",+ "posthog_organization"."logo_media_id",+ "posthog_organization"."created_at",+ "posthog_organization"."updated_at",+ "posthog_organization"."is_active",+ "posthog_organization"."is_not_active_reason",+ "posthog_organization"."session_cookie_age",+ "posthog_organization"."is_member_join_email_enabled",+ "posthog_organization"."is_ai_data_processing_approved",+ "posthog_organization"."is_ai_training_opted_in",+ "posthog_organization"."is_ai_training_locked",+ "posthog_organization"."is_ai_training_cta_shown",+ "posthog_organization"."enforce_2fa",+ "posthog_organization"."members_can_invite",+ "posthog_organization"."members_can_create_projects",+ "posthog_organization"."members_can_use_personal_api_keys",+ "posthog_organization"."allow_publicly_shared_resources",+ "posthog_organization"."default_role_id",+ "posthog_organization"."plugins_access_level",+ "posthog_organization"."for_internal_metrics",+ "posthog_organization"."default_experiment_stats_method",+ "posthog_organization"."default_anonymize_ips",+ "posthog_organization"."is_hipaa",+ "posthog_organization"."is_pending_deletion",+ "posthog_organization"."customer_id",+ "posthog_organization"."available_product_features",+ "posthog_organization"."usage",+ "posthog_organization"."never_drop_data",+ "posthog_organization"."customer_trust_scores",+ "posthog_organization"."setup_section_2_completed",+ "posthog_organization"."personalization",+ "posthog_organization"."domain_whitelist",+ "posthog_organization"."is_platform"+ FROM "posthog_organization"+ WHERE "posthog_organization"."id" = '00000000-0000-0000-0000-000000000000'::uuid+ LIMIT 21+ '''+# ---+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.40'''SELECT "posthog_organizationmembership"."id","posthog_organizationmembership"."organization_id",@@ -1821,18 +1795,7 @@WHERE "posthog_organizationmembership"."user_id" = 99999'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.43- '''- SELECT COUNT(*) AS "__count"- FROM "posthog_sessionrecordingplaylist"- WHERE (NOT "posthog_sessionrecordingplaylist"."deleted"- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND "posthog_sessionrecordingplaylist"."team_id" = 99999- AND NOT "posthog_sessionrecordingplaylist"."deleted"- AND NOT "posthog_sessionrecordingplaylist"."deleted")- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.44+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.41'''SELECT COUNT(*) AS "__count"FROM "posthog_sessionrecordingplaylist"@@ -1843,7 +1806,7 @@AND NOT "posthog_sessionrecordingplaylist"."deleted")'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.45+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.42'''SELECT "posthog_sessionrecordingplaylist"."id","posthog_sessionrecordingplaylist"."short_id",@@ -2053,7 +2016,7 @@LIMIT 100'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.46+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.43'''SELECT "posthog_sessionrecordingplaylistitem"."playlist_id" AS "playlist_id",COUNT("posthog_sessionrecordingplaylistitem"."id") AS "c"@@ -2070,7 +2033,7 @@GROUP BY 1'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.47+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.44'''SELECT "posthog_sessionrecordingplaylistitem"."playlist_id" AS "playlist_id","posthog_sessionrecordingplaylistitem"."recording_id" AS "recording_id"@@ -2084,7 +2047,7 @@5 /* ... */))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.48+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.45'''SELECT "posthog_sessionrecordingviewed"."session_id" AS "session_id"FROM "posthog_sessionrecordingviewed"@@ -2093,7 +2056,7 @@AND "posthog_sessionrecordingviewed"."session_id" IN ('00000000-0000-0000-0000-000000000000'))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.49+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.46'''SELECT COUNT(*) AS "__count"FROM "posthog_sessionrecordingviewed"@@ -2101,6 +2064,44 @@AND "posthog_sessionrecordingviewed"."user_id" = 99999)'''# ---+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.47+ '''+ SELECT COUNT(*)+ FROM+ (SELECT DISTINCT "posthog_comment"."item_id" AS "item_id"+ FROM "posthog_comment"+ WHERE (NOT "posthog_comment"."deleted"+ AND "posthog_comment"."scope" = 'Replay'+ AND "posthog_comment"."team_id" = 99999+ AND NOT ("posthog_comment"."item_id" IS NULL))) subquery+ '''+# ---+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.48+ '''+ SELECT COUNT(*)+ FROM+ (SELECT DISTINCT "posthog_sharingconfiguration"."recording_id" AS "recording__session_id"+ FROM "posthog_sharingconfiguration"+ WHERE ("posthog_sharingconfiguration"."enabled"+ AND "posthog_sharingconfiguration"."team_id" = 99999+ AND NOT ("posthog_sharingconfiguration"."recording_id" IS NULL))) subquery+ '''+# ---+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.49+ '''+ SELECT COUNT(*)+ FROM+ (SELECT DISTINCT ("posthog_exportedasset"."export_context" -> 'session_recording_id') AS "export_context__session_recording_id"+ FROM "posthog_exportedasset"+ WHERE (("posthog_exportedasset"."expires_after" >= '2025-01-01 12:00:00+00:00'::timestamptz+ OR "posthog_exportedasset"."expires_after" IS NULL)+ AND "posthog_exportedasset"."team_id" = 99999+ AND "posthog_exportedasset"."export_context" ? 'session_recording_id'+ AND NOT (("posthog_exportedasset"."export_context" -> 'session_recording_id') IS NULL)+ AND NOT (("posthog_exportedasset"."export_context" -> 'session_recording_id') = '""'::jsonb+ AND "posthog_exportedasset"."export_context" IS NOT NULL))) subquery+ '''+# ---# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.5'''SELECT "posthog_team"."id",@@ -2239,44 +2240,6 @@'''# ---# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.50- '''- SELECT COUNT(*)- FROM- (SELECT DISTINCT "posthog_comment"."item_id" AS "item_id"- FROM "posthog_comment"- WHERE (NOT "posthog_comment"."deleted"- AND "posthog_comment"."scope" = 'Replay'- AND "posthog_comment"."team_id" = 99999- AND NOT ("posthog_comment"."item_id" IS NULL))) subquery- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.51- '''- SELECT COUNT(*)- FROM- (SELECT DISTINCT "posthog_sharingconfiguration"."recording_id" AS "recording__session_id"- FROM "posthog_sharingconfiguration"- WHERE ("posthog_sharingconfiguration"."enabled"- AND "posthog_sharingconfiguration"."team_id" = 99999- AND NOT ("posthog_sharingconfiguration"."recording_id" IS NULL))) subquery- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.52- '''- SELECT COUNT(*)- FROM- (SELECT DISTINCT ("posthog_exportedasset"."export_context" -> 'session_recording_id') AS "export_context__session_recording_id"- FROM "posthog_exportedasset"- WHERE (("posthog_exportedasset"."expires_after" >= '2025-01-01 12:00:00+00:00'::timestamptz- OR "posthog_exportedasset"."expires_after" IS NULL)- AND "posthog_exportedasset"."team_id" = 99999- AND "posthog_exportedasset"."export_context" ? 'session_recording_id'- AND NOT (("posthog_exportedasset"."export_context" -> 'session_recording_id') IS NULL)- AND NOT (("posthog_exportedasset"."export_context" -> 'session_recording_id') = '""'::jsonb- AND "posthog_exportedasset"."export_context" IS NOT NULL))) subquery- '''-# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.53'''SELECT "posthog_datawarehousesavedquery"."created_by_id","posthog_datawarehousesavedquery"."created_at",@@ -2334,7 +2297,7 @@ORDER BY "posthog_datawarehousesavedquery"."name" ASC'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.54+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.51'''SELECT "posthog_datawarehousesavedquery"."created_by_id","posthog_datawarehousesavedquery"."created_at",@@ -2384,7 +2347,7 @@AND "posthog_datawarehousesavedquery"."deleted" IS NOT NULL))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.55+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.52'''SELECT "posthog_externaldatasource"."created_by_id","posthog_externaldatasource"."created_at",@@ -2418,7 +2381,7 @@AND "posthog_externaldatasource"."deleted" IS NOT NULL))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.56+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.53'''SELECT "posthog_datawarehousetable"."created_by_id","posthog_datawarehousetable"."created_at",@@ -2449,7 +2412,7 @@ORDER BY "posthog_datawarehousetable"."created_at" DESC'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.57+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.54'''SELECT "posthog_datawarehousejoin"."created_by_id","posthog_datawarehousejoin"."created_at",@@ -2469,7 +2432,7 @@AND "posthog_datawarehousejoin"."deleted" IS NOT NULL))'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.58+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.55'''SELECT "posthog_team"."id","posthog_team"."uuid",@@ -2607,7 +2570,7 @@LIMIT 1'''# ----# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.59+# name: TestSessionRecordingPlaylist.test_filters_playlist_by_type.56'''SELECT COUNT(*)FROMdiff --git a/posthog/session_recordings/test/test_session_recording_playlist.py b/posthog/session_recordings/test/test_session_recording_playlist.pyindex 9b76e5d086d3..5e236cd166a1 100644--- a/posthog/session_recordings/test/test_session_recording_playlist.py+++ b/posthog/session_recordings/test/test_session_recording_playlist.py@@ -7,7 +7,8 @@ from posthog.test.base import APIBaseTest, QueryMatchingTest, snapshot_postgres_from unittest import mockfrom unittest.mock import MagicMock, patch-from django.db import transaction+from django.db import connection, transaction+from django.test.utils import CaptureQueriesContextfrom boto3 import resourcefrom botocore.config import Config@@ -222,6 +223,26 @@ class TestSessionRecordingPlaylist(APIBaseTest, QueryMatchingTest):},]+ def test_list_does_not_issue_redundant_db_count(self) -> None:+ self._create_playlist({"name": "test", "type": "collection"})++ with CaptureQueriesContext(connection) as ctx:+ response = self.client.get(f"/api/projects/{self.team.id}/session_recording_playlists")++ assert response.status_code == status.HTTP_200_OK++ playlist_count_queries = [+ q+ for q in ctx.captured_queries+ if "count(" in q["sql"].lower()+ and "posthog_sessionrecordingplaylist" in q["sql"].lower()+ and "posthog_sessionrecordingplaylistitem" not in q["sql"].lower()+ ]+ assert len(playlist_count_queries) == 1, (+ f"expected a single COUNT on the playlists table, saw {len(playlist_count_queries)}: "+ f"{[q['sql'] for q in playlist_count_queries]}"+ )+@parameterized.expand([[