Skip to content

: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.

fix(session-replay): expire every export of a deleted recording (#101932)

Tue Haulunde9b2190c9287
diff --git a/nodejs/src/session-replay/recording-api/recording-cleanup.integration.test.ts b/nodejs/src/session-replay/recording-api/recording-cleanup.integration.test.ts
index e8b2a3a4fb36..a4c560ce14f5 100644
--- a/nodejs/src/session-replay/recording-api/recording-cleanup.integration.test.ts
+++ b/nodejs/src/session-replay/recording-api/recording-cleanup.integration.test.ts
@@ -112,14 +112,23 @@ describe('recording cleanup (integration)', () => {
[randomUUID(), alertId, targetObservationId, teamId],
'fixtureMatch'
)
- await postgres.query(
- PostgresUse.COMMON_WRITE,
- `INSERT INTO posthog_exportedasset
- (team_id, export_format, created_at, export_context, is_system, expires_after)
- VALUES ($1, 'video/mp4', now(), $2::jsonb, true, now() + interval '30 days')`,
- [teamId, JSON.stringify({ session_recording_id: 'session-to-delete' })],
- 'fixtureAsset'
- )
+ // Of the deleted recording: a system render, a person's own gif export, and an event screenshot frame.
+ // Of a kept recording: one export, which must stay.
+ for (const [format, isSystem, sessionId] of [
+ ['video/mp4', true, 'session-to-delete'],
+ ['image/gif', false, 'session-to-delete'],
+ ['image/png', true, 'session-to-delete'],
+ ['video/mp4', false, 'session-to-keep'],
+ ] as const) {
+ await postgres.query(
+ PostgresUse.COMMON_WRITE,
+ `INSERT INTO posthog_exportedasset
+ (team_id, export_format, created_at, export_context, is_system, expires_after)
+ VALUES ($1, $2, now(), $3::jsonb, $4, now() + interval '30 days')`,
+ [teamId, format, JSON.stringify({ session_recording_id: sessionId }), isSystem],
+ 'fixtureAsset'
+ )
+ }
})
afterEach(async () => {
@@ -184,15 +193,16 @@ describe('recording cleanup (integration)', () => {
expect(await queued(otherObservationId)).toBe(0)
})
- it('expires the rendered video for the deleted recording', async () => {
+ it('expires every export of the deleted recording and none of a kept one', async () => {
await service.deleteRecordings(['session-to-delete'], teamId, '[email protected]')
- expect(
- await count(
+ const live = async (sessionId: string): Promise<number> =>
+ count(
`SELECT count(*) FROM posthog_exportedasset
- WHERE team_id = $1 AND expires_after > now()`,
- [teamId]
+ WHERE team_id = $1 AND expires_after > now() AND export_context ->> 'session_recording_id' = $2`,
+ [teamId, sessionId]
)
- ).toBe(0)
+ expect(await live('session-to-delete')).toBe(0)
+ expect(await live('session-to-keep')).toBe(1)
})
})
diff --git a/nodejs/src/session-replay/recording-api/recording-service.test.ts b/nodejs/src/session-replay/recording-api/recording-service.test.ts
index 800f9e7fb6cc..a7ac43623ee1 100644
--- a/nodejs/src/session-replay/recording-api/recording-service.test.ts
+++ b/nodejs/src/session-replay/recording-api/recording-service.test.ts
@@ -522,7 +522,7 @@ describe('RecordingService', () => {
expect.anything(),
expect.stringContaining('posthog_exportedasset'),
[1, ['"session-1"', '"session-2"']],
- 'expireRenderedRecordingVideos'
+ 'expireRecordingExports'
)
})
diff --git a/nodejs/src/session-replay/recording-api/recording-service.ts b/nodejs/src/session-replay/recording-api/recording-service.ts
index 28ced0a26dee..6ebdd9397951 100644
--- a/nodejs/src/session-replay/recording-api/recording-service.ts
+++ b/nodejs/src/session-replay/recording-api/recording-service.ts
@@ -463,16 +463,14 @@ export class RecordingService {
// Expired, not deleted: the row is the only pointer to the stored object, which the expiry sweep needs.
this.postgres.query(
PostgresUse.COMMON_WRITE,
- // `= ANY` over jsonb so the planner can use exportedasset_system_session; a subquery cannot.
+ // `= ANY` over jsonb so the planner can use exportedasset_session; a subquery cannot.
`UPDATE posthog_exportedasset
SET expires_after = now()
WHERE team_id = $1
- AND is_system
- AND export_format = 'video/mp4'
AND expires_after > now()
AND export_context -> 'session_recording_id' = ANY($2::jsonb[])`,
[teamId, sessionIds.map((sessionId) => JSON.stringify(sessionId))],
- 'expireRenderedRecordingVideos'
+ 'expireRecordingExports'
),
])