Time in a replicated environment

Because time information is replicated by value from the primary NPS® node, there is no automatic conversion of time values to their local equivalents or any adjustments for differences in time synchronization between nodes. The exact date/time value that is captured on the primary is the value that is inserted on the replica.

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.

Important: Changing the time on running systems can cause operational issues, such as delays in event firing.

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.

The following behaviors apply to NPS temporal types and handling:
  • 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).
The following example illustrates time zones by using a two-node Netezza replication environment where the primary node is in New York, US (Eastern time zone), and the replica node is in London, UK (GMT). The following order row is created on the primary NPS node in New York and is replicated to the replica in London. In this example, the current timestamp is 19 January 2012 at 9:12 AM.
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 A

This 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

Important: If the NTP synchronization service fails to synchronize the replication nodes, refer to the following workarounds.
Time zone differences can impact reporting applications that run against the replica NPS node. For example, as shown in the previous example, new data might be added to the database that appears to be from several hours ago. Further, additional problems are introduced if the roles of the two NPS nodes are reversed (London is now the primary and New York is now the replica). In that case, there will be rows with timestamps from the future relative to the replica.
There are several possible approaches to working around time zone differences. The correct solution depends on your applications and environment.
  • 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).
These approaches are described in the following sections.

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.

In the previous example, the applications would set the TIMEZONE session variable on each node to GMT before performing the DML operations:
SYSTEM(ADMIN)=> SET TIMEZONE='GMT';
SET VARIABLE
SYSTEM(ADMIN)=> show timezone;
NOTICE: Time zone is GMT
SHOW VARIABLE
The 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 A

The 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.

For example, a reporting application in the previous example would need to be aware of the different relative operating times of the two regions. A query to show “all orders from today” would need to consider what “today” is, relative to the different locations (New York and London). In this case, the application might benefit from knowing where the order originated to be able to convert the relative time to that of a location. To achieve this, an application might convert all common timestamp information (which uses GMT, in the example) into another specific time zone and then process the information. Consider an extended version of the previous example, where output is as follows:

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 A
These entries are all GMT time stamps. They can be converted into other relative times by using SQL functions, as follows. First, convert to Pacific Daylight Time:
SELECT ordernumber, timezone('PDT',
timestamp(substring(ordertimestamp,1,10),
timetz(substring(ordertimestamp, 12)))) as ts, orderdetails FROM
ordertable2 ORDER BY ts desc;
Output follows:

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
In this example, the information in the same rows is converted to Eastern Daylight Time:
SELECT ordernumber, timezone('EDT',
timestamp(substring(ordertimestamp,1,10),
timetz(substring(ordertimestamp, 12)))) as ts, orderdetails FROM
ordertable2 ORDER BY ts desc;
Output follows:

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 A

For 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.

This example, similar to the original example, uses the VARCHAR type to capture the time stamp information:
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').

The resulting order row in the table is as follows:

ORDERNUMBER |     ORDERTIMESTAMP     | ORDERDETAILS
------------+------------------------+--------------
          1 | 2012-01-19 09:12:06-05 | Product A

The 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.

Availability of location or time zone-specific information is necessary in the session f capture. You can achieve this by passing a location code, time zone, time zone offset, or some other identifier into the session or through some other means. A sample table follows:

ORDERNUMBER |    ORDERTIMESTAMP    | ORDERDETAILS | LOC_CODE
------------+----------------------+--------------+----------
          1 | 2012-01-19 09:12:06  | Product A    | 1
The LOC_CODE field can then be a mapping to another table that contains location details:

LOC_CODE |   LOCATIONCITY   | LOCATIONTZ | TIMEZONEOFFSET
---------+------------------+------------+----------------
       1 | New York, NY     | EST        | -5

You 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.