damm
damm · SQL implementation of the Damm check digit algorithm with a damm_code domain and helper functions.
Overview
Usage
Sources:
damm implements the Damm decimal check-digit algorithm in pure SQL. It can generate human-visible identifiers that detect every single-digit error and adjacent digit transposition, validate existing codes, or derive sequential checked IDs from a PostgreSQL sequence.
Core Workflow
Create a sequence and use nextdamm as the default for the damm_code domain:
generate_damm appends one decimal check digit. valid_damm_code recomputes that digit and returns a boolean.
API
damm_check_digit(bigint): computes the check digit without appending it.generate_damm(bigint): returns the input multiplied by ten plus its check digit.valid_damm_code(bigint): validates the final digit.damm_code:bigintdomain constrained byvalid_damm_code.nextdamm(varchar): callsnextvalon the named sequence and returns a checked value.
Caveats
A Damm digit detects common transcription errors; it is not a cryptographic checksum, uniqueness mechanism, or authorization control. The sequence supplies uniqueness, while the domain only validates the decimal representation.
The extension owns a lookup table and is not dynamically relocatable after installation, although it can be installed into a chosen schema. Schema-qualify the domain/functions when they are outside the application’s search path, and test upper bounds because appending a digit can overflow bigint for sufficiently large inputs.