Skip to content

Commit

Permalink
Encapsulate the TimestampTz output behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziomello committed Oct 5, 2024
1 parent 7dfe6b8 commit 61a3048
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/abi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ jobs:
# Force tzdata 2024a for PG17 because PST8PDT is broken on 2024b
# https://github.com/postgres/postgres/commit/b8ea0f67
apk list --installed tzdata | grep '2024b' > /dev/null
if [ $? -eq 0 ] && [ ${{matrix.pg}} -eq 17 ]; then
apk add lzip
cd /root
wget https://data.iana.org/time-zones/releases/tzdb-2024a.tar.lz
lzip -d -c tzdb-2024a.tar.lz | tar -xvf -
cd tzdb-2024a
make TZDIR="/usr/share/zoneinfo" install
fi
# apk list --installed tzdata | grep '2024b' > /dev/null
# if [ $? -eq 0 ] && [ ${{matrix.pg}} -eq 17 ]; then
# apk add lzip
# cd /root
# wget https://data.iana.org/time-zones/releases/tzdb-2024a.tar.lz
# lzip -d -c tzdb-2024a.tar.lz | tar -xvf -
# cd tzdb-2024a
# make TZDIR="/usr/share/zoneinfo" install
# fi
cd /mnt
cp build_abi/install_ext/* `pg_config --sharedir`/extension/
Expand Down
52 changes: 30 additions & 22 deletions tsl/src/continuous_aggs/materialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -369,23 +369,34 @@ spi_update_watermark(Hypertable *mat_ht, SchemaAndName materialization_table,
}
}

static int SavedDateStyle;
static int SavedDateOrder;

static inline void
set_date_config(int newDateStyle, int newDateOrder)
static char *
OidSafeTimestampTzOutputFunctionCall(Oid type, Datum val)
{
SavedDateStyle = DateStyle;
SavedDateOrder = DateOrder;
DateStyle = newDateStyle;
DateOrder = newDateOrder;
}
char *result;
int SavedDateStyle, SavedDateOrder;
Oid functionId;
bool type_is_varlena;

static inline void
unset_date_config()
{
DateStyle = SavedDateStyle;
DateOrder = SavedDateOrder;
/* Save DateStyle and DateOrder and force it to use ISO dates and YMD order */
if (type == TIMESTAMPTZOID)
{
SavedDateStyle = DateStyle;
SavedDateOrder = DateOrder;
DateStyle = USE_ISO_DATES;
DateOrder = DATEORDER_YMD;
}

getTypeOutputInfo(type, &functionId, &type_is_varlena);
result = OidOutputFunctionCall(functionId, val);

/* Restore previous date style and order configuration */
if (type == TIMESTAMPTZOID)
{
DateStyle = SavedDateStyle;
DateOrder = SavedDateOrder;
}

return result;
}

static void
Expand All @@ -394,18 +405,15 @@ spi_update_materializations(Hypertable *mat_ht, const ContinuousAgg *cagg,
const NameData *time_column_name, TimeRange invalidation_range,
const int32 chunk_id)
{
Oid out_fn;
bool type_is_varlena;
char *invalidation_start;
char *invalidation_end;
StringInfo chunk_condition = makeStringInfo();
uint64 rows_processed = 0;

getTypeOutputInfo(invalidation_range.type, &out_fn, &type_is_varlena);
set_date_config(USE_ISO_DATES, DATEORDER_YMD);
invalidation_start = OidOutputFunctionCall(out_fn, invalidation_range.start);
invalidation_end = OidOutputFunctionCall(out_fn, invalidation_range.end);
unset_date_config();
invalidation_start =
OidSafeTimestampTzOutputFunctionCall(invalidation_range.type, invalidation_range.start);
invalidation_end =
OidSafeTimestampTzOutputFunctionCall(invalidation_range.type, invalidation_range.end);

/* MERGE statement is available starting on PG15 and we'll support it only in the new format of
* CAggs and for non-compressed hypertables */
Expand Down
26 changes: 13 additions & 13 deletions tsl/test/expected/cagg_watermark.out
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,12 @@ SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_wa
(23 rows)

-- Update after truncate
INSERT INTO chunks VALUES ('1950-08-01 01:01:01+01', 1, 2);
INSERT INTO chunks VALUES ('1950-07-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H));
to_timestamp
------------------------------
Mon Jul 31 17:00:00 1950 PST
Fri Jun 30 18:00:00 1950 PDT
(1 row)

:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
Expand All @@ -951,29 +951,29 @@ SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_wa
Append (actual rows=1 loops=1)
-> Append (actual rows=1 loops=1)
-> Index Scan using _hyper_8_17_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_17_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> Index Scan using _hyper_8_20_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_20_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> Index Scan using _hyper_8_21_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_21_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> Index Scan using _hyper_8_23_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_23_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> Index Scan using _hyper_8_25_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_25_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> Index Scan using _hyper_8_27_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_27_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> Index Scan using _hyper_8_29_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_29_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> Index Scan using _hyper_8_31_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_31_chunk (actual rows=0 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> Index Scan using _hyper_8_33_chunk__materialized_hypertable_8_bucket_idx on _hyper_8_33_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 hour'::interval, _hyper_7_32_chunk."time"), _hyper_7_32_chunk.device
Batches: 1
-> Result (actual rows=0 loops=1)
-> Index Scan using _hyper_7_32_chunk_chunks_time_idx on _hyper_7_32_chunk (actual rows=0 loops=1)
Index Cond: ("time" >= 'Mon Jul 31 17:00:00 1950 PST'::timestamp with time zone)
Index Cond: ("time" >= 'Fri Jun 30 18:00:00 1950 PDT'::timestamp with time zone)
(26 rows)

-- Test with CAgg on CAgg
Expand Down Expand Up @@ -1008,7 +1008,7 @@ PREPARE cagg_scan_1d AS SELECT * FROM chunks_1d;
-----------------------------------------------------------------------------------------------------------------------------
Append (actual rows=1 loops=1)
-> Index Scan using _hyper_9_34_chunk__materialized_hypertable_9_bucket_idx on _hyper_9_34_chunk (actual rows=1 loops=1)
Index Cond: (bucket < 'Tue Aug 01 16:00:00 1950 PST'::timestamp with time zone)
Index Cond: (bucket < 'Sat Jul 01 17:00:00 1950 PDT'::timestamp with time zone)
-> HashAggregate (actual rows=0 loops=1)
Group Key: time_bucket('@ 1 day'::interval, (time_bucket('@ 1 hour'::interval, "time"))), device
Batches: 1
Expand Down
2 changes: 1 addition & 1 deletion tsl/test/sql/cagg_watermark.sql
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_wa
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;

-- Update after truncate
INSERT INTO chunks VALUES ('1950-08-01 01:01:01+01', 1, 2);
INSERT INTO chunks VALUES ('1950-07-01 01:01:01+01', 1, 2);
CALL refresh_continuous_aggregate('chunks_1h', '1900-01-01', '2021-06-01');
SELECT * FROM _timescaledb_functions.to_timestamp(_timescaledb_functions.cagg_watermark(:MAT_HT_ID_1H));
:EXPLAIN_ANALYZE EXECUTE ht_scan_realtime_1h;
Expand Down

0 comments on commit 61a3048

Please sign in to comment.