Skip to main content

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 and pgbulk.update can ignore updating unchanged fields.
  • pgbulk.upsert and pgbulk.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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_pgbulk-3.2.1.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

django_pgbulk-3.2.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file django_pgbulk-3.2.1.tar.gz.

File metadata

  • Download URL: django_pgbulk-3.2.1.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/6.8.0-1020-aws

File hashes

Hashes for django_pgbulk-3.2.1.tar.gz
Algorithm Hash digest
SHA256 e75ee3489698b5ad13d6f542347fdddf363b6d6c4846ca5af949492c12e418b3
MD5 16e4f45ce3430f086794b6ca1ed5ab41
BLAKE2b-256 6ddfc7f7b5f90ee2ac1f923a6e0e952bf04755eca3b26e3894ee6574801c83d1

See more details on using hashes here.

File details

Details for the file django_pgbulk-3.2.1-py3-none-any.whl.

File metadata

  • Download URL: django_pgbulk-3.2.1-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/6.8.0-1020-aws

File hashes

Hashes for django_pgbulk-3.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dbc2fd78671aa86f4662ab59ab5363ddc009b62d96c05624308f040fcc7c72a0
MD5 d59ca8b7399b64b1ef04b48044562388
BLAKE2b-256 77aed31bf12fcbb3808fec911b5681883aff1daa373664dc457b1f8ecd60f9d7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page