pgbrtypes
pgbrtypes · Brazilian CPF and CNPJ data types with validation, formatting, casts, and indexing support
Overview
PostgreSQL · 10, 11, 12, 13, 14, 15, 16, 17, 18
Usage
Sources:
pgbrtypes adds validated cpf and cnpj types for Brazilian individual and company taxpayer registration numbers. It normalizes formatted or unformatted input, preserves leading zeros in the typed value, and can present a conventional display mask.
Core Workflow
Invalid document numbers are rejected by the type input functions. Use text input when leading zeros matter; integer literals cannot represent the original textual width even though implicit integer conversions are available.
Types and Helpers
cpfandcnpjare fixed-length eight-byte types with binary send/receive functions.is_cpf(text)andis_cnpj(text)validate candidate values without storing them; overloads also accept bigint and C-string inputs.- Comparison operators and the
cpf_op_classandcnpj_op_classB-tree operator classes support equality/range predicates and indexes. - Implicit casts accept integer/bigint input, while assignment casts convert typed values back to bigint.
Validation checks the identifier’s syntactic/check-digit rules; it does not prove that a person or company exists or owns the number.
Output Formatting
The user-settable GUCs change display only, not stored identity or comparison behavior:
Because text output changes by session, integrations should choose a stable setting or cast to bigint deliberately. Treat CPF/CNPJ values as sensitive identifiers and apply access control, logging redaction, and retention policies independently of formatting.
Compatibility
The README requires PostgreSQL 9.1 or later but reports testing only on 9.6; the reviewed source was last changed in 2017 and does not document Windows support. Build and test the exact PostgreSQL/OS target, including indexes, binary dump/restore, upgrade, malformed input, and locale-independent output, before production use.