Skip to content

Instantly share code, notes, and snippets.

View dvarrazzo's full-sized avatar

Daniele Varrazzo dvarrazzo

View GitHub Profile
@dvarrazzo
dvarrazzo / psycopg3_logical_replication.py
Created October 11, 2025 17:13 — forked from RPG-fan/psycopg3_logical_replication.py
A production-ready implementation of PostgreSQL logical replication using psycopg3. Works around psycopg3's missing replication support by using a raw socket bridge.
"""
PostgreSQL Logical Replication for psycopg3
A production-ready implementation of PostgreSQL logical replication using psycopg3.
Works around psycopg3's missing replication support by using a raw socket bridge.
Author: Richard Brandes
Date: October 2025
License: MIT
@dvarrazzo
dvarrazzo / bench-fetchall.py
Created September 13, 2025 21:26
Benchmark script to measure the effects in https://github.com/psycopg/psycopg/pull/1163
import statistics as st
from time import sleep, monotonic
from argparse import ArgumentParser, Namespace
import psycopg
import psycopg2
from psycopg import sql
DSN = "dbname=psycopg3_test host=localhost sslmode=disable"
@dvarrazzo
dvarrazzo / walrusify.py
Last active March 28, 2025 11:25
A script to convert assignments followed by ifs with a walrus operator
#!/usr/bin/env python
"""Convert a codebase to using the walrus operator.
Hint: in order to explore the AST of a module you can run:
python -m ast path/to/module.py
"""
from __future__ import annotations
@dvarrazzo
dvarrazzo / dicttuple.py
Last active September 7, 2022 16:08
DictCursor for psycopg 3
class DictTuple(tuple):
"""Tuple class with added item getting by name.
"""
def __new__(cls, d):
rv = super().__new__(cls, d.values())
rv._map = d
return rv
def __repr__(self):
return f"{type(self).__qualname__}({self._map!r})"
@dvarrazzo
dvarrazzo / stream.mermaid
Created May 1, 2022 19:41
Flow diagram of a cursor.stream() iteration in psycopg 3
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@dvarrazzo
dvarrazzo / tests.yaml
Created March 1, 2021 00:13
Sketch of psycopg3 test action
name: Tests
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
tests-python:
@dvarrazzo
dvarrazzo / dchost
Last active January 27, 2021 16:21
#!/bin/bash
# Return the ip address for a container in docker-compose
set -euo pipefail
# set -x
if [[ "${1:-}" == "" ]]; then
echo "usage: $0 container
#!/usr/bin/env python3
"""
Apply database patches.
Applied patches are recorded in the schema_patch table of the database.
The dsn to connect to defaults to a local one (empty connection string). It can
be chosen using the command line or an environment variable. Patches
application is interactive by default.
@dvarrazzo
dvarrazzo / ministan.py
Last active April 19, 2020 01:00
A simple way to generate valid xhtml in Python (circa 2007)
r"""Implementation of a `stan`_\ -like XML description language.
.. _stan: http://www.kieranholland.com/code/documentation/nevow-stan/
An example XHTML page can be generated by:
.. python::
def items():
return [ T.li['foo'], T.li['bar'], T.li['baz'] ]
@dvarrazzo
dvarrazzo / test_anyio.py
Created April 1, 2020 11:02
A test to use psycopg3 with anyio
"""
A quick test to try and run psycopg3 with anyio
"""
import anyio
from psycopg3 import exceptions as exc
from psycopg3.waiting import Wait, Ready
from psycopg3.connection import AsyncConnection
import logging