El tipo temporal tjsonb permite representar la evolución en el tiempo de valores jsonb. Como todos los tipos temporales, viene en tres subtipos, a saber, instante, secuencia y conjunto de secuencias. A continuación se dan ejemplos de valores tjsonb en estos subtipos.
-- Instant
SELECT tjsonb '"{\"vehicleId\": 1, \"location\": \"Point(1 1)\"}"@2001-01-01';
-- Sequence with discrete interpolation
SELECT tjsonb '{{"vehicleId": 1, "location": "Point(1 1)"}@2001-01-01,
{"vehicleId": 1, "location": "Point(2 2)"}@2001-01-02}';
-- Sequence with step interpolation
SELECT tjsonb '[{"vehicleId": 1, "location": "Point(1 1)"}@2001-01-01,
{"vehicleId": 1, "location": "Point(2 2)"}@2001-01-02]';
-- Sequence set
SELECT tjsonb '{[{"vehicleId": 1, "location": "Point(1 1)"}@2001-01-01,
{"vehicleId": 1, "location": "Point(2 2)"}@2001-01-02],
[{"vehicleId": 1, "location": "Point(2 2)"}@2001-01-03,
{"vehicleId": 1, "location": "Point(3 3)"}@2001-01-04]}';
Como puede verse arriba, para el subtipo Instant necesitamos encerrar el valor JSONB entre comillas y escapar las comillas internas; de lo contrario, la llave de apertura del valor JSONB se interpretaría como el comienzo de un subtipo Sequence con interpolación discreta.
El tipo tjsonb acepta modificadores de tipo (o typmod en la terminología de PostgreSQL) para especificar el subtipo. Los valores posibles para el subtipo son Instant, Sequence y SequenceSet. El argumento es opcional y, si no se especifica para una columna, se permiten valores de cualquier subtipo.
SELECT tjsonb(Instant) '"{\"vehicleId\": 1, \"location\": \"Point(1 1)\"}"@2001-01-01';
-- {"location": "Point(1 1)", "vehicleId": 1}@2001-01-01
SELECT tjsonb(Sequence) '"{\"vehicleId\": 1, \"location\": \"Point(1 1)\"}"@2001-01-01';
-- ERROR: Temporal type (Instant) does not match column type (Sequence)
Los valores JSONB temporales de subtipo secuencia o conjunto de secuencias se convierten a una forma normal de modo que valores equivalentes tengan representaciones idénticas. Para ello, los valores de instantes consecutivos se fusionan cuando es posible. Tres valores de instantes consecutivos pueden fusionarse en dos si sus valores JSONB son iguales. Asimismo, dos secuencias consecutivas que son adyacentes y tienen el mismo valor JSONB de final e inicio pueden fusionarse en una sola. A continuación se dan ejemplos de transformación a una forma normal.
SELECT asText(tjsonb '[{"vehicleId": 1, "location": "Point(1 1)"}@2001-01-01,
{"vehicleId": 1, "location": "Point(1 1)"}@2001-01-02,
{"vehicleId": 1, "location": "Point(1 1)"}@2001-01-03]');
/* [{"location": "Point(1 1)", "vehicleId": 1}@2001-01-01,
{"location": "Point(1 1)", "vehicleId": 1}@2001-01-03] */
SELECT asText(tjsonb '{[{"vehicleId": 1, "location": "Point(1 1)"}@2001-01-01,
{"vehicleId": 1, "location": "Point(2 2)"}@2001-01-03],
({"vehicleId": 1, "location": "Point(2 2)"}@2001-01-03,
{"vehicleId": 1, "location": "Point(2 2)"}@2001-01-04]}');
/* {[{"location": "Point(1 1)", "vehicleId": 1}@2001-01-01,
{"location": "Point(2 2)", "vehicleId": 1}@2001-01-02,
{"location": "Point(2 2)", "vehicleId": 1}@2001-01-04]} */