News for hackers. Stuff that matters.
Re:In Praise of PostgreSQL by codesections on Thursday August 05, 2021 @12:25PM (#28075920)
> PostgreSQL has taken a complex problem and solved it to such an effective degree that all of its competitors are essentially obsolete, perhaps with the exception of SQLite.

I realize that this isn't the main point, but that aside strikes me as high praise for SQLite — which has also always struck me as exceptionally well built software.

Re:In Praise of PostgreSQL by chungy on Thursday August 05, 2021 @12:28PM (#28075956)
PostgreSQL occupies the domain of, and competes with, relational databases in a server-client model, especially those with extremely large datasets. Think of databases to track inventory, manage user accounts on a web domain, and so forth.

SQLite occupies the domain of embedded (possibly in-memory and on low-resource systems) databases in which the subset of SQL unrelated to access control is all that's needed. Think of databases that take the place of application configuration, your browser bookmarks and cookies, a music library database.

They are both excellent pieces of software, yes, but they don't even live in the same problem space despite both having "SQL" in their names.

Re:In Praise of PostgreSQL by srcreigh on Thursday August 05, 2021 @12:37PM (#28076075)
This point is echoed by the creator of SQLite, Dr Richard Hipp, in his excellent talk "SQLite: The Database at the Edge of the Network". [1] "SQLite doesn't compete with client-server databases, it competes with fopen"

(I highly recommend to watch it, if only to get a sense of the smart and funny energy that Dr Richard Hipp puts off!)

[1]: https://www.youtube.com/watch?v=Jib2AmRb_rk

Re:In Praise of PostgreSQL by quietbritishjim on Thursday August 05, 2021 @01:20PM (#28076709)
> they don't even live in the same problem space despite both having "SQL" in their names.

That's true in theory, but I do find that they end up competing in practice when you're looking for something in between the two extremes.

I often seem to find myself in a position where I need a database, but it's only for one day's data, which is not huge but not tiny (say 100's of MB). It may be helpful to have a couple of processes accessing it, but probably only one will be a writer, and it's very likely (but not certain) that those processes will be on the same computer. It useful to have that data in a single file for archiving. Those are situations where you currently face a geniune choice between PostgreSQL and SQLite - more because both of them are a bit wrong, rather than because they're both perfect for it.

I wish there was something in the middle: a separate program you could spin up, so it's a client-server database like PostgreSQL, but it's just a single binary that doesn't need installing. And then it operates on a single file that it creates on the fly (or can open an existing one of course), a lot like SQLite. Hmm, as I think about it, it actually wouldn't be hard to make program that does that by making a thin wrapper around SQLite with e.g. gRPC.

Re:In Praise of PostgreSQL by laurencerowe on Thursday August 05, 2021 @02:44PM (#28077825)
It's possible to run SQLite on an NFS share (with caveats: file locking must work correctly, WAL mode does not work at all). AWS EFS Elastic Filesystem specifically calls out its compatibility with SQLite. https://aws.amazon.com/about-aws/whats-new/2017/03/amazon-el...

I would definitely not try using this in a write heavy workload but could be interesting to consider in a write occasionally scenario.

Re:In Praise of PostgreSQL by slapfrog on Thursday August 05, 2021 @03:52PM (#28078719)
It's also possible to put a two-stroke engine on a mountain bike and ride it on a limited access highway. But that's really not what the bike is meant for, and would almost never be the right way of going about things.
Re:In Praise of PostgreSQL by marcosdumay on Thursday August 05, 2021 @03:45PM (#28078594)
I did once run SQLite on a Windows SMB share. It works unreasonably well.

At the time I was able to push it into something like 10 simultaneous write accesses (transparently serialized by SQLite). It was an adaptation of a MS Access file that would fail at approximately 5 people opening it at the same time (independently of their actual usage). Luckily I got access to a Postgres installation shortly afterwards so I didn't have to push SQLite any further.

Re:In Praise of PostgreSQL by chungy on Thursday August 05, 2021 @10:07PM (#28082511)
SQLite isn't just for datasets that are tiny. There are a few multi-TB-sized SQLite databases around the world.

The main consideration is typically about how the database is used. "Probably only one will be a writer" is a strong argument in favor of SQLite, which doesn't really have much of a concurrency story for writes (multiple processes can open the same SQLite database for writing, but they implement a block so only one at a time can actually write).

The file locks can be tricky to get working over NFS/CIFS with multiple computers, but it's still possible.

Re:In Praise of PostgreSQL by quietbritishjim on Friday August 06, 2021 @02:14AM (#28083868)
But there are a couple of "probably"s in my comment above doing a lot of work. There is probably only one writer, and it's probably all running on one machine - but occasionally one or both of those isn't true, and suddenly SQLite starts to look less attractive.
Re:In Praise of PostgreSQL by bobthebuilders on Thursday August 05, 2021 @03:24PM (#28078307)
We use H2 Database (JVM only) at work for that. It solves the problem really well of a bunch of stuff that needs access to a single file DB, while still being able to embed the DB engine.
Re:In Praise of PostgreSQL by 411111111111111 on Thursday August 05, 2021 @04:26PM (#28079247)
H2 is a jdbc only in-memory database, so that's only an option for volatile data from/to Java.

I think it's a pretty different use case to what the parent wanted

Re:In Praise of PostgreSQL by mdaniel on Thursday August 05, 2021 @05:23PM (#28080066)
It most certainly is not just in-memory; it can do in-memory, but also has file and server options: https://h2database.com/html/features.html#connection_modes

Exact same story for its HyperSQL and Derby friends: http://hsqldb.org/doc/2.0/guide/running-chapt.html#rgc_hsqld... https://db.apache.org/derby/#What+is+Apache+Derby%3F

Re:In Praise of PostgreSQL by 411111111111111 on Saturday August 07, 2021 @02:47AM (#28096365)
It's jdbc only, with the main usage being in-memory.

Redis is considered an in memory store as well, even though it can persist it's data on disk too. And the capability isn't what makes it in-memory either, as you can store a SQLite db only in-memory too, and nobody would consider it to be an in-memory database. You could technically do the same for PostgreSQL, though that's not officially supported and needs a little effort on the users part.

I guess my phrasing was poor if you misunderstood me there, but I do stand by what I said: while H2 is a great project in Java-Land, it's very different to what I understood the parents goal to be.

Re:In Praise of PostgreSQL by pedrocr on Thursday August 05, 2021 @01:11PM (#28076572)
I'm always amazed at the amazing reviews SQLite gets, particularly in a thread about PostgreSQL. I get the angle of SQLite competing with fopen(). If what you need is a file format you can do a lot worse than SQLite. But I've worked on multiple desktop apps where the only reason SQLite gets used for the internal database is that PostgreSQL doesn't have a mode where you can ship it as part of the app easily and asking the user to setup a database is a non-starter. If not for that the performance benefits of the swap would be very significant. I suspect if PostgreSQL shipped such a mode a very large percentage of SQLite uses would switch over.
Re:In Praise of PostgreSQL by sa46 on Thursday August 05, 2021 @10:27PM (#28082628)
It's possible but tediuos to run Postgres standalone. I packaged Postgres up for our Bazel build which requires a hermetic environment. The hard parts were:

1. Postgres doesn't really didn't want to be statically compiled, according to [1].

2. Messing with LD_LIBRARY_PATH to point to an embedded glibc and openssl when running the heremetic postgres binaries.

3. Postgres will absolutely refuse to run the server process as root. That's sensible for a production environment but a pain in CI because it would require setting up another user on machines. I patched away the problem to allow Postgres to run as root.

On the bright side, with some minor tweaking, Postgres can start a fresh database in 300 ms which is plenty fast for tests and you avoid spinning up Docker containers. I ended up using template databases to only spin up the database once per test suite. Each tests then copies the database template for each test in the suite which reduced the setup time per test to ~20 ms.

Instead of using rules_foreign_cc to build Postgres from source with Bazel, I ended up building Postgres outside of Bazel with Docker and zipping it up by target platform (linux|darwin)_(arm64|amd64).

https://www.postgresql.org/message-id/4E0DE1B6.6070600@postn...

Re:In Praise of PostgreSQL by mleonard on Friday August 06, 2021 @06:09AM (#28085181)
Very interesting. Is any of this open source? I'd love to take a look!

I'm guessing it's not open source - as I couldn't find it on your Github account or your company's account. If that's the case, would you be able to create a quick gist with a copy/paste of any of the code you can share? If you have the time I'd appreciate it!

Related links for anyone interested: - dataform uses bazel for ci tests. They builds redis from source, but run postgres as a container. See here: https://www.reddit.com/r/bazel/comments/kcmbwb/how_to_run_se...

Re:In Praise of PostgreSQL by jvolkman on Saturday August 07, 2021 @03:09AM (#28096460)
We pull Postgres from Nix using rules_nixpkgs for our Bazel tests. It works pretty well. I wrote some additional infrastructure that makes it easy to test against different versions of postgres (11, 12, 13 - all available from Nix) using simple Bazel command line options.
Re:In Praise of PostgreSQL by sa46 on Friday August 06, 2021 @06:43PM (#28093627)
Sure, here's sketch of how it works: https://github.com/jschaf/bazel-postgres-sketch
Re:In Praise of PostgreSQL by mleonard on Thursday August 12, 2021 @04:50AM (#28153765)
Amazing thanks so much!
Re:In Praise of PostgreSQL by anarazel on Friday August 06, 2021 @07:30PM (#28094025)
> 1. Postgres doesn't really didn't want to be statically compiled, according to [1].

Right. There's a lot of features that'll flat out not work if you somehow compile it statically. I don't think it's a useful thing to run tests with a version of postgres built like that. Nor does it really solve anything - postgres will still require a bunch of on-disk files.

You shouldn't need have to do so anyway - you can relocate the postgres installation itself when compiled normally. Binaries like postgres will try to find the files they need relative to their own location if not found at the builtin location.

Of course, libraries that PG binaries dynamically link to need to be somewhere in the library path. If you need to adjust that you can do it by adding rpaths to the binaries/libraries to additional locations, so you don't need to adjust LD_LIBRARY_PATH.

Re:In Praise of PostgreSQL by sa46 on Friday August 06, 2021 @07:41PM (#28094101)
I ended up skipping static compilation for the reasons you outlined (not necessary). I packaged everything up in a directory and used LD_LIBRARY_PATH. I linked to a sketch on GitHub in a sibling comment.
Re:In Praise of PostgreSQL by anarazel on Thursday August 05, 2021 @04:26PM (#28079264)
There's a decent bit of optimizations that sqlite can do by virtue of effectively being single-writer that postgres can't without compromising a lot of other workloads. That's e.g. one of (but not the only) the contributing factors to why the per-row overhead in sqlite is smaller than in PG. And of course something like PG won't be embeddable into the address space of an application, which invariably leads to higher latency (even if it still can be low).

WRT "embeddable postgres":

Postgres relies on having multiple processes that collaborate on making database access fast. And it requires there to be only a single instance of postgres that can access the data. There's some inherent increase in difficulty of embedding something like that compared to something with sqlite's architecture.

Until recently PG didn't have a way to provide non-tcp access on windows, which made embedding on windows a bit more problematic. But since Win 10 unix domain sockets are available on windows, so things have gotten better.

I'd guess that after that the fact that a postgres installation consists out of many files, instead of a shared library or two, is the biggest difficulty. Those files are relocatable at least, but it's still far less convenient.

I don't know how much of a relevant factor the layout of the "data directory" is - for some database embedding scenarios it sure is convenient to only have to deal with a file or two. But moving a directory around isn't that much harder...

My guess is that somebody with interest could improve the situation measurably within a reasonable timeframe...

Re:In Praise of PostgreSQL by donarb on Thursday August 05, 2021 @01:24PM (#28076766)
On the Mac at least, there is PostgresApp. A double-clickable application that wraps a PostgreSQL server. Installation takes a minute or two without having to use the command line for compilation.

https://postgresapp.com

Re:In Praise of PostgreSQL by Tostino on Thursday August 05, 2021 @02:26PM (#28077589)
Completely agreed. I would love to be able to use an embedded version of Postgres. I have no interest in SQLite though because of the dynamic typing, reduced feature set, lack of advanced features, etc...
Re:In Praise of PostgreSQL by marcosdumay on Thursday August 05, 2021 @03:48PM (#28078654)
On Linux you don't actually need to install Postgres. For Windows there is this: https://garethflowers.dev/postgresql-portable/

I agree it could be better packaged, but it's actually not impossible to ship a Postgres with your program.

Re:In Praise of PostgreSQL by SahAssar on Friday August 06, 2021 @06:05AM (#28085163)
> On Linux you don't actually need to install Postgres.

What do you mean? It's not like postgres is bundled by default with most distros.

Re:In Praise of PostgreSQL by marcosdumay on Friday August 06, 2021 @09:24AM (#28086810)
You can run it without installing. You can run almost anything on Linux without installing.
Re:In Praise of PostgreSQL by SahAssar on Friday August 06, 2021 @11:01AM (#28088087)
That's true for all OS'es (except iOS & android), if you have the executable you can run it. Installing usually just consists of placing the executable on a specific path and creating some directories to hold data/config/logs. You also often need to consider the dependencies of the application (unless it's statically compiled), so it's a lot easier to use your distros package manager.
Re:In Praise of PostgreSQL by marcosdumay on Friday August 06, 2021 @11:47AM (#28088790)
That's really not true for all applications, and Windows applications tend to require being installed, while Mac ones are all over the place.

Yes, the OS doesn't impose any opinion. It's just a matter of what application developers do, and the developers of Linux applications tend to write code that doesn't require installing. As an example, there's Postgres :)

Re:In Praise of PostgreSQL by SahAssar on Friday August 06, 2021 @03:46PM (#28091925)
I still don't get what you mean (or exactly what you mean when you say "installing"), Postgres can be a simple executable on OSX and Windows and on linux it is usually "installed" because it needs to setup a data directory, user/group, config directory and so on.

There is nothing about linux that makes postgres more or less portable than on windows or osx, and I'd be interested if you have any examples of postgres being used in a non-packaged way (outside of development tools like postgresapp.com).

Besides that, one of the strengths of most linux distros is that they provide a centralized way to install/upgrade packages like apt/yum/dnf/pacman and don't have to rely on third party tools like homebrew.

Re:In Praise of PostgreSQL by hitekker on Thursday August 05, 2021 @02:08PM (#28077362)
The "mode" you wish for is a rearchitecture of PostgreSQL. Embedded and client-server DBs are two distinct systems.
Re:In Praise of PostgreSQL by 7952 on Thursday August 05, 2021 @04:13PM (#28079052)
Why though? Naively it would be possible to bundle postgres and have your app connect on localhost. What would be wrong with doing that?
Re:In Praise of PostgreSQL by handrous on Thursday August 05, 2021 @05:59PM (#28080526)
You could do it, but you'd be paying (in complexity, resource use, and bundle size) for a bunch of features and optimizations you don't need, and it'd be tuned for a totally different use case than occasional access by one local process.
Re:In Praise of PostgreSQL by pcr910303 on Thursday August 05, 2021 @12:35PM (#28076036)
SQLite is an exceptional piece of software as well…

It’s lightweight, reliable, knows what it’s tasked for, does exactly wat it does, and easy to extend. I personally reach SQLite first in so many cases, including web backends. That the DB is a file that’s literally a cp away to backup is super-useful in development and debugging.

Really the only complaint is that type enforcement is lacking… which I wish I could have said not a big deal, but it turns out it is. I wish I had a embedded DB that’s as reliable and trustful, lightful as SQLite, but with types enforced. :-(

Re:In Praise of PostgreSQL by open-source-ux on Thursday August 05, 2021 @01:25PM (#28076791)
It's worth taking a look at the Firebird relational database.

Firebird can be used as an embedded DB or an client/server DB. It supports a wide variety of data types, is open source and cross-platform too.

It's a database that's rarely discussed here (a mystery why not). It certainly deserves wider attention.

Firebird features: https://firebirdsql.org/en/features/

Re:In Praise of PostgreSQL by kstrauser on Thursday August 05, 2021 @02:25PM (#28077573)
I worked with a company a long time ago that was using Interbase as the backend for a few websites. It had the interesting property that if you were using the command line client to connect to the server, and you used ^Z to pause the client, then the server would hang for all clients until you resumed it.

I know it’s completely unreasonable to judge Firebird today for Interbase 20 years ago, but admit that’s still the first thing that pops to my mind.

Do you feel Firebird offers something SQLite doesn’t?

Re:In Praise of PostgreSQL by open-source-ux on Thursday August 05, 2021 @03:30PM (#28078382)
Nothing beats the nimbleness, simplicity and speed of SQLite, but it is not a client/server database engine (although it is often used for that purpose due to its speed).

Firebird can be used as a proper client/server database engine. It supports static data types and stored procedures (unlike SQLite). It sits somewhere between SQLite and PostgreSQL. So it may be worth evaluating if you don't want the heft of PostgreSQL but need features missing from SQLite.

Re:In Praise of PostgreSQL by zimpenfish on Friday August 06, 2021 @03:40AM (#28084308)
> embedded DB

Sounds like it's not quite as embedded as SQLite? Seems like it just smushes the server into your app and spins that up from within. Also it only seems to be properly "embedded" on Windows (no macOS support and Linux is a bit weird?)

> Finally, you can't just ship libfbembed.so with your application and use it to connect to local databases. Under Linux, you always need a properly installed server, be it Classic or Super.

Re:In Praise of PostgreSQL by password4321 on Thursday August 05, 2021 @12:53PM (#28076317)
Does https://duckdb.org enforce types?
Re:In Praise of PostgreSQL by names_are_hard on Thursday August 05, 2021 @01:03PM (#28076473)
Agreed on all points. I'd also add that I miss table-valued functions in sqlite.
Re:In Praise of PostgreSQL by DemocracyFTW on Thursday August 05, 2021 @06:47PM (#28081056)
you can write your own table-valued, aggregate and window functions when you use better-sqlite3 (see https://github.com/JoshuaWise/better-sqlite3/blob/master/doc... see Database#function(), Database#aggregate(), Database#table()) in JavaScript.
Re:In Praise of PostgreSQL by names_are_hard on Thursday August 12, 2021 @03:47PM (#28160620)
This is really cool, thanks for pointing me to that. I'm still probably going to migrate to postgres because I'd like the functions to exist in the db and not just in code, so I can use them in ad-hoc queries.

But this is worth considering as an option.

Re:In Praise of PostgreSQL by suprfsat on Thursday August 05, 2021 @02:35PM (#28077706)
Javascript does exactly wat it does but I don't see anybody rushing to praise it
Re:In Praise of PostgreSQL by orev on Thursday August 05, 2021 @10:09PM (#28082521)
To CoRecursive podcast has a great interview with Richard Hipp (author of SQLite). Very much worth a listen.

https://corecursive.com/066-sqlite-with-richard-hipp/

Re:In Praise of PostgreSQL by smt88 on Thursday August 05, 2021 @07:49PM (#28081548)
SQL Server and Oracle are also fantastic software. There may not be any good reason to use them today for a new product, but they're far from obsolete.

Oracle has a lot of advanced functionality that no other RDBMS really has, and I do hit sharp edges of PG (especially in unexpected query planning decisions) more than I do in SQL Server.

And, as much as I hate MySQL, I have a few instances that are approaching 10 years old with billions of records that have almost never needed query optimization. I just added indices in logical places and they just work somehow.

Re:In Praise of PostgreSQL by valbaca on Thursday August 05, 2021 @12:32PM (#28076000)
> but that aside strikes me as high praise for SQLite

It's intended to be and is deserved.

Re:In Praise of PostgreSQL by amedvednikov on Thursday August 05, 2021 @03:57PM (#28078811)
SQLite is perfect except for dynamic types.
Re:In Praise of PostgreSQL by beermonster on Thursday August 05, 2021 @01:11PM (#28076568)
SQLite is literally on billions of devices and most people (even tech folk) are none the wiser.