pg_vartype
pg_vartype · Multiple scalar types in a single column
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 100661 | pg_vartype | pg_vartype | 0.4 | TYPE | Unknown | C |
Usage
Sources:
pg_vartype provides a vartype column type that can hold integer, floating-point, Boolean, string, date, or timestamp values. It predates PostgreSQL jsonb; upstream notes that native JSON storage is a better fit for many current applications.
Core Workflow
When using an untyped literal, a string must contain double quotes, for example '"blue"'::vartype; otherwise a token such as 'false'::vartype is parsed as a Boolean. Casting from text is less ambiguous for application parameters.
Types, Casts, and Operators
vartype_typeis an enum describing the stored category; thevartype_type(vartype)andvartype_len(vartype)helpers expose category and stored length.- Explicit casts cover common integer and floating types,
boolean,text/character types,date,timestamp, andtimestamptzin both directions where defined. - Comparison operators and the default
vartype_opsB-tree operator class allow ordering and indexing.
Values from different category groups are ordered by the extension’s internal precedence. Integer and floating values are intended to compare numerically, and date/timestamp values are compared through conversion; these rules are extension-specific rather than PostgreSQL’s ordinary single-type semantics.
Critical Comparison Caveat
The reviewed 0.4 C comparator handles mixed numeric operands asymmetrically: one integer-versus-float branch converts the float to an integer, while the reverse branch compares as floating point. Fractional values can therefore produce inconsistent comparison results and may violate the ordering contract required by vartype_ops.
Do not use mixed integer/float vartype values in B-tree indexes, unique constraints, ordered aggregates, or range predicates until this behavior is fixed or conclusively reproduced as safe on the chosen build. Keeping indexed values within one category avoids that particular path but does not replace validation.
The extension is non-relocatable and the reviewed codebase was last changed in 2017, with documentation centered on PostgreSQL 9.6. Test input parsing, casts, time-zone behavior, comparison laws, dump/restore, and the exact PostgreSQL version before production use.