> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# set_integer_now_func()

> Define the relationship between integer time values and actual time

export const CHUNK_CAP = 'Chunk';

export const CHUNK = 'chunk';

export const HYPERTABLE_CAP = 'Hypertable';

export const HYPERTABLE = 'hypertable';

<Icon icon="tag" iconType="duotone" /> Since [1.5.0][tsdb-1.5.0]

Override the [`now()`][now] date/time function used to
set the current time in the integer `time` column in a {HYPERTABLE}. Many policies only apply to
[{CHUNK}s][chunks] of a certain age. `integer_now_func` determines the age of each {CHUNK}.

The function you set as `integer_now_func` has no arguments. It must be either:

* `IMMUTABLE`: Use when you execute the query each time rather than prepare it prior to execution. The value
  for `integer_now_func` is computed before the plan is generated. This generates a significantly smaller
  plan, especially if you have a lot of {CHUNK}s.

* `STABLE`: `integer_now_func` is evaluated just before query execution starts.
  [{CHUNK} pruning][chunk-pruning] is executed at runtime. This generates a correct result, but may increase
  planning time.

`set_integer_now_func` does not work on tables where the `time` column type is `TIMESTAMP`, `TIMESTAMPTZ`, or
`DATE`.

## Samples

Set the integer `now` function for a {HYPERTABLE} with a time column in [unix time][unix-time].

* `IMMUTABLE`: when you execute the query each time:
  ```sql theme={"dark"}
  CREATE OR REPLACE FUNCTION unix_now_immutable() returns BIGINT LANGUAGE SQL IMMUTABLE as $$  SELECT extract (epoch from now())::BIGINT $$;

  SELECT set_integer_now_func('hypertable_name', 'unix_now_immutable');
  ```

* `STABLE`: for prepared statements:
  ```sql theme={"dark"}
  CREATE OR REPLACE FUNCTION unix_now_stable() returns BIGINT LANGUAGE SQL STABLE AS $$ SELECT extract(epoch from now())::BIGINT $$;

  SELECT set_integer_now_func('hypertable_name', 'unix_now_stable');
  ```

## Arguments

The syntax is:

```sql theme={"dark"}
SELECT set_integer_now_func(
    hypertable = '<hypertable_name>',
    integer_now_func = '<function_name>',
    replace_if_exists = true | false
);
```

| Name                | Type     | Default | Required | Description                                                                                        |
| ------------------- | -------- | ------- | -------- | -------------------------------------------------------------------------------------------------- |
| `hypertable`        | REGCLASS | -       | ✔        | The {HYPERTABLE} `integer_now_func` is used in.                                                    |
| `integer_now_func`  | REGPROC  | -       | ✔        | A function that returns the current time set in each row in the `time` column in the {HYPERTABLE}. |
| `replace_if_exists` | BOOLEAN  | `FALSE` | ✖        | Set to `true` to override `integer_now_func` when you have previously set a custom function.       |

## Returns

This function returns void.

[chunk-pruning]: https://www.timescale.com/blog/optimizing-queries-timescaledb-hypertables-with-partitions-postgresql-6366873a995d

[chunks]: /use-timescale/hypertables/#hypertable-partitioning

[now]: https://www.postgresql.org/docs/16/functions-datetime.html

[tsdb-1.5.0]: https://github.com/timescale/timescaledb/releases/tag/1.5.0

[unix-time]: https://en.wikipedia.org/wiki/Unix_time
