From 61a3048fa8a2db456a81e6d4f688a47cff33e356 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabr=C3=ADzio=20de=20Royes=20Mello?= Date: Fri, 4 Oct 2024 17:42:33 -0300 Subject: [PATCH] Encapsulate the TimestampTz output behavior --- .github/workflows/abi.yaml | 18 +++++----- tsl/src/continuous_aggs/materialize.c | 52 +++++++++++++++------------ tsl/test/expected/cagg_watermark.out | 26 +++++++------- tsl/test/sql/cagg_watermark.sql | 2 +- 4 files changed, 53 insertions(+), 45 deletions(-) diff --git a/.github/workflows/abi.yaml b/.github/workflows/abi.yaml index f83f3d9f127..06cb8091e7d 100644 --- a/.github/workflows/abi.yaml +++ b/.github/workflows/abi.yaml @@ -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/ diff --git a/tsl/src/continuous_aggs/materialize.c b/tsl/src/continuous_aggs/materialize.c index 1f68aff453d..b1cd3c8c22c 100644 --- a/tsl/src/continuous_aggs/materialize.c +++ b/tsl/src/continuous_aggs/materialize.c @@ -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 @@ -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 */ diff --git a/tsl/test/expected/cagg_watermark.out b/tsl/test/expected/cagg_watermark.out index d40601bef29..f05d125b3d9 100644 --- a/tsl/test/expected/cagg_watermark.out +++ b/tsl/test/expected/cagg_watermark.out @@ -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; @@ -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 @@ -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 diff --git a/tsl/test/sql/cagg_watermark.sql b/tsl/test/sql/cagg_watermark.sql index 524c2958f42..e88297353c9 100644 --- a/tsl/test/sql/cagg_watermark.sql +++ b/tsl/test/sql/cagg_watermark.sql @@ -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;