This report documents the final database design used in the Campus Resource Management and Booking System (CRMBS), including schema design choices, normalization, indexing, core query patterns, and conflict resolution strategy.
- Engine: PostgreSQL 16+
- Design goals:
- strict overlap prevention for booking slots
- role-aware approvals
- auditability and traceability
- efficient filtered reads for role dashboards
Primary transactional and master tables:
departmentusersresource_categoryresourcebooking_status(lookup)priority(lookup)bookingapprovalnotificationblackout_periodresource_unavailabilityaudit_loglog
users.department_id -> department.department_iddepartment.hod_id -> users.user_idresource.category_id -> resource_category.category_idresource.manager_id -> users.user_idresource.department_id -> department.department_idbooking.user_id -> users.user_idbooking.resource_id -> resource.resource_idbooking.status_id -> booking_status.status_idapproval.booking_id -> booking.booking_idapproval.approver_id -> users.user_id
The schema follows practical 3NF patterns:
- Status and priority vocabularies are normalized into lookup tables (
booking_status,priority). - Department data is separated from user/resource entities.
- Resource category policy (
advance_days, defaultapproval_steps) is centralized inresource_category. - Booking decisions are normalized into
approvalstep records instead of denormalized approval columns inbooking.
This reduces duplication and keeps updates consistent.
Approval behavior uses both category defaults and per-resource override:
resource_category.approval_stepsis system defaultresource.approval_steps_overrideallows resource-specific policy:0= auto-approve1= RM only2= RM + HOD
Effective steps = COALESCE(resource.approval_steps_override, resource_category.approval_steps).
Implemented indexes:
idx_resource_categoryonresource(category_id)idx_resource_featuresGIN onresource(features)idx_booking_resource_timeonbooking(resource_id, date, start_time, end_time)idx_booking_useronbooking(user_id)idx_booking_statusonbooking(status_id)idx_mv_utilunique index on materialized viewmv_resource_utilization(resource_id, week_start)
Why these matter:
- fast resource filtering by category/features
- fast conflict/availability scans by resource and slot
- fast role dashboards by user and status
Conflict safety is implemented in three layers:
create_booking(...) enforces:
- start/end time ordering
- advance window (
advance_days) - blackout period block
- recurring maintenance/unavailability block
- advisory transaction lock on resource (
pg_advisory_xact_lock(resource_id)) - overlap check against active slot-blocking bookings
booking.is_slot_blockingmarks whether the row should participate in overlap lock.- Trigger
trg_set_booking_blocking_flagupdates this on insert/update based on status. Pending/Approved=> slot-blocking true.
no_overlap constraint:
EXCLUDE USING gist (
resource_id WITH =,
tsrange(date + start_time, date + end_time) WITH &&
) WHERE (is_slot_blocking)This guarantees no overlapping active slots even if application logic is bypassed.
Booking rejection paths in create_booking include:
- category blackout period (
blackout_period) - per-resource recurring unavailability (
resource_unavailability) - inactive/maintenance resource state check
This ensures policy constraints are enforced before any booking row is inserted.
Audit design:
- Trigger function
fn_audit_trigger()writes old/new row snapshots forusers,resource, andbooking. - Rules
no_update_auditandno_delete_auditblock mutation/deletion of audit entries.
Result: append-only audit trail semantics.
Materialized view: mv_resource_utilization
- Pre-aggregates weekly bookings and booked hours by resource/category.
- Improves role analytics endpoint performance.
- lookup by email and
firebase_uid - role/status enrichment with department join
- dynamic filtered user queries (
role,department,is_active, free text) - system-wide booking filters (
status,user,resource, date range) - department CRUD and blackout CRUD
- department-scoped users
- step-2 approvals scoped by resource department
- department-scoped bookings/analytics
- manager-owned resource listing and updates
- step-1 approvals scoped by managed resources
- recurring unavailability management
- resource search + availability views
- create booking via stored function
- booking detail + approval trail + cancel rules
Key checks and controls:
- role enum check in
users.role - resource status enum check in
resource.status - booking time order check (
start_time < end_time) - approval decision enum check
- blackout start/end date check
- unique email and unique firebase UID
Verified in final checks:
- student-student and faculty-student conflicts return
409 - pending requests correctly block new requests in same slot
- cancellation works under policy rules
- resource approval can be configured per resource (0/1/2)
- multi-department HOD/RM flows work (CSE/ECE/MECH)
- inactive resources are visible and non-bookable
- blackout periods correctly reject booking requests
- audit logging remains append-only
- scheduled materialized view refresh job
- query plan profiling for additional composite indexes
- optional partitioning for high-volume booking history
- optional row-level security per role at DB level