Dear background mind: please think about Postgres and Clojure

I used a Lisp variant in my first programming job, so I have some appreciation for the “code as data” power of that language. Nowadays I’m building an analytics system that combines Postgres and two of its procedural languages, pl/pgsql and pl/python, with a sense-making tool called Metabase that’s written in a Lisp variant called Clojure.

In Postgres I’m able to wrap SQL queries in functions; they compose with other functions that do things like cache results in materialized views and aggregate subgroups. It all feels very dynamic and functional, which are two of Clojure’s main calling cards, so this makes me wonder about Clojure as another Postgres procedural language.

For the pl/python functions in my system, “code as data” looks like building SQL statements that swap variables into SQL templates and combine them. This string-building approach is an anti-pattern for Clojure folk. They don’t want to work with fragments of SQL text, they want to work with Clojure maps. Here’s an example from Honey SQL, the library that Metabase uses to build SQL texts from structured data.

(def sqlmap {:select [:a :b :c]
:from [:foo]
:where [:= :f.a "baz"]})

This seems like an important power to be able to wield in the hybrid programming environment that Postgres provides. I can imagine making very good use of it.

But I know very little yet about Clojure. Is this really an idea worth exploring? How else would it differ from what I’m now able to do in Python? To learn more about Clojure I’ve been watching talks by its creator, Rich Hickey. Most are tech-heavy but one of them isn’t like the others. In Hammock-driven development he lays out a process for creative problem solving that coordinates the waking mind (at the computer doing critical thinking and analysis) with the background mind (on the hammock doing abstraction, analogy, and synthesis). The coordination is explicit: You use the waking mind to feed work to the background mind which is “the solver of most non-trivial problems”; you weight your inputs to the background mind in order to influence their priority in its thinking.

I guess I’ve done that kind of thing implicitly from time to time, but never in such an intentional way. So, is Clojure-in-Postgres worth exploring? Perhaps by writing this I’ll prime my background mind and will receive more clarity in the morning.

Posted in .

2 thoughts on “Dear background mind: please think about Postgres and Clojure

  1. Hi Jon,
    Interesting that you are talking about Clojure-in-Postgres. The first I heard about Clojure was when I’m trying out Logseq.com alpha web app (“a privacy first, open-source platform for knowledge management”). They are written in Clojure and Clojurescript. They do use in-app databases but I’m a bit unclear which one. Logseq just released the first beta desktop app application.

    1. I’ve also noticed a few significant Clojure apps appearing in the note taking space such as RoamResearch and its clones.

      I’ve dabbled in Lisp for decades (didn’t Jon write about iLisp in Byte?) but Clojure is the first one I’ve been able to put to use in real applications. It has a very practical philosophy of leveraging the host language while improving it with FP and immutability.

      love it.

Leave a Reply to sabre23t Cancel reply