Native Postgres update, upsert, and copy operations.
Project description
django-pgbulk
django-pgbulk
provides functions for doing native Postgres bulk upserts (i.e. UPDATE ON CONFLICT), bulk updates, and COPY FROM.
Bulk upserts can distinguish between updated/created rows and ignore unchanged updates.
Bulk updates are true bulk updates, unlike Django's bulk_update which can still suffer from O(N) queries and can create poor locking scenarios.
Bulk copies can significantly speed-up bulk inserts, sometimes by an order of magnitude over Django's bulk_create
.
Quick Start
Examples
Update or insert rows
import pgbulk
pgbulk.upsert(
MyModel,
[
MyModel(int_field=1, some_attr="some_val1"),
MyModel(int_field=2, some_attr="some_val2"),
],
# These are the fields that identify the uniqueness constraint.
["int_field"],
# These are the fields that will be updated if the row already
# exists. If not provided, all fields will be updated
["some_attr"]
)
Bulk update rows
import pgbulk
pgbulk.update(
MyModel,
[
MyModel(id=1, some_attr='some_val1'),
MyModel(id=2, some_attr='some_val2')
],
# These are the fields that will be updated. If not provided,
# all fields will be updated
['some_attr']
)
Copy rows into a table
import pgbulk
pgbulk.copy(
MyModel,
# Insert these rows using COPY FROM
[
MyModel(id=1, some_attr='some_val1'),
MyModel(id=2, some_attr='some_val2')
],
)
Advanced Features
Here are some advanced features at a glance:
pgbulk.upsert
can categorize which rows were inserted or updated.pgbulk.upsert
andpgbulk.update
can ignore updating unchanged fields.pgbulk.upsert
andpgbulk.update
can use expressions in updates.
Documentation
View the django-pgbulk docs here for more examples.
Compatibility
django-pgbulk
is compatible with Python 3.9 - 3.13, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 17.
Installation
Install django-pgbulk
with:
pip3 install django-pgbulk
Contributing Guide
For information on setting up django-pgbulk for development and contributing changes, view CONTRIBUTING.md.
Creators
Other Contributors
- @max-muoto
- @dalberto
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django_pgbulk-3.2.0.tar.gz
.
File metadata
- Download URL: django_pgbulk-3.2.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/5.15.0-1057-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f589210a66be2ffc1c5cae5c9d82ee8cd5f50de3ab6531f8c4fdeac917110c70 |
|
MD5 | 2d229ed4c6ea7dec709490a1039ba0d6 |
|
BLAKE2b-256 | 64ed28639783a81267b02acd8034cc1fc6f00665254fd8fe4a7123e125a9fade |
File details
Details for the file django_pgbulk-3.2.0-py3-none-any.whl
.
File metadata
- Download URL: django_pgbulk-3.2.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.12.7 Linux/5.15.0-1057-aws
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0cfe084200f687d1b06a4f36dbe1c9074203cbccb2076567e4e2127277efe59a |
|
MD5 | 1bbf4f111cd7f482a93a36ed7323f118 |
|
BLAKE2b-256 | 522522a26931bd5f3a74c92210f3af169978664f5d283d0e3594c70948f33840 |