Building PostgreSQL for the Future with Heikki Linnakangas

20 May 2025 · 42 min

Ask about this episode

Ask anything about it. ChatGPT or Claude reads this page and answers with the times it was said.

Connect VO and ask about every podcast you hear, including the moments you saved. Add to ChatGPT · Add to Claude

In short

Podcast Episode Notes: Building PostgreSQL for the Future with Heikki Linnakangas

Podcast Overview

  • Title: Software Engineering Daily
  • Episode Title: Building PostgreSQL for the Future with Heikki Linnakangas
  • Host: Kevin Ball
  • Guest: Heikki Linnakangas, leading developer of the PostgreSQL project and co-founder at Neon.

Episode Summary This episode features a discussion about the PostgreSQL database, its increasing popularity, and future developments. Heikki Linnakangas shares insights from his extensive experience with PostgreSQL and elaborates on his work at Neon, a serverless platform for PostgreSQL databases. Key topics include PostgreSQL's extensibility, the impact of AI applications, and ongoing innovations in database technology.

---

Key Concepts and Discussions

  1. PostgreSQL Popularity
  2. Robustness and Extensibility:
  3. PostgreSQL is recognized for its stability, feature set, and compliance with SQL standards.
  4. It has evolved to become the default choice for many developers, surpassing MySQL in popularity over the years.
  • Ecosystem Advantages:
  • A broad ecosystem of tools and community support has contributed to PostgreSQL's popularity, making it easier for users to find solutions to common problems.
  1. Extensibility of PostgreSQL
  2. Custom Data Types:
  3. PostgreSQL allows developers to define custom data types, operators, and indexing methods, which enhances its flexibility and adaptability for various applications.
  4. Example: Heikki describes creating a color data type with unique operators for comparisons.
  • Importance of Extensions:
  • Extensions such as PostGIS (for geographic data) and PG Vector (for AI applications) showcase PostgreSQL's extensibility.
  • Extensions allow for rapid innovation without burdening the PostgreSQL core.
  1. Innovations in PostgreSQL
  2. Future Developments:
  3. Upcoming features include asynchronous I/O and multi-threading to enhance performance and scalability.
  4. The need for a balance between core features and extension capabilities is essential for PostgreSQL's continued evolution.
  1. Neon: A Serverless PostgreSQL Solution
  2. Architecture:
  3. Neon separates compute and storage, enabling rapid PostgreSQL instance spin-up and point-in-time recovery without traditional backup systems.
  4. The storage layer retains the write-ahead log, allowing reconstruction of data from any point in time.
  • Advantages:
  • Reduces backup complexity, enabling users to query data as it existed at any previous state.
  • Supports scalability by allowing instances to scale independently and quickly.
  1. Challenges and Future Directions
  2. Connection Management:
  3. Current challenges in PostgreSQL revolve around connection management in cloud environments, and the potential for multi-threading could simplify this issue.
  • Long-Term Outlook:
  • Heikki emphasizes the need for ongoing innovation and flexibility to maintain PostgreSQL’s relevance amidst emerging database technologies.
  1. Community Involvement
  2. Getting Involved:
  3. Heikki encourages developers to contribute to PostgreSQL, particularly through extensions or bug fixes, as involvement can lead to rewarding experiences.

---

Key Takeaways

  • PostgreSQL’s success can be attributed to its stability, extensibility, and strong community support.
  • Extensions play a crucial role in PostgreSQL’s growth, enabling rapid innovation while keeping the core stable.
  • Neon offers a compelling serverless solution that simplifies data management and enhances recovery options.
  • The evolution of PostgreSQL will depend on addressing existing challenges and embracing new technologies such as AI and multi-threading.

---

Conclusion The episode provides an in-depth look at PostgreSQL's journey, its strengths, ongoing developments, and the innovative solutions offered by Neon. The discussion highlights the importance of community involvement in the open-source ecosystem and the exciting future that lies ahead for database technology.

Written by AI. May contain mistakes. Listen to the episode to check what was said.

Hear the part that matters, and keep it.Open this episode in VO. Double tap your headphones to save a moment as you listen.
Get VO free

Transcript

Automatic transcript. May contain errors.

0:00PostgreSQL is an open source database known for its robustness, extensibility, and compliance with SQL standards. Its ability to handle complex queries and maintain high data integrity has made it a top choice for both startups and large enterprises. Heike Linakangas is a leading developer for the PostgreSQL project, and he's a co-founder at Neon, which provides a serverless platform for spinning up PostgreSQL databases. In this episode, he joins Kevin Ball to talk about why PostgreSQL has become so popular, why he founded Neon, PostgreSQL Core vs. Extensions, the PG Vector Similarity Search for AI Applications, and much more.

0:42Kevin Ball, or KBall, is the Vice President of Engineering at Mento and an independent coach for engineers and engineering leaders. He co-founded and served as CTO for two companies, founded the San Diego JavaScript Meetup, and organizes the AI in Action discussion group through Latent Space. Check out the show notes to follow KBall on Twitter or LinkedIn, or visit his website, kball.llc.

1:17hey key welcome to the show thank you kevin i'm excited to get to talk to you so let's start a little bit with about you so can you introduce yourself and your background and what got you to the point where we're talking today so hi my name is heiklin nagangas i've been working on Postgres for the last 20 years for different companies. Currently, I'm a co-founder of Neon, which is a cloud provider for Postgres. Before that, I worked for Dreamplum, like an open source analytics platform built on Postgres and many other things. But currently, I'm working on Neon on serverless Postgres and cloud.

1:53Yeah, I have been very heavily on the Postgres train since probably the mid-2010s. I sort of grew up on MySQL, but then at some point, they seemed to really fall behind and Postgres kept going. And honestly, these days, I feel like there's all these new specialized VectorDBs and specialized DBs. And I'm like, why not just Postgres? It's a high bar. Yeah, I think that's a bit of a meme nowadays, like just use Postgres. It's kind of funny because I started around 2003, I want to say. And back then, it was not a given that people would use Postgres. Like MySQL was made more popular. And you kind of had to explain to people, first of all, what is Postgres?

2:35And then next you had to explain why would you use Postgres rather than MySQL or something else. Like proprietary databases were much bigger back then as well. But over the years, something changed. And you don't have to explain that anymore. It's become the default, which is funny. It feels strange to me because it used to not be that way. But nowadays, people just take it for granted that people are using Postgres. What do you think gave Postgres such both longevity and why has it become the default? That's a great question. I've thought about that myself. To be honest, I don't know, but I can speculate.

3:07I think Postgres has done really well. It has a reputation of being very stable and it has a good feature set, but it's been around for a long time and it's stable and predictable. And I think a lot of the credit for that goes to the packagers and the whole release management teams that we've been doing regular yearly releases, annual releases for a long, long time now. And it's like very predictable, the schedule and what's new, all the upgrade process, all of that. It keeps improving, but it's a very stable and predictable process. At the same time, proprietary databases, well, proprietary software in general has become less popular.

3:42People are moving to open source increasingly. and in the open source ecosystem, MySQL have had their own problems with the versions and there's been confusing in that world and then other databases never really became that popular for some reason. But then there's, of course, technical reasons like Postgres has a big ecosystem of all kinds of tools and a lot of things that kind of come with the fact that it has become popular and the default. Like a lot of people are, like there's a big ecosystem and if you have a problem with Postgres, you can Google that easily and you will find 10 blog posts explaining the same error message that you are seeing and how do you fix that?

4:16So it has reached kind of the point where there's a snowball effect going on. But because it is popular, it has advantages simply because it is popular. Yeah, that momentum effect is there. One of the things that stands out to me actually thinking about it is also extensibility. I feel like Postgres has been able to not just grow in the core, and it has grown in the core. When document stuff started to get big, we said, okay, we've got JSONB, like, let's go. but the custom types and i'm kind of curious like actually for folks who have never played around with that maybe we can talk a little bit about that extensibility like what does the extensibility story for postgres look like that has actually been like if you go all the way back to the university project 30 years ago when postgres was born that was at the core of postgres already back then that the idea of extensibility and especially with the data types postgres has a very flexible type system, you can create your own data type with its own functions.

5:13And not just functions, but also operators on how you integrate into the different index types. And you get to define your own definition of ordering for your data type. And what does it mean to sort it? Or what does it mean to, you know, you can create your hash functions. Many other databases have a few basic data types, strings and integers, for example, and everything is either a string or an integer with some sugar on top. But Postgres goes a lot deeper in that. Even all of the built-in data types are created using the same primitives of operators, operator families, operator classes, and how do other operators work together.

5:46And you get to define all of that yourself. Remember, I wrote for a presentation once a data type for indexing colors, for example, or to work with different colors. It was a toy, but it's really interesting to see that you can create a data type for colors, for example, and then you get to define your own operators. What does it mean for yellow to be greater than blue, for example, or whatever operators make sense. And then you get to build your own indexing system for that. You can build, similar to geographic indexes, you can build indexes on which colors are closer to each other. And you get to define the distance function and all of that.

6:19So yeah, the type system is really very flexible. A lot of the credit goes all the way back to the university at the times. That was the whole idea of Postgres, even before any of the other stuff was like while logging out all that was even created yet but then we've had these extensions post gis has been really important for that like that's been around for a very very long time and postgres has been kind of the default for geographic applications even longer than it has been for other data for like other applications because of post gis and the fact that post gis was built as an extension and it has a different license it's gpl licensed so it was never going to become part of the core because of the license issue, if nothing else.

6:58But that has kind of forced a nice dynamic between the communities too. It's like we're all friends and we talk to each other. So whenever there has been a need for a new kind of an indexing or some kind of support for indexing geographical types, we've kind of designed the core features for what does it mean to have defined these things. And then on the post GIS side, they've implemented the implementations of the data for the geographic types. But that does play really well. Like another very popular extension nowadays, recently, thanks to all the AI stuff, is PG vector. So that's become really popular in the last couple of years suddenly.

7:31But the fact that, why is it possible to create a PG vector extension? It all plugs into the same extension points that we have had for PostGIS and other data types. And it just plugged in really nicely there. That makes sense. And it is interesting how having that external body be such a big part, actually, of Postgres's early growth forced you into that. Yeah, I think that's really, really helpful. And the fact that it was a different license, there was never really any question of, should it be part of the core? It was not meant to be, but that has kind of driven the way we think about extensions.

8:05And in general, like it's a good thing to have extensions and we don't want to have all the different data types in core. Like they have a more happy life outside of the core as an extension. How do you think about what should live in core and what should not? That's a great question. People have different opinions. my opinion is that anything that if there's a reason it needs to be part of core then it can be part of core but for most new things it's better if you can have something else than extensions have a lot of advantages like you can have your own release schedule you get to decide your own releases you get to decide your own other external dependencies something like post for example depends on a bunch of other libraries that we would if we would rather not have a dependency on core postcards on those libraries anyway.

8:48But similarly, if you're building a new data type nowadays, you get to decide which other libraries you use, for example. You don't need to ask for permission for anyone, you just do it. And I think that's really powerful. That allows you to move much faster. Absolutely. There are reasons. People do suggest or want to include prior stuff in core, and we have to have discussions like, why should this be in core or why not? I think that dynamic is shifting a little bit. One of the reasons why people want to have extensions in core or move these things into core is that they want to have the same level of support and the same kind of they want it to be in the core part of postgres for whatever reasons but i'm as a developer i'm always pushing back on that because that basically sounds like someone is dumping a lot of work to me for me to to maintain all that stuff and i don't want that i want to have less things to maintain yeah for sure so i mean i think this balance is always tricky right and you've got as you say if you're outside of core you can move much faster you can explore things much quicker.

9:45You can use a lot more third-party dependencies. What types of innovations are still in progress for Core? Where do you think the Core of Postgres is developing? Well, there's no central roadmap. We know what's coming in version 18, which is released in autumn, in September, probably. There's going to be asynchronous IO. That's the big feature I've been keeping an eye on. I haven't done much work on it except some reviewing, but That's the Andres Freund and others from Microsoft have been leading that effort. But that's going to bring big improvements to the I.O. characteristics of Postgres. One thing that I've been working on a little bit is multitreading.

10:22I kind of raised the flag on that, like, let's get multitraded and that has hit social media and stuff. I haven't done much work on actually making that happen, but I think we are in a place where there's agreement that that's where we want to go. And that's kind of the first step in establishing that it's desirable. That's something we want to happen. And I'm hoping to spend a lot more time on that for the next release, actually. But yeah, there's no central roadmap for Postgres. So it always depends on what are the stuff that people submit patches for, what are the things that people and companies who are contributing decide to work on.

10:55Yeah, that makes sense. Well, and let's talk, you mentioned briefly PG vector, right? And I know that whole space of vector databases and storing embeddings and all of these different semantic search layers and things like that is very much the hot topic today. And I think you worked quite a bit on PG vector. Is that correct? Yeah, I have contributed a little bit to that. So what is in PG vector? What does it take to run it? And how does it stack up against, say, for example, a dedicated vector database? I mean, it uses the same algorithm as many other vector databases, HNSW and IVF flat. Like those are the two algorithms that PG vector implements.

11:33And that's pretty much the state of the art. Like there are some other algorithms with slightly different trade-offs. There are other implementations of the algorithms that can be faster or slower, but it's roughly the same algorithm that everyone is using. So I think PG vector is stacking up okay. It could be a little bit faster. It could use less memory. There's always improvements to be made, but it's doing okay. But the thing with all of these algorithms, in my experience, Coming from a traditional database world, I haven't done much with AI, but what I have done, when I started to look at PT vector, I started to look at it from the point of view of, I know indexes, I know how post GIS works, I know all the other index types.

12:09Let's look at this new thing. How different can it be? And it turns out that, yeah, it has some vector data is slightly different. Vectors are large, first of all, and building in these indexes is very slow compared to traditional B3 indexes or other indexes. That was a bit of a shocker, just how expensive and CPU intensive these workloads are. Another thing that is striking is that there are no good disk algorithms for vectors. So all of the algorithms pretty much depend on you have to fit your workload in memory, which puts kind of a cap on upper limit on how much data can you deal with. And the big differences between many of these algorithms and implementations is actually in how much you can compress it, like how much lossy compression can you do.

12:54Some of them, you can compress the vectors all the way down to one bit per dimension. And it works surprisingly well. And there's like all of the new research that is happening is happening on how do you just make the data smaller so that it becomes faster to process and you can fit more of that in a RAM. So that's very different from traditional data types that I'm used to dealing with. Yeah, that makes sense. And then are there any ways that PG vector is different from some other custom type? Or does it essentially end up looking to Postgres the same as PostGIS or something else? It looks exactly the same.

13:30So that's the interesting thing. There are, and this is something we haven't solved, but something that is interesting with the vector algorithms is that they're all approximate. Whenever you're doing a search on a vector index, it's always like the thing it does, like the thing that all of these algorithms do is approximate nearest neighbor search. And whenever you hear something approximate, that's kind of a red flag to a SQL developer. Like the databases are supposed to be very exact. And if you do select star from a table, you don't expect to get approximate results. You expect to get exactly the same data set, what you inserted.

14:04But that's not what these algorithms do. They are always approximate. They lose not that much information, but they do lose some information. And whenever you then do the search and you pick the top 10 results, for example, it's not deterministic which results you get. And that's fine for simple cases, but that actually throws off some of the rest of the system. Like if you're using pgVector together as part of a larger SQL query, for example, you would do a vector search and then you would filter that or whatnot. Then it gets complicated because suddenly the kind of the lossiness of the query propagates all the way to the other stuff.

14:38If you fetch the top 10 results and then you get the different results and then you filter them and then you sort them and you get different results than you would otherwise, for example, it controls the rest of the query and it can even cause errors when the ordering doesn't match what the planner expects, stuff like that. There are rare corner cases, but it can happen. So that's something that we still haven't figured out. How do you represent that kind of approximate query results from these operations at SQL level? I'm not aware of anyone who has a good solution to that. I would be all ears.

15:11And maybe this is something that needs to go all the way to the SQL standard or something, where you would kind of define the semantics of what does it mean to get approximate results. Yeah, that's fascinating. Because I think, yeah, in a lot of cases, you might have that as a separate data store. And so you don't have to worry about how is that combining then with the SQL place. It's up to the application developer. They decide. Yeah, exactly. You have to figure it out yourself. Yeah. Yeah. So that kind of leads to an interesting question. So you know, this is a type of bug that can occur. How do you deal with that inside of Postgres?

15:43Do you sort of look for it and raise an error? Do you just return incorrect results? Like, what does that look like? Yeah, it depends. It depends on how exactly it happens. It can lead to incorrect query results. Well, they're kind of all incorrect. Like, this is approximate search. Yeah, what does correct mean in this world? Right, what does correct mean? Like there's different levels of correctness and how do you measure that? None of that is well-defined. There are ways you can work around it. Like you can have in the SQL syntax, you can kind of work with it the same way you would with an external vector database and do one query for the approximate part and then do the rest of the query.

16:18Usually the results of that, you can put the barrier with the width clause or something to kind of force the planner to make the plan a certain way. So that's one workaround. But if you don't do that, then yeah, those errors can propagate. And it depends on what the rest of the plan looks like. If it's a merge join and you'd get the different ordering for the results, for example, then there's actually checks like sanity checks in Postgres where if the result said that was supposed to be ordered is not, you will get an error. But in other cases, you might just silently get the incorrect query results again for some definition of incorrect.

16:50Yeah, it is a fascinating world we're in right now. So looking at this and looking at sort of where Postgres is going and how it's being used actually brings me a little bit towards Neon. So can you actually share what was the motivation behind starting Neon? So the way we started, we looked at the architecture of Amazon Aurora, and we decided that we want to build something similar, but make it open source. That was kind of the starting point. Now, along the way, of course, we've come up with new ideas and other stuff, but that's still kind of the core of what we do. And the core is the separation of compute and storage.

17:26So the idea is that there is a separate storage system which keeps all of the history. Like that's the interesting part about Neon and that's different from Aurora. At least they don't expose it. But the way the Neon storage works is that it takes the write-ahead log from regular Postgres and it keeps all of the history and it kind of allows you to do point-in-time recovery and it replaces your regular backups and while archived that you would normally use with the Postgres setup. It kind of integrates and replaces all that with the Neon storage, which keeps all of the history. And that allows you to do stuff like point-in-time query or launch multiple reader replicas against the same storage without having to make multiple copies of your data.

18:05Things like that. And that allows us to scale the storage layer separately, like independently. And then there's the compute side. And the compute for us means basically Postgres. and Postgres connects to the specialized Neon storage instead of the local disk. So this idea of separating the compute and storage, that was at the heart of when we founded Neon and what we started to work on. And it's still at the heart of everything we do. All of the features we have, branching is a popular one that a lot of our paying customers are using Neon because of the branching functionality. Well, that's possible thanks to the storage because the storage allows us to do that branching.

18:41We are serverless. Again, that's the big reason why we can be serverless. It's that we can launch Postgres very quickly. And the reason we can do that is because we have separated the storage. So it all kind of ties back to thanks to the storage engine that makes all of the other stuff we do possible. Yeah, this concept of a serverless database is fascinating because as the world has gone towards, okay, serverless and stateless services and all these things, like the most heavy inertial piece that's hardest to do that for has always been where your data lives. because data is persistent. Data has to be persistent in order to be there.

19:17And if I understand correctly, you're saying, okay, great, but let's sort of cut the tightest barrier possible around that thing that has to be persistent and make everything else serverless. Exactly. So yeah, that's exactly what we do. So we made even Postgres serverless by pushing down the thing that needs to have the state, which is the storage, kind of push that further down the stack and separate that as well from the compute side of Postgres. Yeah, as you said, the data has inertia and it doesn't move easily. It's not serverless. It doesn't just, you can't just suddenly come up with data from thin air.

19:50You have to actually store it somewhere. So how fast can you spin up Postgres if your data is separated off in Neon? What is that response time on a serverless Postgres? So we measure it internally and it's about 700 milliseconds at the moment, I think. There's a little bit more that the user pursues because handshake to the client and so forth. That's a little bit, but we tried to keep it under one second. When we started, another part of how we make this work is that we run the Postgres instances in Kubernetes cluster, and we launch a VM, a separate virtual machine for every Postgres instance.

20:24When we started, the delay of that was about five seconds to launch a new Kubernetes pod and connect your connection to that. And we kept hearing from users like, yeah, Neon is awesome, but man, the cold start time, that's too long. Like, that's killing us. That's too long. So we kept hearing about that and we've started to work on it based on the feedback we got. And we have plans to bring it down further and further. The thing that made a big difference is pre-creating these VMs. So we have a pool of pre-warmed VMs available at all times. And we got it down to about one second and we stopped hearing these complaints.

20:58So that was kind of the tipping point where people stopped complaining. And that was really interesting to see. We still have plans to bring it down even further, but it's not really a problem anymore. Like that seems to be that people are happy with the roughly one second delay when you connect for the first time. Got it. So that's low enough that you can essentially spin this down if you don't have requests coming in, but it'll stay hot so long as you're coming at it. It's not quite the same thing in some ways as a like serverless function that really is lifetime is only the request. Well, a serverless function has the same thing.

21:31The function lives somewhere. There's data, like the code is somewhere. And there is a delay the first time you call a serverless function as well. And then it gets loaded and so forth. It's not as high. Typically, that's even lower. But we also have plans to bring it down even further. But the pain point seems to be somewhere between one and five seconds where people stopped complaining. That makes sense. So I'd love to dive into what you're doing in the storage layer. because I was researching this a little bit ahead of time and found it completely fascinating. So can we maybe just start with high-level architecture, which I think you laid out when you were starting Neon, or at least I saw it in the blog post.

22:10But what does this minimum viable encapsulation of your heavy inertial data look like? And how does it enable these things like point-in-time queries and all of that sort of thing? The kind of abstraction we have is that we store all of the writer head log of what Postgres produces. It's the same writer head log that people use normally with Postgres for point in time recovery, backups, while archive, replication, all of those features. So all of this is based on the same writer head log. And the basic idea is that the storage takes the writer head log stream from Postgres and processes it. And it transforms it into a different, like reshuffles the data into a different format.

22:53Based on the writer head log, it can reconstruct any version of a page so postgres always requests a pay like all of this works at the page level at the block level it works with the eight kilobyte blocks that postgres always uses but whenever postgres needs to read a page instead of reading it locally from disk it sends a request to the storage get page number one two three at this point in time and the kind of this time dimension is what makes all of these other features possible so the storage layer can reconstruct any version of any page. So if the page has been modified a hundred times, it actually keeps all of the hundred versions of the page available and it can return any version of those pages depending on what the request is.

23:38And you can kind of see where, you know, that's the thing that enables us to do things like having a read replica that's lagging a little bit behind the primary. It will just request all of the pages at the slightly delayed point in time or it replaces a point in time recovery because you can just launch a new compute node on your Postgres instance, and you can just tell it to, please show me all the data at this older point in time. And the storage layer can do that. Now, there's a lot of complexity and a lot of smart engineering that goes into the storage to make it possible to do that. Like actually keeping every version of every page is very expensive, obviously.

24:16So there's a lot of smarts in how it stores the data, and it doesn't literally keep all of the versions, but it keeps the writer head log and some images of the pages at specific points in time so that it can quickly reconstruct any page based on the writer head log and the images it has. This sounds to me like essentially an event sourced model, if I'm understanding correctly. Yeah, that's one way to think about it, yes. Where the events are the write-ahead log. It's saying, okay, this changed in this way, this changed in this way. and then it's projecting out here's the state of data at some point in time and you save those images essentially and so any point in time becomes the most recent image before it plus a set of deltas right yeah that's one way to think about it in an event sourcing system that's it's up to you to define what do you do with the events and how do you collapse the events into the final image of whatever whatever you have in this case it's the pages and the page images so postgres never needs to know about any of the stuff that's happening behind the scenes.

Read the full transcript

25:18Postgres can just request a page and it will get a page and all the storage does all the magic to reconstruct that. Yeah. Postgres can stay blissfully unaware of the events underneath the surface. And it's just, what is my state? That's really interesting. So you highlighted at a point before around the importance of having data in memory for certain index types or other different things. Like, what does the memory hierarchy of this system look like? Are those servers that are responding to pages? Like, how much are they keeping in memory? Where are they falling back to? Like, what does this look like?

25:51Starting from the top, like from the fastest tier, well, I guess there's even the CPU caches and registers and so forth. But the way I think about it is that at the top, there's the Postgres shared buffer cache, which is the same buffer cache that Postgres always has. We actually configure that buffer cache to be very small. I think we set it to 128 megabytes, regardless of the size of your database and regardless of the instance size. And the reason for that is flexibility. So that allows us to easily scale that up and down, because it doesn't allow you to change the size of that. So we kind of set it to the minimum that we can get away with.

26:26Then there's the next level of cache, which is something we call the local file cache. And that's a Neon-specific thing that we built that just uses a local file as a kind of second level cache. So if the page is already in that cache, then you don't need to go and request it from the storage. We can return it from locally. And that makes it possible for us to have such a small shared buffer cache that it kind of allows us to overflow that. Then the next step is that whenever there's a cache miss from that, now you have to go to actually go to the storage and you have to go to what we call the page servers, which is that's the service that does all the reconstruction of the pages we talk about.

27:03So now you have to actually do a network request. You have to go over the network, make that request, and then bring it back. There is caching involved within the page server as well for various things, but that's not very significant to the latency. We pay a lot of attention to the latency of all of this because that easily adds up. But as soon as you have to go over the network, the other latencies don't matter so much. So we spend a lot of time making sure that there is caching that happens in Postgres and that we do prefetching properly. That's a very effective way of hiding the latency. then the stack goes even deeper than that.

27:37So the page servers actually don't, they're not the final source of truth either. The page servers are actually just a cache of what we store in object storage, like Amazon S3 or Azure Blob Storage. So that's where the object storage is what ensures that we don't lose data in the long run. So if one of the page servers goes down, we just launch a new one and it will download the files from the object storage that it needs on demand or beforehand. So there is a pretty deep hierarchy of caching involved. Yeah, absolutely. So that's fascinating. So you pay very close attention to the different latencies involved.

28:14So what does that look like? One network hop, and then if you have to go all the way down, say you're accessing something that's completely cold, coming out of the object storage, how much does that add to your query time? Yeah, the first time you have to do a request and download this data from object storage, that's hundreds of milliseconds or up to a second or two seconds even. So that's very slow, but we do try to keep things cast on the paid server size so you don't see that latency. You can also hide that a little bit. We do offload the stuff and remove it from the hot storage for databases that haven't been accessed for a long time.

28:47I don't remember what exactly that time is. But if you haven't used your database for weeks, then you're probably okay with a few seconds of latency on the first hit. The first time you actually access it again. And those downloads tend to happen in big batches. So it's not like you have to pay the one second latency for every page. It's only really for the first few pages. And then it gets cached again. Nice. You talked a little bit about some of the benefits that this gets you, but let's maybe dive a little bit more. So you said you don't need your whole backup service. Is that because it's all going to object storage anyway, which is already dealing with all of that?

29:20Or how does that work? Yeah, that's correct. So all the data ultimately goes to object storage, and that's what ensures the durability. There's actually like two pieces that take care of the durability. For the recent stuff, like recent modifications, we have a service called the Safekeepers that makes sure that we don't lose the recent transactions because you can't stream data to the object storage. So that's the thing. That's the service. We keep like three copies of the recent while, the recent writer head log. And there's a query algorithm there based on Paxos, which makes sure that when you commit the transaction and we respond to the client that, okay, okay, the transaction is committed.

29:57We don't lose the recent transactions. But that's only for the recent stuff. So pretty quickly, the data gets processed by the page servers and uploaded to files on object storage. And that's ultimately what ensures that we don't lose the data. That's awesome. So to Postgres, all of this just looks like a file system? It doesn't have to worry about it? That's right. We have to modify Postgres a little bit to make this work because there was no extension point for this, unfortunately. It's a very small patch. It's a tiny patch to hook into the, very close to the functions where Postgres normally does, like read a page from disk or write a page to disk.

30:34One interesting fact about this all is that it's all based on the writer head log and we reconstruct the pages from the writer head log. So whenever Postgres writes out a page from the buffer cache, we actually just throw it away. We don't need it. So there is no, you know, because we can always reconstruct the data from the writer headlog, just like you would when you're restoring for backup. So in a way, we are continuously all the time restoring the data from the writer headlog. It's interesting to me because I remember when I first looked into how Postgres does replication and all these different things, I saw this writer headlog and I was like, oh, under the covers, it is like this event sourced model, but it's just continuously creating like the one projection or the one like true image of it.

31:17You're essentially saying, okay, well, yeah, let's take advantage of that. Let's hook into it. Let's put it over here. We can ignore Postgres's attempt to keep a safe image. We've got that handled. Yeah, the write-ahead log is the data. Like, that's the important thing. Yeah, absolutely. You said you did this wanting to do something like Aurora that is all open source. So is the Neon storage layer open sourced as well? Yes, the Neon storage layer is all open source. Wow. So what else goes into Neon that is making this work? Right. Well, there's a lot of, like, all of the storage and all the things that we talk about.

31:51But so far, it's roughly half of what the company does, like how roughly half of the engineering effort goes into all that. The other half is all the other stuff like the website, the control panel, billing, all kinds of stuff and user-facing APIs, dashboards, managing the whole cluster. I mentioned the Kubernetes cluster. There's a lot of stuff to make all that work. Oh, and auto-scaling, like scaling these computer VMs up and down, moving them around, just managing the cluster and managing the whole thing. whole service and all of the serverless aspects. Got it. So if you were to say, again, about 50 % is like business stuff, essentially, what it takes to keep the business going.

32:31And 50 % is open source, Postgres, Neon. If somebody wanted to take the Neon storage system and run it somewhere else, they could just go and do that? Yeah. And I would love that. People try that every now and then. And I encourage them. It can be a little bit tricky because it's not a product we sell like the open source thing we built this thing so we can run our service so we we don't do tagged releases and things like that so it can be a little bit hard for other people but there are people always doing that like there we and we have gotten some contributions to like docker images for people to run run that on their own and i welcome that like i love it when people are doing that yeah well and i think it speaks in a slightly different area to sort of the openness of the Postgres community, right?

33:18That you can do this and have it be open and connect into Postgres without needing to do too much. You have a patch, but it's, I assume, also open source. Yes, for sure. That's super cool. So I guess then the question I would come to from this is, where are things going? What is the evolution of Postgres and kind of cloud databases looking like in your mind? Yeah. Working for Neon, I have things I want to work on for Postgres or for upstream Postgres. People might have different opinions. But the thing that strikes me is that I would love to see much more flexibility in Postgres so that it's easier to run in this kind of serverless environment, not just Neon, but for anyone who wants to run it in cloud and scale it up and down.

34:04We have had a lot of friction with connection management. For example, if you want to have thousands and thousands of connections, you need to have a connection pool. And, well, there's a lot of connection poolers out there in the ecosystem, and they have slightly different trade-offs. And people have found the workarounds, and there's thousands of blog posts on how to do all that. But kind of at the core, having to deal with all of that is a bit painful. Some other databases do a lot better, to be honest, with just having lots of connections open and dealing with that problem internally. For Postgres, that's always a bit messy.

34:38It comes down to things like memory management. If you have a lot of connections, now they're competing for memory. Every connection has its own caches for queries and stuff like that. So it kind of adds up. So that whole story is a bit awkward. So I would love to somehow address that. And that's one of the reasons I wanted to start on working on the multi-threading is that it will... Just switching to threads won't fix any of those other problems. But it makes it possible to start having more shared caches. It makes it possible to resize these certain memory areas more easily. I think in the future, five years down the line, maybe, that we will start to reap the benefits of that.

35:16And then we can have more flexibility. Yeah, I think that's always one of the challenges with a really long-lived project is you get these big architectural choices that were made, in Postgres' case, 20 years ago. Yeah. And you get to a point where they're limiting you. But you've got millions of users. You can't just rewrite it. You've got to very gradually shift things forward. So maybe walk us through in your head what that looks like for, for example, multi-threading. Like what does it take to make such a huge architectural change that's going to set you up in five years to be able to reap those benefits?

35:51Well, there's a long list of to-do items on a wiki. So what do we need to do? There are some core components that need to be refactored. Like the first step is to refactor things so that it's easier to use either threads or processes. Because this is not going to happen within one release. We're not going to just switch over. So we will need to have a plan where we can comfortably live with threads or processes for at least a few releases. I would love to keep that transition period as short as possible. But realistically, it's going to take years. One aspect of that is, again, the whole ecosystem we have.

36:24There are tons of extensions out there. Even if we do all the changes we need in core, the whole ecosystem will have to be dragged along. And we need to make it as easy as possible for them. I actually think the ecosystem like extensions might actually move faster because it might be a lot easier to write some extensions in a multi-character environment to begin with. So I think we will pretty quickly start to actually see extensions that only work when you're using the threads, even as soon as we get that feature out there. But yeah, it will take years and there's a lot of refactoring that needs to happen.

36:55And then we'll need to define the user visible. How do you choose? How do you configure this thing? And hopefully we won't invalidate much of the conventional wisdom of how do you set up Postgres. And the goal is that it would not be, it should have roughly the same trade-offs, at least in the beginning. But then once we get to the point where we're going to start to remove the stuff that requires processes and kind of go all the way in, that's when we're going to start to really reap the benefits and we can start to rely on having threads and the shared address space. is databases feel like an area where there's been a lot of noise recently about new approaches and different things.

37:34We alluded a little bit to vector databases and we had the whole boom of key value stores and then, oh, now that we can do distributed and still maintain SQL guarantees and asset guarantees and things like that. What do you think is going to keep Postgres as the main choice for developers? Or what are the risks that it's not addressing that something else might be able to come in and take that crown? I mean, nothing is forever, but Postgres has been around for a long time. And I think it will stick around for a long time still. There's a lot of new projects that are choosing to use the Postgres syntax.

38:11There's a lot of projects that are choosing to use the wire protocol. There's a lot of projects that are choosing to use bits and pieces of Postgres, even if there are completely new implementations of a completely new system. So I think there's some staying power in that, like just being the lowest common denominator between all of the forks and all of the different approaches. That's one way. But Postgres is still alive and kicking. There isn't really any serious competitors, I find. There's a lot of competitors for niches, but there's nothing that is taking over that I see. And there's no reason.

38:42It's a similar story with the Linux, for example. It's very dominant. The fact that it is open, there's no particular need for anyone to compete directly with that. You can just join the project. Why compete when you can join the project? And Postgres is similarly very open. It's not dominated by any single commerce or vendor. There's a true open source ecosystem around that. So if someone wants to do something cool, some kind of a new approach, they can just use Postgres for that. And that's how it will keep evolving with the times. So we've covered a lot of ground. We've talked a lot about Postgres.

39:16We've talked a lot about Neon. Is there anything we haven't covered yet that you want to make sure that we talk about before we wrap up. Talk about the future of Postgres. That and the whole open source ecosystem, like that really depends on what people come up with. So if someone is out there thinking that, hey, I have this new cool algorithm or something, please submit it to the Postgres community. The review process can be long and tedious, but there's a lot of people paying attention. And if it's, some things get done very quickly, depending on what it is. I guess that is one thing that might be worth diving into.

39:47I feel like in the last few years, the vast majority of growth in the software engineering world has been really at the application layer. Lots and lots of new developers jumping into applications. Getting involved with a database project feels very intimidating. How would you recommend people approach that? And why should they be looking? I mean, I think that's the other thing, right? We've got these new sexy AI development tools. We're doing apps out for days. Why should somebody get involved with Postgres? I think someone needs to be motivated and have their own reasons. A lot of people historically have gotten active with Postgres because they have an itch to scratch.

40:24Maybe they run into a bug, maybe they have a missing feature and they want to fix that. But a lot of people, including myself, actually, have started by just wanting to work on databases for whatever strange reason. And then Postgres is a good one to get started with. So yeah, I wouldn't spend too much time thinking, why would someone contribute? I think if you feel drawn, do it. If you don't feel drawn, don't. Exactly. For advice for how to get started, I don't know. Like I feel that I've been around the community for such a long time that however I got started is probably obsolete by now. I know there are a bunch of good books.

40:58There's a bunch of, there's a lot of resources out there. So I would suggest people just Google for it. Maybe writing your own extension is a good way to get started. We talk about all the extensible type systems and stuff. That's a good place to get started and play with. Yeah, absolutely. Yeah, the extension ecosystem, that is a great way, especially because you can get in, you're writing your own indexing code, right? You're like understanding what does it take to index? How does this stuff have to work under the covers? What do I need to do? Like, I feel like that's a good way in. Yeah, for sure.

41:26Awesome. Well, this has been super fun. Thank you for joining me today. And yeah, good luck. I'm really excited to, I actually have not tried Neon yet. I have to. When I started reading it, I was like, I know. No, well, that's the thing. It's like I have dealt with so many backup things and this, that. As I said, databases have so much inertia. The idea of, oh, I could just spin it up and get my point in time. I don't have to build a custom time system to keep track of what things were. That sounds amazing. Yeah, for sure. Thanks for having me. Cheers.

42:08hermano en mí

From the publisher

PostgreSQL is an open-source database known for its robustness, extensibility, and compliance with SQL standards. Its ability to handle complex queries and maintain high data integrity has made it a top choice for both start-ups and large enterprises. Heikki Linnakangas is a leading developer for the PostgreSQL project, and he’s a co-founder at Neon, which

The post Building PostgreSQL for the Future with Heikki Linnakangas appeared first on Software Engineering Daily.

More from Software Engineering Daily

All 195 episodes
Building PostgreSQL for the Future with Heikki LinnakangasSoftware Engineering Daily · 42 min
Listen in VO