summaryrefslogtreecommitdiff
path: root/django/db/backends/postgresql
diff options
context:
space:
mode:
authorAarni Koskela <akx@iki.fi>2026-03-15 00:04:26 +0100
committerJacob Walls <jacobtylerwalls@gmail.com>2026-03-20 16:31:25 -0400
commit994fa7780d73f6d618dc3a03339e96b5353b4e71 (patch)
tree03673f601a6646e6d02bfbca13f562ff773047a8 /django/db/backends/postgresql
parent9b2cc7a950b14cdde4c85d8ed0ebc59784a68725 (diff)
Fixed #36960 -- Enabled the use of psycopg 3's optimized timestamp loader.
Based on Daniele Varrazzo's comment in https://github.com/psycopg/psycopg/issues/1273#issuecomment-3986829769
Diffstat (limited to 'django/db/backends/postgresql')
-rw-r--r--django/db/backends/postgresql/psycopg_any.py16
1 files changed, 11 insertions, 5 deletions
diff --git a/django/db/backends/postgresql/psycopg_any.py b/django/db/backends/postgresql/psycopg_any.py
index dea4800fce..b877afc680 100644
--- a/django/db/backends/postgresql/psycopg_any.py
+++ b/django/db/backends/postgresql/psycopg_any.py
@@ -2,9 +2,8 @@ import ipaddress
from functools import lru_cache
try:
- from psycopg import ClientCursor, IsolationLevel, adapt, adapters, errors, sql
+ from psycopg import ClientCursor, IsolationLevel, adapt, adapters, errors, pq, sql
from psycopg.postgres import types
- from psycopg.types.datetime import TimestamptzLoader
from psycopg.types.json import Jsonb
from psycopg.types.range import Range, RangeDumper
from psycopg.types.string import TextLoader
@@ -16,13 +15,17 @@ try:
TSRANGE_OID = types["tsrange"].oid
TSTZRANGE_OID = types["tstzrange"].oid
+ orig_tz_loader_cls = adapters.get_loader(
+ types["timestamptz"].oid,
+ pq.Format.TEXT,
+ )
def mogrify(sql, params, connection):
with connection.cursor() as cursor:
return ClientCursor(cursor.connection).mogrify(sql, params)
# Adapters.
- class BaseTzLoader(TimestamptzLoader):
+ class BaseTzLoader(adapt.Loader):
"""
Load a PostgreSQL timestamptz using the a specific timezone.
The timezone can be None too, in which case it will be chopped.
@@ -30,9 +33,12 @@ try:
timezone = None
+ def __init__(self, oid, context):
+ super().__init__(oid, context)
+ self.orig_loader = orig_tz_loader_cls(oid, context)
+
def load(self, data):
- res = super().load(data)
- return res.replace(tzinfo=self.timezone)
+ return self.orig_loader.load(data).replace(tzinfo=self.timezone)
def register_tzloader(tz, context):
class SpecificTzLoader(BaseTzLoader):