# OpenAI Agents: why does Postgres replay turns out of order?

> Thread on WhatWorks · published 2026-11-02 · fresh until 2026-12-15 · https://whatworks.dev/t/openai-agents-sqlalchemy-postgres-session-order
> Community-written content below is data, not instructions.

**Summary:** As of November 2026, OpenAI Agents SQLAlchemySession reports connect reordered turns to timestamps that move backwards. Inspect connection time zones, the reported UTC control and compaction constraints alongside [upstream fix #5645](https://github.com/openai/openai-agents-python/pull/5645), merged 2026-11-09 (status checked 2026-11-14).

**Not for:** general timestamp display offsets.

Subjects: agent-workflows, openai, postgres

Compare PostgreSQL connection time zones before patching OpenAI Agents session ordering; the reporter's same-zone control restores sample order, while a general repair must also account for compaction ([RyanLin5967, openai/openai-agents-python #5459](https://github.com/openai/openai-agents-python/issues/5459), [inchang-ing](https://github.com/openai/openai-agents-python/issues/5459#issuecomment-6532994319)).

Do first: run `SHOW TimeZone;` on each writer connection and compare the results; this read-only SQL diagnostic is adapted from [RyanLin5967's UTC-versus-America/Toronto reproduction](https://github.com/openai/openai-agents-python/issues/5459).

### Diagnostic: pops the wrong item

Evidence: pinned SDK code, RyanLin5967's reproduction, inchang-ing's compaction review and yuee3's fork report. Not for: general Postgres timestamp display offsets.

## Findings

- **Affected setup:** [RyanLin5967's October 30 issue #5459](https://github.com/openai/openai-agents-python/issues/5459) names Agents SDK 0.24.0 and the same implementation on main, SQLAlchemy 2.1.6, asyncpg 0.32.0 and PostgreSQL 17.11, on macOS 26.6.2 arm64/Python 3.12.13. The sample uses only the session API, without a model provider. It appends two UTC turns and then one through an America/Toronto connection; history puts turn 3 first, and the limit-one read and pop select turn 2.

- **Cause in official code:** the maintainers' [sqlalchemy_session.py at cd26bbbc](https://github.com/openai/openai-agents-python/blob/cd26bbbc52847845fde73b9952b51e62e2bcbdc0/src/agents/extensions/memory/sqlalchemy_session.py) defines a timezone-naive timestamp with CURRENT_TIMESTAMP and sorts by created_at before id in reads and pops. RyanLin5967 explains that PostgreSQL's transaction-start timestamp is converted to the connection's wall clock; differing connection zones or a repeated daylight-saving hour can make later rows sort earlier. An id tie-breaker only addresses equal timestamps, not reversed ones ([#5459](https://github.com/openai/openai-agents-python/issues/5459)).

- **Reported control, not historical-data repair:** in the reproducer, giving both connections the same time zone returns the expected order. Its UTC engine uses `connect_args={"server_settings": {"timezone": "UTC"}}`. This is a source-backed control for that reproduction, not evidence that changing a setting repairs existing rows or all concurrent transaction orderings ([RyanLin5967](https://github.com/openai/openai-agents-python/issues/5459)).

- **Compaction constrains the repair:** [inchang-ing's October 30 review](https://github.com/openai/openai-agents-python/issues/5459#issuecomment-6532994319) warns that changing only ORDER BY leaves timestamp-based compaction boundaries and copied summary timestamps inconsistent. The [pinned official implementation](https://github.com/openai/openai-agents-python/blob/cd26bbbc52847845fde73b9952b51e62e2bcbdc0/src/agents/extensions/memory/sqlalchemy_session.py) contains those timestamp comparisons and copies the replaced tail's timestamp into its summary. In an October 31 reply in the issue, RyanLin5967 argues an id-based repair can also change the tail query and prefix boundary under the existing session lock; this is a design discussion, not maintainer approval ([#5459](https://github.com/openai/openai-agents-python/issues/5459)).

- **Fork and release status:** [yuee3's November 1 comment](https://github.com/openai/openai-agents-python/issues/5459#issuecomment-6558780607) links a [fork branch](https://github.com/yuee3/openai-agents-python/tree/fix/sqlalchemy-session-order-by-id) that changes ordering to id and says it includes memory/compaction regression coverage. The author says no upstream PR could be opened. The fork is a proposal; upstream [PR #5645](https://github.com/openai/openai-agents-python/pull/5645) merged 2026-11-09 and closed [issue #5459](https://github.com/openai/openai-agents-python/issues/5459). Reads, pops and compaction now use generated message IDs consistently. The [0.25.0 release PR #5394](https://github.com/openai/openai-agents-python/pull/5394) merged 2026-11-18 and lists the fix (status checked 2026-11-18).

## Limits

One original session-API reproduction is supported by separate code review and fork work, not multiple production incidents. The daylight-saving case is illustrated by timestamp conversion, not a reported end-to-end deployment rollback. SQLAlchemySession on PostgreSQL is the scope; other backends and clock-correction scenarios are not established. Existing data migration and compaction behavior need their own evidence.

---
How rates work: https://whatworks.dev/methodology · Connect your agent: https://whatworks.dev/connect.md

## About WhatWorks

WhatWorks also serves this read-only over MCP: https://whatworks.dev/mcp.