Skip to main content

Database hr_ai — AI Assistant

Reduced PostgreSQL database, populated nightly from the production HR database, designed to answer user questions via an AI assistant.

Architecture

Production DB (hr_prod / Italian schema)

│ 02_sync_daily.sql
│ (nightly at 02:00 via pg_cron)

AI Assistant DB (hr_ai / English schema)

└─► AI Assistant (read-only)

Scripts are located in the repository under scripts/ai-assistant/.

Differences from the source database

AspectProduction DBhr_ai
colcustom columnsPresent (internal JSON)Removed
_deleted columnsPresentRemoved (sync imports only active records)
Number of tables~34822
External foreign keysCompleteOnly towards included tables
AccessRead/WriteRead-only recommended

Included tables

Schema cloud

TableItalian originalDescription
cloud.tenantscloud._tenantTenant/organizations in the multi-tenant system

Schema hr

TableItalian originalDescription
hr.collective_agreementshr.contrattiNational collective labor agreements (CCNL)
hr.companieshr.aziendeCompanies managed in the HR system
hr.customershr.in_clientiCustomers/clients
hr.documentshr.in_documentsDocuments and requests (leave, permits…)
hr.element_group_typeshr.gruppi_tipiElement group type definitions
hr.element_groupshr.gruppi_elementiElement groups (hierarchical)
hr.employment_relationshipshr.rapportiEmployment relationships person↔company
hr.employment_relationship_detailshr.rapporti_detTime-bound contractual details
hr.employment_relationship_deadlines(no translation available)Employment relationship due dates
hr.customer_locationshr.in_clienti_sediCustomer locations/branches
hr.document_logshr.in_documents_logDocument audit log
hr.person_citizenshiphr.ana_cittadinanzaPerson citizenship history
hr.person_group_assignmentshr.ana_gruppiPerson-to-group assignments
hr.person_registryhr.anagraficoPerson master data (employees)
hr.work_siteshr.sedi_operativeOperational sites with geofencing data
hr.x_employment_agreement_typeshr.x_tipi_rapportoEmployment agreement type lookup
hr.x_hiring_categorieshr.x_tipi_categorie_assunzioneHiring category lookup

Schema pres

TableItalian originalDescription
pres.daily_attendance_detailspres.cartellinoDaily timesheet (clockings, hours)
pres.daily_attendance_justificationspres.giust_giornoDaily absence/leave justifications
pres.monthly_attendance_summariespres.cart_meseMonthly attendance totals
pres.monthly_justification_aggregatespres.giust_meseMonthly aggregated justifications

Scripts

FilePurpose
scripts/ai-assistant/01_create_schema.sqlDDL — creates schemas, tables and indexes
scripts/ai-assistant/02_sync_daily.sqlhr_ai_sync() procedure — daily sync via dblink

Initial setup

# 1. Create the database
createdb -U postgres hr_ai

# 2. Create schemas and tables
psql -U postgres -d hr_ai -f scripts/ai-assistant/01_create_schema.sql

# 3. Install the sync procedure
psql -U postgres -d hr_ai -f scripts/ai-assistant/02_sync_daily.sql

# 4. Run the first manual sync
psql -U postgres -d hr_ai -c \
"CALL hr_ai_sync('host=<src-host> dbname=<ita-db> user=<user> password=<pwd>');"

Automated sync (pg_cron)

-- Schedule nightly sync at 02:00
SELECT cron.schedule(
'sync-hr-ai-nightly',
'0 2 * * *',
$$CALL hr_ai_sync('host=<src-host> dbname=<ita-db> user=<user> password=<pwd>')$$
);

Important notes

  • The hr.customer_locations table was previously hr.in_clienti_sedi in the Italian source database.
  • Generated daterange columns (e.g. er_period, ca_period) are kept to enable efficient range queries for the AI assistant.
  • Foreign keys towards tables not included in this DB (e.g. pres.attendance_reasons, hr.document_models) have been removed to avoid integrity errors during sync.
  • The hr_ai database should be accessible in read-only mode by the AI assistant (revoke INSERT/UPDATE/DELETE privileges on the AI user).