r/Kotlin • u/OctaviusAugustus27 • 3h ago
3 months after 1.0.0 of my PostgreSQL driver in Kotlin
Hello, that's my second post on this subreddit. The first one was about version 1.0.0 of my PostgreSQL driver written in Kotlin. Now, 3 months later, I have version 2.15.0. Why am I writing a post for that version? I don't know, just because.
A little context
Last time there were only one and a half sentences about it. I use the driver in my own app: a desktop app for Windows on Compose and Koin, with a Ktor server and a browser extension.
The most natural question is why even use Postgres for a desktop app, and that's simple: the database was BEFORE the application. A database with enums and arrays, and after some time also trigrams. Migrating away from Postgres is now probably impossible, and away from my driver too, because I return data as anonymous rows (record and record[]) and now also read them into nested classes.
About the driver
2.15.0 is the 19th published version after 1.0.0. I version by "reach of a change" (it's in the README), so even with other breaking changes only 2.0.0 got a new major: since then a query pins one type catalog for its whole execution.
A correction to the previous post: requiring protocol 3.2 wasn't that great an idea. PgBouncer (at least 1.25) ends on 3.0, so the driver still asks for 3.2 but accepts 3.0 and checks the server_version parameter instead. I also added a check that search_path is reported at all.
The JDBC facade
When you first think about it, it should be easy, right? You throw "not implemented" on most methods and there are no problems. Well, it's not that easy. I describe this from the side of Spring and Hikari, because I don't know if it would even work with other libraries (probably not).
First, how do you even get to your own API? First thought: unwrap. You get the native connection, and its close closes the socket. So you must have a layer above Connection.
Great, now the next problem: how to make Hikari aware that a connection is dead when you don't use Statement/PreparedStatement. Hikari wraps the connection and statements in proxies and checks the SQLSTATE of each SQLException passing through. Maybe you think you don't need this because isValid exists? If a connection was used in the last 500 ms (aliveBypassWindowMs), Hikari hands it out without calling isValid. So you MUST have a way to tell Hikari to evict the connection. I do it by calling abort() instead of close() on the proxy, and having my abort() throw a SQLException with a prepared SQLSTATE so Hikari catches it and evicts the connection.
Spring isn't that easy either. After some time I found that I MUST have DatabaseMetaData, because Spring asks it about savepoint support. Then there are the problems with JdbcTransactionManager, which was written a long time ago. Timeouts, for example: I don't have setQueryTimeout, because I don't have statements, so I changed them to SET LOCAL commands.
And in all this you have the worst enemy: SQLException. It was the hardest fight. All JDBC libraries rely on JDBC throwing SQLException. Not any other, only SQLException. Even if you don't like it, you must use it. I wrap my exception in a wrapper with a copied SQLSTATE, without a cause and even without a stack trace, and unwrap it again in my API. In Spring I ended up writing my own class inheriting from AbstractPlatformTransactionManager...
Even with these pain points I think it was worth it, because you really can use it in Spring and the configuration is easy.
Mostly fixes
When I think about it, most versions have fixes, not new features. Integration tests are better than unit tests, and bugs in other drivers can be a great inspiration. Recently I was bored and went through pgjdbc and libpq CVEs, and fixed the ones that affected my driver. The most interesting was CVE-2026-134842, the part about SJIS: in simple query mode pgjdbc pastes parameters into the query, and with allowEncodingChanges=true and SJIS the yen sign is encoded as 0x5C, which Postgres reads as a backslash. So a parameter value could run as SQL. I don't paste parameters, but I found that I didn't check client_encoding at all, so the driver could write and read text incorrectly. Now I close the connection when the encoding changes. I also hadn't checked whether an error from Postgres was FATAL (more on this below).
Things I found in Postgres
First, date and time, the thing from my comment under the last post: kotlinx-datetime writes dates past year 9999 with a +, and Postgres doesn't accept it. Now I can say that thread is closed: Postgres 19 has this beautiful parenthesis in section 8.5.1: "(PostgreSQL does not support ISO 8601-optional signed years.)" I have no idea if other people would understand what that means :> What's interesting, jOOQ has a similar problem in MULTISET: a date like 10000-01-01 or infinity silently comes back as null.
Second, Postgres 18 changed the SQLSTATE for RESTRICT foreign keys from 23503 to 23001. This change wasn't in the release notes (now it is, because of my patch).
And lastly, sockets on Windows. The client can lose the FATAL message from Postgres: the backend exits without closing the socket gracefully, Windows sends RST instead of FIN like on Linux, and whatever the client hasn't read yet, FATAL included, is thrown away. I compiled my own Postgres to check an old fix for this (it was reverted in 2022, so no release has it).
Kotlin and Java ecosystem
The ecosystem is sewn up by giants: pgjdbc, Vert.x (with its own libraries) and R2DBC (which looks rather quiet). Even with AI usage I found only about 5 new projects with the Postgres wire protocol (maybe I just don't know how to use GitHub search :>). For one of the most popular languages and one of the most popular databases I think this is a low number. Even counting wrappers over libpq or sqlx, it's not much higher.
I don't understand why most people in Java and Kotlin don't want to write native SQL. Most database libraries are ORMs, DSLs, query builders and code generators. I don't see much benefit in that (not that I have a problem with code generation, my plugin for translations generates classes from JSONs), but Postgres with its custom types and operators isn't a great target: you end up with 2 versions of the same code or writing strings anyway, and at the end you debug the generated SQL, not the DSL.
I was also surprised that the libraries I looked at are not that different from JDBC in their general shape, and don't really use Kotlin's advantages over Java. For example, I use reified in get<T> to know if a column can be null. Maybe that's my prejudice, but I think many people still write Kotlin like Java, and no, changing if (variable != null) to variable?.let {} doesn't magically turn Java code into Kotlin code. Kotlin is enjoyable to write, with one limitation: type erasure. It is horrible, especially when passing arguments to the final fetch call as a map or a list. reified can't fix everything, and Valhalla doesn't look like it will help much.
AI, solo development
The wire protocol was easy, especially with AI. Worse was thinking about the API and how to connect all the mechanisms, and for that an application is a very good point to look from, because you see how you will use it. But in general writing is easy. Maintenance is hard. Fast progress at the beginning is fun, but it changes into fixing strange things, and AI doesn't make that much easier: AI (and people) tend to add code, and when you add code, you (and AI) have more context and more things to look at. In solo development (and without the most expensive AI plans) the key is cutting: code, KDocs, documentation, and questioning what AI writes. It is also a great way of learning.
Motivation matters, especially for a driver. Stars are not a great motivation: of those new wire protocol projects I saw only one still alive besides mine, pg-java. Mine was the application. And trying to keep the code readable and simple (not spaghetti and not lasagna) matters too. If it gets ugly, it starts to feel like work, where the code is always bad, and then you just don't want to keep working on it.
Writing a project in 2 weeks for a CV, going wide (different library, different stack), is easy. Maintaining something for yourself for 2 years and going deep, is not.
Repo: https://github.com/Octavius-Framework/octavius-postgresql