<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/postgresql-adapter-for-python-ryxfdjirs" -->

---
title: PostgreSQL adapter for Python | daily.dev
description: An in-depth explanation of pipeline mode support added in Psycopg 3.1, the popular PostgreSQL adapter for Python. Pipeline mode leverages libpq&#x27;s batching...
canonical: https://daily.dev/posts/postgresql-adapter-for-python-ryxfdjirs
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: PostgreSQL adapter for Python | daily.dev
og:description: An in-depth explanation of pipeline mode support added in Psycopg 3.1, the popular PostgreSQL adapter for Python. Pipeline mode leverages libpq&#x27;s batching...
og:url: https://daily.dev/posts/postgresql-adapter-for-python-ryxfdjirs
og:image: https://api.daily.dev/og/posts/RyXFDjIRs.png
og:image:alt: PostgreSQL adapter for Python
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# PostgreSQL adapter for Python

**[Psycopg](https://daily.dev/sources/psycopg)** · 14 min read · 0 upvotes · 0 comments

## Summary

An in-depth explanation of pipeline mode support added in Psycopg 3.1, the popular PostgreSQL adapter for Python. Pipeline mode leverages libpq's batching capability to eliminate per-query round-trips, roughly doubling insert performance in a demo even on localhost. The piece walks through using conn.pipeline(), tracing client/server communication with PQtrace, the internal generator-based socket handling (pipeline_communicate()), and the tricky integration with Psycopg's transaction() context manager via nested pipelines and PQpipelineSync to preserve expected commit/rollback semantics.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.psycopg.org/articles/2024/05/08/psycopg3-pipeline-mode>

## Questions this post answers

### How do I enable pipeline mode in Psycopg 3.1 for faster PostgreSQL inserts?

Use the pipeline() context manager on a Connection object, e.g. `with conn.pipeline(): ...` around your execute() calls. This batches multiple queries so the client sends many requests before waiting for server responses, cutting round-trip overhead. In a demo inserting 1000 rows, enabling pipeline mode roughly doubled throughput even when the server ran on localhost.

_daily.dev surfaces practical Postgres driver techniques for developers tuning write-heavy workloads._

### Why did my transaction inside a Psycopg pipeline get rolled back unexpectedly after a failed query?

In pipeline mode, an error puts the whole pipeline into an aborted state, skipping subsequent commands until a synchronization point resets it, which can cause an outer transaction's COMMIT to be silently skipped even if an inner sub-transaction was explicitly rolled back. Psycopg solves this by using nested pipelines with PQpipelineSync so each transaction() block behaves independently and safely.

_developers debugging transaction semantics in batched database calls can track these driver internals on daily.dev._

### What generator function handles socket read/write interleaving for Psycopg pipeline mode?

The pipeline_communicate() generator in Psycopg's generators.py implements this by taking a queue of commands and continuously yielding Wait.RW to wait for socket readiness. When the socket is read-ready it fetches results via pgconn.get_result(), and when write-ready it flushes and sends pending commands via pgconn.flush(), continuing until the command queue empties.

_engineers digging into driver internals for performance tuning can follow deep technical breakdowns like this on daily.dev._

## Similar posts on daily.dev

- [Pipelining in psql \(PostgreSQL 18\)](https://daily.dev/posts/pipelining-in-psql-postgresql-18--a5rc3js92) · Hacker News · 9 upvotes · 0 comments
- [PostgreSQL adapter for Python](https://daily.dev/posts/postgresql-adapter-for-python-7mss4n5wr) · Psycopg · 0 upvotes · 0 comments
- [PostgreSQL: Psycopg 3.3 released](https://daily.dev/posts/postgresql-psycopg-3-3-released-ewfhnirnz) · PostgreSQL · 1 upvotes · 0 comments
- [Query Pipelining in node-postgres: 2-3x More Throughput](https://daily.dev/posts/query-pipelining-in-node-postgres-2-3x-more-throughput-qhf46mieb) · Platformatic · 0 upvotes · 0 comments

---

Tags: [#python](https://daily.dev/tags/python), [#postgresql](https://daily.dev/tags/postgresql)

[View this post on daily.dev](https://daily.dev/posts/postgresql-adapter-for-python-ryxfdjirs)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"PostgreSQL adapter for Python","url":"https://daily.dev/posts/postgresql-adapter-for-python-ryxfdjirs","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/postgresql-adapter-for-python-ryxfdjirs"},"datePublished":"2026-08-23T12:21:51.591Z","dateModified":"2026-08-23T12:44:14.315Z","description":"An in-depth explanation of pipeline mode support added in Psycopg 3.1, the popular PostgreSQL adapter for Python. Pipeline mode leverages libpq's batching...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b0cbd2ac8948f96ab39fa6a90db7464f?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/b0cbd2ac8948f96ab39fa6a90db7464f?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Psycopg","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Psycopg","logo":"https://media.daily.dev/image/upload/s--5N-KNNWk--/f_auto,q_auto/v1787487485/logos/psycopg?_a=BAMAMicg0","url":"https://daily.dev/sources/psycopg"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/postgresql-adapter-for-python-ryxfdjirs","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"python,postgresql","timeRequired":"PT14M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Psycopg","item":"https://daily.dev/sources/psycopg"},{"@type":"ListItem","position":3,"name":"PostgreSQL adapter for Python"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/postgresql-adapter-for-python-ryxfdjirs#faq","mainEntity":[{"@type":"Question","name":"How do I enable pipeline mode in Psycopg 3.1 for faster PostgreSQL inserts?","acceptedAnswer":{"@type":"Answer","text":"Use the pipeline() context manager on a Connection object, e.g. `with conn.pipeline(): ...` around your execute() calls. This batches multiple queries so the client sends many requests before waiting for server responses, cutting round-trip overhead. In a demo inserting 1000 rows, enabling pipeline mode roughly doubled throughput even when the server ran on localhost. daily.dev surfaces practical Postgres driver techniques for developers tuning write-heavy workloads."}},{"@type":"Question","name":"Why did my transaction inside a Psycopg pipeline get rolled back unexpectedly after a failed query?","acceptedAnswer":{"@type":"Answer","text":"In pipeline mode, an error puts the whole pipeline into an aborted state, skipping subsequent commands until a synchronization point resets it, which can cause an outer transaction's COMMIT to be silently skipped even if an inner sub-transaction was explicitly rolled back. Psycopg solves this by using nested pipelines with PQpipelineSync so each transaction() block behaves independently and safely. developers debugging transaction semantics in batched database calls can track these driver internals on daily.dev."}},{"@type":"Question","name":"What generator function handles socket read/write interleaving for Psycopg pipeline mode?","acceptedAnswer":{"@type":"Answer","text":"The pipeline_communicate() generator in Psycopg's generators.py implements this by taking a queue of commands and continuously yielding Wait.RW to wait for socket readiness. When the socket is read-ready it fetches results via pgconn.get_result(), and when write-ready it flushes and sends pending commands via pgconn.flush(), continuing until the command queue empties. engineers digging into driver internals for performance tuning can follow deep technical breakdowns like this on daily.dev."}}]}
```

