054_ops_system_logs.sql 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. -- 054_ops_system_logs.sql
  2. -- 统一日志索引表与清理审计表
  3. CREATE TABLE IF NOT EXISTS ops_system_logs (
  4. id BIGSERIAL PRIMARY KEY,
  5. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  6. level VARCHAR(16) NOT NULL,
  7. component VARCHAR(128) NOT NULL DEFAULT '',
  8. message TEXT NOT NULL,
  9. request_id VARCHAR(128),
  10. client_request_id VARCHAR(128),
  11. user_id BIGINT,
  12. account_id BIGINT,
  13. platform VARCHAR(32),
  14. model VARCHAR(128),
  15. extra JSONB NOT NULL DEFAULT '{}'::jsonb
  16. );
  17. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_created_at_id
  18. ON ops_system_logs (created_at DESC, id DESC);
  19. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_level_created_at
  20. ON ops_system_logs (level, created_at DESC);
  21. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_component_created_at
  22. ON ops_system_logs (component, created_at DESC);
  23. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_request_id
  24. ON ops_system_logs (request_id);
  25. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_client_request_id
  26. ON ops_system_logs (client_request_id);
  27. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_user_id_created_at
  28. ON ops_system_logs (user_id, created_at DESC);
  29. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_account_id_created_at
  30. ON ops_system_logs (account_id, created_at DESC);
  31. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_platform_model_created_at
  32. ON ops_system_logs (platform, model, created_at DESC);
  33. CREATE INDEX IF NOT EXISTS idx_ops_system_logs_message_search
  34. ON ops_system_logs USING GIN (to_tsvector('simple', COALESCE(message, '')));
  35. CREATE TABLE IF NOT EXISTS ops_system_log_cleanup_audits (
  36. id BIGSERIAL PRIMARY KEY,
  37. created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  38. operator_id BIGINT NOT NULL,
  39. conditions JSONB NOT NULL DEFAULT '{}'::jsonb,
  40. deleted_rows BIGINT NOT NULL DEFAULT 0
  41. );
  42. CREATE INDEX IF NOT EXISTS idx_ops_system_log_cleanup_audits_created_at
  43. ON ops_system_log_cleanup_audits (created_at DESC, id DESC);