feat(db): add postgres audit helpers#231
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a small, Postgres-feature-gated “audit” module to the crate to help standardize Postgres connection application naming and to log whether pgAudit-related settings appear to be enabled.
Changes:
- Introduces
src/audit.rswith helpers to setapplication_nameand to log pgAudit / connection logging configuration detected via SQL. - Exposes the new module and re-exports its public helpers from
src/lib.rsbehind thepostgresfeature.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/lib.rs | Feature-gated module + re-exports for new Postgres audit helpers. |
| src/audit.rs | New helpers for setting Postgres application_name and auditing/logging pgAudit-related settings. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let database_name: String = match sqlx::query_scalar("SELECT current_database()") | ||
| .fetch_one(pool) | ||
| .await | ||
| { | ||
| Ok(name) => name, | ||
| Err(error) => { | ||
| tracing::warn!(error = %error, "Failed to inspect PostgreSQL audit settings"); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| let extension_installed: bool = match sqlx::query_scalar( | ||
| "SELECT EXISTS (SELECT 1 FROM pg_extension WHERE extname = 'pgaudit')", | ||
| ) | ||
| .fetch_one(pool) | ||
| .await | ||
| { | ||
| Ok(installed) => installed, | ||
| Err(error) => { | ||
| tracing::warn!( | ||
| database = %database_name, | ||
| error = %error, | ||
| "Failed to check whether pgAudit is installed" | ||
| ); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| let shared_preload_libraries: Option<String> = | ||
| match sqlx::query_scalar("SELECT current_setting('shared_preload_libraries', true)") | ||
| .fetch_one(pool) | ||
| .await | ||
| { |
There was a problem hiding this comment.
log_postgres_audit_status performs multiple round trips to the database sequentially (current_database, pg_extension check, and four current_setting reads). This increases startup latency and load; consider combining these into a single SELECT (or at least running independent reads concurrently) and then evaluating the results locally.
| use sqlx::{PgPool, postgres::PgConnectOptions}; | ||
|
|
||
| pub const POSTGRES_APPLICATION_NAME: &str = "fusillade"; | ||
|
|
There was a problem hiding this comment.
with_application_name only has an effect via its return value; if a caller accidentally ignores the returned PgConnectOptions, the application name won't be set and the compiler won't warn. Consider adding #[must_use] to this helper (or changing the signature to take &mut PgConnectOptions and mutate in place) so misuse is caught early.
| #[must_use] |
No description provided.