Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

refactor(audit-log): enhance row count retrieval and email alert handling

Victor Hugo dos Santos3a76d3e2a246
diff --git a/backend/src/ee/services/audit-log/audit-log-dal.ts b/backend/src/ee/services/audit-log/audit-log-dal.ts
index 7995bee322..332fce2455 100644
--- a/backend/src/ee/services/audit-log/audit-log-dal.ts
+++ b/backend/src/ee/services/audit-log/audit-log-dal.ts
@@ -205,18 +205,28 @@ export const auditLogDALFactory = (db: TDbClient) => {
const getApproximateRowCount: TAuditLogDALFactory["getApproximateRowCount"] = async () => {
try {
+ // Sum across parent + all partitions via pg_inherits
const result = await db.raw<{ rows: Array<{ count: string | number }> }>(
- `SELECT n_live_tup::bigint AS count FROM pg_stat_user_tables WHERE relname = ?`,
- [TableName.AuditLog]
+ `SELECT COALESCE(SUM(s.n_live_tup), 0)::bigint AS count
+ FROM pg_stat_user_tables s
+ JOIN pg_class c ON s.relname = c.relname
+ WHERE c.oid = ?::regclass
+ OR c.oid IN (SELECT inhrelid FROM pg_inherits WHERE inhparent = ?::regclass)`,
+ [TableName.AuditLog, TableName.AuditLog]
);
- if (!result.rows?.[0]?.count) {
- const fallback = await db.raw<{ rows: Array<{ count: string | number }> }>(
- `SELECT reltuples::bigint AS count FROM pg_class WHERE relname = ?`,
- [TableName.AuditLog]
- );
- return Number(fallback.rows?.[0]?.count ?? 0);
- }
- return Number(result.rows[0].count);
+
+ const count = Number(result.rows?.[0]?.count ?? 0);
+ if (count > 0) return count;
+
+ // Fallback: reltuples (handles never-analyzed tables returning -1)
+ const fallback = await db.raw<{ rows: Array<{ count: string | number }> }>(
+ `SELECT COALESCE(SUM(GREATEST(c.reltuples, 0)), 0)::bigint AS count
+ FROM pg_class c
+ WHERE c.oid = ?::regclass
+ OR c.oid IN (SELECT inhrelid FROM pg_inherits WHERE inhparent = ?::regclass)`,
+ [TableName.AuditLog, TableName.AuditLog]
+ );
+ return Number(fallback.rows?.[0]?.count ?? 0);
} catch (error) {
logger.error(error, "Failed to get approximate audit log row count");
return 0;
diff --git a/backend/src/ee/services/audit-log/audit-log-service.ts b/backend/src/ee/services/audit-log/audit-log-service.ts
index dd2c5eabff..724f618829 100644
--- a/backend/src/ee/services/audit-log/audit-log-service.ts
+++ b/backend/src/ee/services/audit-log/audit-log-service.ts
@@ -184,23 +184,29 @@ export const auditLogServiceFactory = ({
adminsOnly: true
});
- if (superAdminsResult.users.length === 0) return;
+ const adminsWithEmail = superAdminsResult.users.filter((admin): admin is TUsers & { email: string } =>
+ Boolean(admin.email)
+ );
+ if (adminsWithEmail.length === 0) return;
- await Promise.all(
- superAdminsResult.users.map((admin: TUsers) =>
+ const emailResults = await Promise.allSettled(
+ adminsWithEmail.map((admin) =>
smtpService.sendMail({
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
template: SmtpTemplates.AuditLogMigrationAlert,
subjectLine: "Action recommended: Audit log storage is large",
- recipients: [admin.email!],
+ recipients: [admin.email],
substitutions: {
siteUrl: appCfg.SITE_URL
}
})
)
- ).catch((error) => {
- logger.error(error, "Failed to send audit log migration alert emails");
- });
+ );
+
+ const failedEmails = emailResults.filter((r) => r.status === "rejected");
+ if (failedEmails.length > 0) {
+ logger.error({ failedCount: failedEmails.length }, "Failed to send some audit log migration alert emails");
+ }
await notificationService
.createUserNotifications(
diff --git a/docs/documentation/platform/audit-logs-clickhouse-setup.mdx b/docs/documentation/platform/audit-logs-clickhouse-setup.mdx
index 371788ad16..9978924a8f 100644
--- a/docs/documentation/platform/audit-logs-clickhouse-setup.mdx
+++ b/docs/documentation/platform/audit-logs-clickhouse-setup.mdx
@@ -35,6 +35,9 @@ CLICKHOUSE_AUDIT_LOG_ENABLED=true
# Optional: Enable reading audit logs from ClickHouse instead of Postgres (default: false)
CLICKHOUSE_AUDIT_LOG_QUERY_ENABLED=false
+
+# Optional: Disable audit log writes to PostgreSQL (default: false)
+DISABLE_AUDIT_LOG_STORAGE=false
```
### Connection URL Format