Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#28334 closed Cleanup/optimization (fixed)

contrib.postgresql overwhelms database with "select from pg_type" queries on each request

Reported by: Igor Gumenyuk Owned by: Igor Gumenyuk
Component: contrib.postgres Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Igor Gumenyuk)

On each newly created database connection django.contrib.postgresql tries to register hstore type for this connection via connection_created signal and register_hstore function.

https://github.com/django/django/blob/master/django/contrib/postgres/apps.py#L20
https://github.com/django/django/blob/stable/1.11.x/django/contrib/postgres/signals.py#L16

Behind the scenes register_hstore runs sql query to get oid of hstore type:
https://github.com/psycopg/psycopg2/blob/master/lib/extras.py#L885

This happens on every(!) request (unless persistent connections enabled)

SELECT t.oid, %s
FROM pg_type t JOIN pg_namespace ns
ON typnamespace = ns.oid
WHERE typname = 'hstore';

This is just huge overhead, since register_hstore accepts oid argument to avoid hitting database every call.
We have seen significant latency spikes because of this.

Possible solution would be to have configurable GET_HSTORE_OID_FUNC in DATABASES->OPTIONS setting which can be cached in any way.
This will also maintain backwards compatibility.

I can send patch for this if proposed solution is acceptable to be merged in master.

Change History (13)

comment:1 by Igor Gumenyuk, 7 years ago

Description: modified (diff)

comment:2 by Claude Paroz, 7 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Igor Gumenyuk, 7 years ago

Owner: set to Igor Gumenyuk
Status: newassigned

comment:4 by Igor Gumenyuk, 7 years ago

Has patch: set
Version 0, edited 7 years ago by Igor Gumenyuk (next)

comment:5 by Tim Graham, 7 years ago

Patch needs improvement: set

There are test failures on the pull request.

comment:6 by Igor Gumenyuk, 7 years ago

Patch needs improvement: unset

comment:7 by Igor Gumenyuk, 7 years ago

All issues resolved, there are no failed tests

comment:8 by Claude Paroz, 7 years ago

Needs tests: set

comment:9 by Igor Gumenyuk, 7 years ago

Needs tests: unset

comment:10 by Tim Graham, 7 years ago

Patch needs improvement: set

comment:11 by Tim Graham, 7 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:12 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 86a18dc:

Fixed #28334 -- Added caching for hstore/citext OIDs.

comment:13 by Tim Graham <timograham@…>, 7 years ago

In f7b0532:

Refs #28334 -- Fixed crash in hstore/citext oid caching during test db creation.

Note: See TracTickets for help on using tickets.
Back to Top