Time in a replicated environment
The replication logic relies on time consistency between all NPS and replication queue manager nodes. In a replicated environment where NPS systems are in different time zones, you must synchronize operations by using a time synchronization service, such as Network Time Protocol (NTP). For more information, see Node side network configuration in the Cloud Pak for Data documentation.
Time and NPS
Time on the NPS node is based on the NPS host system time. The query execution environment uses this time. By default, the time zone is considered UNKNOWN. However, you can override the default by specifying time zone information for the TIMEZONE session variable.
- Netezza® temporal data types (DATE, TIME, and TIMESTAMP) store date, time, and timestamp values that do not reflect the time zone in which they were generated.
- Any legal date, time, and timestamp literals from an SQL session are accepted.
- The CURRENT_DATE, CURRENT_TIME, and CURRENT_TIMESTAMP (also NOW) SQL functions generate date, time, and timestamp values that reflect the time in the current time zone. The current time zone, by default, is that of the NPS server host. You can override this time zone in an SQL session by using the SET TIME ZONE command or equivalently, the SET TIMEZONE command. It affects the values that are generated when that session calls the three functions. Even on a single Netezza host, you have no guarantee that date, time, or timestamp values that are inserted from different client sessions (or the same session, with different SET TIME ZONE command settings) have any common timestamp reference or time zone.
- The application must manage whether the time zone interpretation is relative to date, time, and time values in table columns.
- Some built-in tools for handling time and time zone information include the TIMETZ data type (an alias for the TIME WITH TIME ZONE data type) and the TIMEZONE() function. TIMETZ is a slightly different temporal data type in that values carry a time zone offset, which you can specify in TIMETZ literals (for example, '01:23:45-06'). If a time-like literal without the time zone offset is inserted into a TIMETZ column, Replication Services uses '+00' (GMT).
INSERT INTO ordertable values (1, current_timestamp, 'Product A'); The row
in the table on the primary NPS node would appear as
follows:
ORDERNUMBER | ORDERTIMESTAMP | ORDERDETAILS
------------+---------------------+--------------
1 | 2012-01-19 09:12:06 | Product AThis row is also replicated to the replica NPS node and has the same values. The values are accurate; however, as shown in the example, there is no information to identify which time zone this information is relative to. At the time of the example, New York is on standard time (EST), and the time difference between there and London is 5 hours (-05). Because the row is replicated at approximately the same time that it was inserted, the time is registered on the replica NPS node as 2:12 PM local time (GMT).
Workarounds for time zone differences
- Make all time relative to a common base (for example, UTC or GMT).
- Capture time zone-specific information whenever time is recorded.
- Capture locale, location, or system information for all records (for example, in a Location column and a separate location information table).
Make all time relative to a common base
In this approach, the application that applies the changes on the database converts all-time information to a common base (for example, UTC or GMT). To do this, you set the TIMEZONE session variable in NPS to the required base zone, resulting in all time-related functions utilization use the same value.
SYSTEM(ADMIN)=> SET TIMEZONE='GMT';
SET VARIABLE
SYSTEM(ADMIN)=> show timezone;
NOTICE: Time zone is GMT
SHOW VARIABLEThe same INSERT statement in the previous section would now record the
current time relative to GMT:
ORDERNUMBER | ORDERTIMESTAMP | ORDERDETAILS
------------+---------------------+--------------
1 | 2012-01-19 14:12:06 | Product AThe GMT timestamp stamp is the value that is replicated to the replica NPS node in London. As a result, a reporting application would see all rows updated with the same values.
ORDERNUMBER | TIMESTAMP | ORDERDETAILS
-------------+---------------------+--------------
8 | 2012-04-13 13:40:41 | t3
7 | 2012-04-13 13:40:25 | t2
6 | 2012-04-13 13:40:13 | t1
5 | 2012-04-13 13:34:32 | jki
4 | 2012-04-13 13:33:49 | def
3 | 2012-04-13 13:33:33 | abc
2 | 2012-04-12 14:41:40 | Product A
1 | 2012-04-12 14:33:03 | Product ASELECT ordernumber, timezone('PDT',
timestamp(substring(ordertimestamp,1,10),
timetz(substring(ordertimestamp, 12)))) as ts, orderdetails FROM
ordertable2 ORDER BY ts desc;
ORDERNUMBER | TS | ORDERDETAILS
------------+------------------------+--------------
8 | 2012-04-13 06:40:41-07 | t3
7 | 2012-04-13 06:40:25-07 | t2
6 | 2012-04-13 06:40:13-07 | t1
5 | 2012-04-13 06:34:32-07 | jki
4 | 2012-04-13 06:33:49-07 | def
3 | 2012-04-13 06:33:33-07 | abc
2 | 2012-04-12 07:41:40-07 | Product A
1 | 2012-04-12 07:33:03-07 | Product A
SELECT ordernumber, timezone('EDT',
timestamp(substring(ordertimestamp,1,10),
timetz(substring(ordertimestamp, 12)))) as ts, orderdetails FROM
ordertable2 ORDER BY ts desc;
ORDERNUMBER | TS | ORDERDETAILS
------------+------------------------+--------------
8 | 2012-04-13 09:40:41-04 | t3
7 | 2012-04-13 09:40:25-04 | t2
6 | 2012-04-13 09:40:13-04 | t1
5 | 2012-04-13 09:34:32-04 | jki
4 | 2012-04-13 09:33:49-04 | def
3 | 2012-04-13 09:33:33-04 | abc
2 | 2012-04-12 10:41:40-04 | Product A
1 | 2012-04-12 10:33:03-04 | Product AFor applications being run in different time locales, time comparisons and processing can be done relative to an application's session time zone. As shown in the examples, you do this by using a common relative time base (for example, UTC).
Capture time zone-specific information
The approach of capturing and including time zone-specific information is similar to the approach of using a common time base. With the approach of capturing and including time zone-specific information, you can also store the originating locale of the row information when NPS nodes are in different time zones.
You must change the data type for the time information from TIMESTAMP to VARCHAR. You can still convert the character representation of the timestamp information into date/time-related types for use in functions, because multiple functions can accept a varying character type. Alternatively, you can use separate fields for date and time information, storing time by using the TIMETZ type.
INSERT INTO ordertable2 values (1, timezone('EDT', current_timestamp),
'Product A');In this example, which uses the TIMEZONE function, the time zone of the session variable was first set to GMT (as in the example that used a common relative time base in the previous section). This is necessary because the string value representation of the time that is returned by the TIMEZONE function is based on the offset from GMT that is specified in the first parameter for the function (in this case, 'EDT').
ORDERNUMBER | ORDERTIMESTAMP | ORDERDETAILS
------------+------------------------+--------------
1 | 2012-01-19 09:12:06-05 | Product AThe timestamp now has the local primary NPS node timestamp (EST) but with the offset in number of hours from GMT (‘-05’). This value is replicated to the replica NPS node in London. A reporting application that runs against the London NPS node can see that the time of the row indicates that it originated on the New York-based NPS node. However, the application still must convert the timestamp to a common, local, or relative time value to perform valid comparisons on the values.
Capture location identifier information
Capturing location identifier information is similar to the second approach. However, instead of setting the time to a common base and capturing the relative offset, this third approach uses location-specifying information to generate the appropriate date time value that can be used to convert, identify, or look up location identifiers. In this scenario, the time zone settings of the session are left as the default system time zone settings.
ORDERNUMBER | ORDERTIMESTAMP | ORDERDETAILS | LOC_CODE
------------+----------------------+--------------+----------
1 | 2012-01-19 09:12:06 | Product A | 1
LOC_CODE | LOCATIONCITY | LOCATIONTZ | TIMEZONEOFFSET
---------+------------------+------------+----------------
1 | New York, NY | EST | -5You can customize the format and stored information for specific application needs.
Other similar approaches capture a specific time zone identifier or time offset specifically, rather than a code mapping to another table. Key is the ability to capture information that might then pass relevant information that can be passed to a time conversion function such as TIMEZONE(). As illustrated in the first set of examples, you can convert time values to a common base for analysis, comparison, reporting, and other purposes.