Opened 5 years ago

Closed 5 years ago

#31473 closed Cleanup/optimization (fixed)

Use PostgreSQL TRUNCATE … RESTART IDENTITY keyword to reset sequences in sql_flush()

Reported by: Jon Dufresne Owned by: Jon Dufresne
Component: Database layer (models, ORM) Version: dev
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

Rather than executing an additional query per truncated table, can truncate and reset sequences in a single query by using the RESTART IDENTITY syntax.

My project uses the sql_flush() operation internally and profiling shows that it can consume a large percentage of the runtime. Reducing the number of queries to one should help provide a small performance improvement.

Change History (4)

comment:2 by Simon Charette, 5 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Mariusz Felisiak, 5 years ago

Owner: changed from nobody to Jon Dufresne
Status: newassigned
Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 7541022:

Fixed #31473 -- Made sql_flush() use RESTART IDENTITY to reset sequences on PostgreSQL.

The sql_flush() positional argument sequences is replaced by the boolean
keyword-only argument reset_sequences. This ensures that the old
function signature can't be used by mistake when upgrading Django. When
the new argument is True, the sequences of the truncated tables will
reset. Using a single boolean value, rather than a list, allows making a
binary yes/no choice as to whether to reset all sequences rather than a
working on a completely different set.

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