Skip to content

v2.4.0

Choose a tag to compare

@DanielArndt0 DanielArndt0 released this 30 May 19:38
· 6 commits to main since this release
715d35c

Hybrid PostgreSQL Import and Data Preparation Improvements

This release introduces a new hybrid PostgreSQL direct import workflow and improves the preparation of RFB datasets for large-scale database loading.

The CNPJ DB Loader can now handle extraction, validation and sanitization while PostgreSQL performs the heavier loading and materialization steps directly from sanitized source files.

Hybrid PostgreSQL direct import

  • Added postgres generate-script support for generating PostgreSQL direct import scripts.
  • Added direct psql-based loading from sanitized Receita Federal files.
  • Added PostgreSQL-side raw loading, staging and materialization.
  • Added generation of establishment_secondary_cnaes.
  • Added modular SQL scripts by phase.
  • Added import-postgres-direct.sql as the orchestration script.
  • Added manifest metadata with generated steps and dependencies.

Transaction modes

Added configurable transaction strategies:

  • single: uses one transaction for the entire import. It is the safest option because everything is committed only at the end, but any failure requires restarting the complete run.
  • phase: uses one transaction per generated phase. It is the recommended option for large imports because completed phases remain committed and failures are easier to isolate and rerun.
  • none: does not wrap generated scripts in explicit transactions. It can be faster for local benchmarks or disposable databases, but failures may leave partially imported data.

Selective script generation

  • Added --include support for generating only selected phases.
  • Added --skip-indexes support.
  • Added --skip-analyze support.
  • Added clearer \echo messages for long-running PostgreSQL operations.
  • Added PostgreSQL monitoring documentation and ready-to-use SQL snippets.

Hybrid quarantine handling

  • Added quarantine handling during hybrid staging validation.
  • Reused the existing import_quarantine table and operational model.
  • Preserved compatibility with the Node-driven import flow.
  • Added summarized logs and checkpoints for quarantined records.

Technical limitation

Because hybrid validation happens after the raw COPY step, the exact original byte offset may not always be available. In these cases, checkpoint_offset can remain NULL, and raw_line is stored as a textual JSON representation of the raw temporary row.

Federal Revenue configuration

  • Added federal-revenue config set.
  • Added federal-revenue config show.
  • Added federal-revenue config test.
  • Added federal-revenue config reset.
  • Added support for configuring share-token, webdav-url and user-agent.

Large ZIP and ZIP64 extraction

  • Added support for large ZIP and ZIP64 archives.
  • Added support for split ZIP volumes such as .zip.001.
  • Added automatic detection of supported split archive entry points.
  • Added safer extraction using temporary directories before replacing the final destination.
  • Fixed extraction failures where valid large archives could be incorrectly reported as unsupported multi-disk ZIP files.

Sanitization encoding improvements

  • Added streaming normalization from ISO-8859-1 / LATIN1 to UTF-8.
  • Added byte-level removal of NUL characters.
  • Added detection of Unicode replacement characters (�).
  • Added temporary output files and safe replacement after validation.
  • Added optional --allow-replacement-chars support for controlled manual inspection.
  • Preserved the existing sanitization command behavior.

Recommended flow

Download the current Receita Federal reference:

cnpj-db-loader federal-revenue download --current --output C:\ReceitaDB

Then use the generated reference directory in the next steps:

cnpj-db-loader extract C:\ReceitaDB\2026-05

cnpj-db-loader validate C:\ReceitaDB\2026-05\extracted

cnpj-db-loader sanitize C:\ReceitaDB\2026-05\extracted --output C:\ReceitaDB\2026-05\sanitized --force

cnpj-db-loader postgres generate-script C:\ReceitaDB\2026-05\sanitized --output C:\ReceitaDB\2026-05\postgres-direct --source-encoding UTF8 --transaction-mode phase --force

psql -d "postgres://postgres:postgres@localhost:5432/cnpj" -f "C:\ReceitaDB\2026-05\postgres-direct\import-postgres-direct.sql"

The --current flag automatically selects the current RFB reference during download.

Notes

For large imports, phase is the recommended transaction mode because it provides a practical balance between safety and recoverability.