News for hackers. Stuff that matters.
Re:Pg8000 – Pure-Python PostgreSQL driver by jquaint on Thursday May 13, 2021 @03:16PM (#27146154)
Question, what advantages does a pure python driver give you over the competition? Is it multi threading support? (psycopg2 does not support cursors over threads)
Re:Pg8000 – Pure-Python PostgreSQL driver by lmns on Thursday May 13, 2021 @06:42PM (#27148412)
Packaging is simpler. psycopg2-binary is actually not recommended for production use, so you have to compile it yourself.
Re:Pg8000 – Pure-Python PostgreSQL driver by ohlookabird on Friday May 14, 2021 @03:19AM (#27151657)
Thanks for the pointer, I wasn't aware of this! I was wondering what the problem is, because I use psycopg2-binary in production without problems for ages. It seems the danger of psycopg2-binary is that one can end up with segfaults, due to the libpq library (and hence psycopg2) might use a different libssl than the system one (use by Python, with e.g. urllib and requests): https://www.psycopg.org/articles/2018/02/08/psycopg-274-rele...
Re:Pg8000 – Pure-Python PostgreSQL driver by BiteCode_dev on Friday May 14, 2021 @04:03AM (#27151899)
I've never heard of that, why would it not be?
Re:Pg8000 – Pure-Python PostgreSQL driver by lmns on Friday May 14, 2021 @08:48AM (#27153656)
I don't know, that's what the developers say (last paragraph): https://pypi.org/project/psycopg2/
Re:Pg8000 – Pure-Python PostgreSQL driver by jka on Thursday May 13, 2021 @04:46PM (#27147224)
Personal opinion, not really inherited from any authoritative wisdom: pure Python is useful because it:

- Simplifies and improves the performance of deployment: it becomes a straightforward case of copying code files

- Largely achieves the same stated goal of Java: write once, run anywhere

- Allows for easy code inspection and debugging in production (it's debatable whether that's a good thing; I think it is, overall)

Re:Pg8000 – Pure-Python PostgreSQL driver by grandinj on Friday May 14, 2021 @03:38AM (#27151774)
Agreed. It also unlocks another level of optimisation - the human one - I'm much better at reasoning my way through code improvements when I don't have to manage dealing with switching language/runtime models in the middle of the code :-)
Re:Pg8000 – Pure-Python PostgreSQL driver by GateCrasher on Friday May 14, 2021 @04:46AM (#27152180)
It is also easy to add to an executable with pyinstaller
Re:Pg8000 – Pure-Python PostgreSQL driver by anentropic on Friday May 14, 2021 @06:36AM (#27152776)
It is useful if you want a speed boost by running under PyPy

Take all benchmarking with a pinch of salt, but see some stats here: https://suade.org/dev/12-requests-per-second-with-python/#th...

Re:Pg8000 – Pure-Python PostgreSQL driver by willvarfar on Friday May 14, 2021 @03:03AM (#27151573)
I once speeded up a program by changing from the normal native wrap MySQL drivers to a pure python one.

The pure python MySQL drivers were marginally slower for cpython.

The native drivers worked fine with pypy, but pypy didn’t give any real performance gains as the program spent all its time in the native code

Then when testing pure python drivers we found pypy made the whole program massively faster.

Re:Pg8000 – Pure-Python PostgreSQL driver by qeternity on Friday May 14, 2021 @03:15AM (#27151637)
That’s surprising if your app spent most of the time in database calls, since 1) native code is still faster than pypy and 2) most of the time spent in database libraries is spent waiting for the database to return.
Re:Pg8000 – Pure-Python PostgreSQL driver by willvarfar on Friday May 14, 2021 @07:06AM (#27152949)
More details of my use case:

The programs were ETLs that are generating big REPLACE INTO statements.

Different databases and drivers do prepared statements in different ways. Postgres, for example, can receive the statement and the parameters and do the interpolation in the server. The params still need to be serial used by the client and the template isn’t reused, so it’s no big perf win.

MySQL, on the other hand, needs the driver to do the interpolation. The server only knows how to execute sql strings.

It turns out it is far better to prepare the sql in pure python where pypy knows all about types etc than to have the c MySQL driver do it.

Exactly the same thing plays out in java. The MySQL java drivers are staggeringly inefficient at large prepared statements, and in my profiling massive amounts of time is spent in java functions in the MySQL driver that are trying to work out where the next question-mark is. Sadly the drivers are the poo they are. Iirc the mariadb java drivers are rather more optimized.

Re:Pg8000 – Pure-Python PostgreSQL driver by dragonwriter on Friday May 14, 2021 @06:35AM (#27152767)
> That’s surprising if your app spent most of the time in database calls, since 1) native code is still faster than pypy and 2) most of the time spent in database libraries is spent waiting for the database to return

I would argue a speedup (though maybe not a “massive” one) from pure python over a native extension is not that surprising, specifically because of (2), which makes it possible for the runtime-extension interface overhead to be more significant than (1). Native extensions tend to give a speed boost for computationally intensive tasks, but can’t really wait any faster, and aren’t free of overhead.

Re:Pg8000 – Pure-Python PostgreSQL driver by zeta0134 on Friday May 14, 2021 @03:17AM (#27151648)
Interesting. What sort of optimizations does PyPy do? Could it be that pure python unlocked more options in that department due to not needing to preserve the native bindings?
Re:Pg8000 – Pure-Python PostgreSQL driver by fulafel on Friday May 14, 2021 @04:49AM (#27152196)
Wider compatibility than just CPython, potentially at least.
Re:Pg8000 – Pure-Python PostgreSQL driver by mjbrownie on Friday May 14, 2021 @05:05AM (#27152289)
I just managed to get it working on an ipads iVim python environment which is pretty crippled (no compiler etc).