AGENT SKILL

timestamptz-standardization

Timestamp Standardization (timestamptz)

License 许可
MIT
最近更新
2026-11-14
Stars
★ 18
Forks
6

01 前言 Front Matter

name
timestamptz-standardization
metadata
{ "original-index": 26, "short-description": "Timestamp Standardization (timestamptz)" }
description
Timestamp Standardization (timestamptz)

02 资源文件 1

03 SKILL.md 正文

Timestamp Standardization (timestamptz)

Source: PostgreSQL Best Practices -- "Use timestamptz (UTC). Avoid timezone-naive types."

What

Converting all timestamp without time zone columns to timestamptz (timestamp with time zone) and ensuring UTC semantics across the schema.

Why

timestamp without time zone stores a bare datetime with no timezone information. PostgreSQL interprets it differently depending on the session's TimeZone setting. This causes:

  • Ambiguous time values across servers, clients, and time zones
  • Wrong results in scheduled jobs that cross DST boundaries
  • Incorrect ordering/filtering when Azure servers default to UTC but clients assume local time
  • Silent data corruption when aggregating timestamps from multiple zones

timestamptz stores the value in UTC internally and converts for display based on the session timezone. This eliminates all ambiguity.

Official PG 16 specification (datatype-datetime.html):

  • Both timestamp and timestamptz use 8 bytes, microsecond precision
  • timestamptz is stored internally as UTC
  • On output, the value is converted from UTC to the session's TimeZone
  • The SQL standard requires plain timestamp to mean timestamp without time zone -- always spell out timestamptz explicitly

How

Step 1: Assess scope

-- Find all timestamp columns that are NOT timestamptz
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'public'
  AND data_type = 'timestamp without time zone'
ORDER BY table_name, ordinal_position;

Step 2: Convert with explicit UTC reinterpretation

ALTER TABLE public."MyTable"
    ALTER COLUMN "CreatedDate" TYPE timestamptz
    USING "CreatedDate" AT TIME ZONE 'UTC';

The AT TIME ZONE 'UTC' clause tells PostgreSQL: "these existing values were meant to be UTC -- store them as UTC timestamptz."

Step 3: Handle dependent views

Views that SELECT timestamp columns will break if the underlying type changes. Drop and recreate them:

-- Save view definition
SELECT pg_get_viewdef('public."MyView"'::regclass, true);

-- Drop view
DROP VIEW IF EXISTS public."MyView";

-- Alter column type
ALTER TABLE public."MyTable"
    ALTER COLUMN "CreatedDate" TYPE timestamptz
    USING "CreatedDate" AT TIME ZONE 'UTC';

-- Recreate view
CREATE VIEW public."MyView" AS ...;

Step 4: Set default for new rows

ALTER TABLE public."MyTable"
    ALTER COLUMN "CreatedDate" SET DEFAULT now();
    -- now() returns timestamptz, not timestamp

Decision Matrix

Scenario Use
Any new column storing "when something happened" timestamptz NOT NULL DEFAULT now()
Columns storing a user-entered date only (no time) date
Columns storing a duration or interval interval
Legacy column with timestamp without time zone Convert to timestamptz

Key PostgreSQL Behavior

-- Session timezone affects display, NOT storage
SET timezone = 'America/Los_Angeles';
SELECT now();  -- displays in Pacific time
SELECT now() AT TIME ZONE 'UTC';  -- displays in UTC

-- timestamptz stores in UTC regardless of session
INSERT INTO t (ts) VALUES ('2026-03-04 10:00:00-05');
-- Stored as: 2026-03-04 15:00:00+00 (UTC)

When to Use

  • Every table audit that finds timestamp without time zone columns
  • Every new table definition (always use timestamptz)
  • After audit findings flag timestamp type inconsistencies

Where We Used It

  • ****: Converted ALL Scribe tables to timestamptz (original ticket)
  • ****: Standardized AuditLog/ErrorLog timestamps
  • ****: Migrated 15 Feature Flag columns to timestamptz
  • ****: Converted 65 myapp columns to timestamptz
  • scheduling Audit: Finding #2 flagged 28 columns across 12 tables

Related Skills

  • Skill #07 (Backward Compatibility) -- app code may assume naive timestamps
  • Skill #04 (Idempotent Design) -- check column type before converting

References

Gotchas

  • ALTER COLUMN TYPE timestamptz rewrites the table -- locks it briefly; on very large tables, plan for downtime or use a staged approach
  • Views that reference the column must be dropped first and recreated after
  • If the server timezone is NOT UTC, AT TIME ZONE 'UTC' is critical -- without it, PostgreSQL assumes the session timezone
  • Application code that formats timestamps without timezone info may display differently after conversion
  • created_at defaults should use now() (returns timestamptz), not CURRENT_TIMESTAMP (same, but now() is more portable in PL/pgSQL)

*Category: DDL | Origin: , , , *

04 同仓库更多 Skills

CarlosCaPe
CarlosCaPe/octorato

yeet

Use only when the user explicitly asks to stage, commit, push, and open a GitHub pull request in one flow using the GitHub CLI (`gh`).

★ 18
CarlosCaPe
CarlosCaPe/octorato

wrangler

Cloudflare Workers CLI for deploying, developing, and managing Workers, KV, R2, D1, Vectorize, Hyperdrive, Workers AI, Containers, Queues, Workflows, Pipelines, and Secrets Store. Load before running wrangler commands to ensure correct syntax and best practices. Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

★ 18
CarlosCaPe
CarlosCaPe/octorato

workspace-skill-discovery

Discover and include ALL skills across the workspace — not just global ones. Use at the start of every session or when the user asks about available skills. Ensures project-level skills (under .claude/skills/ or skills/ in any repo) are never missed.

★ 18
CarlosCaPe
CarlosCaPe/octorato

workers-best-practices

Reviews and authors Cloudflare Workers code against production best practices. Load when writing new Workers, reviewing Worker code, configuring wrangler.jsonc, or checking for common Workers anti-patterns (streaming, floating promises, global state, secrets, bindings, observability). Biases towards retrieval from Cloudflare docs over pre-trained knowledge.

★ 18
CarlosCaPe
CarlosCaPe/octorato

winui-app

Create, build and design WinUI 3 desktop apps with C# and the Windows App SDK following Microsoft's official guidance and CommunityToolkit. Covers XAML, navigation, theming, accessibility and deployment.

★ 18
CarlosCaPe
CarlosCaPe/octorato

windows-brain-script-portability

Scripts in ~/.claude/scripts/ that run on a Windows brain: use the python3.cmd shim and not bare python3, and reconfigure stdout to UTF-8 if you print symbols. Use it when writing a brain script or triaging a Windows-only crash.

★ 18