units
units · Unit conversion table and PL/pgSQL conversion function for length units.
Overview
| Relationships | |
|---|---|
| Requires | plpgsql |
Usage
Sources:
units is a small PL/pgSQL learning extension that stores length-conversion factors in a table and multiplies an input value by the selected factor. It is useful as a transparent example or a locally curated conversion table, not as a comprehensive units system.
Core Workflow
The implementation treats both unit arguments as PostgreSQL regular expressions. Anchor the names to request an exact pair.
Objects
units(msr, uin, niu, mop)stores the measurement family, input unit, output unit, and multiplication factor. Its primary key is(uin, niu).unitsConvert(numeric, text, text) RETURNS numericlooks up a factor withuin ~ input_patternandniu ~ output_pattern, then returns the supplied value multiplied by that factor.
The bundled data covers length conversions among kilometer, meter, centimeter, millimeter, micrometer, nanometer, mile, yard, foot, inch, and nautical mile.
Caveats
Because lookup uses regular expressions and a non-strict SELECT INTO, a broad pattern can match several rows and select an unspecified one. Use anchored patterns such as ^mile$; a missing match returns NULL. Review conversion precision and edit the table deliberately if it becomes application data.
The upstream README describes the project as a learning experiment, and version 0.0.1 provides no broader dimensional analysis, unit aliases, compound units, validation across measurement families, or documented license. Qualify the units table and unitsConvert function if the extension is installed outside the default schema.