# PostgreSQL: Tuning parameters or Tuning Queries? with Henrietta Dombrovskaya

Source: https://djangotv.com/videos/djangocon-us/2025/postgresql-tuning-parameters-or-tuning-queries-with-henrietta-dombrovskaya/
Date: 2025-11-06
Speakers: Speaker 1, Speaker 2, Speaker 3, Speaker 4

## Summary

PostgreSQL parameters are not a set of magic switches that make an application fast. They mainly tell PostgreSQL what resources—such as memory and CPU—it can use; in Henrietta Dombrovskaya’s example, adjusting parallel workers, work memory, and shared buffers barely improved a slow query, while adding two appropriate indexes cut its execution time from seconds to about 60 milliseconds. She argues that query and application changes often have far greater performance impact, while parameter tuning helps PostgreSQL make better use of the available hardware and workload.

## Transcript

### [00:00] Introduction and Speaker Background

**Speaker 1:** [00:15] Thank you so much for attending and you know what that is oh my gosh it's the last session it's 5 p. When I saw it like nobody will come. It's not even Janga and whatever. But So thank you. Thank you everybody who's who stuck here. All right. So how many of you know what is Postgres? Okay. How many of you ever worked with Postgres directly not through your beloved ORM? Alright, good, good. Because you know what honestly I know that most times developers just use Postgres, but they do not really talk to Postgres. All right, so let's see where we will get this way. Okay. So that's me.

[01:01] I'm Heiji Dombrowska. I am from Chicago. I'm a database architect in DRW Holdings, right across the river, but you cannot fly, so you need to use the bridge. I'm the founder and president of Prairie PostgreS Non -for-Profit organization dedicated to postgreest education in Midwest states in the United States. Also, I'm organizer of PG Data conference and I have literature about PG Data. Please consider coming, contributing, etc. And I run Chicago Postgres User Group. I already mentioned a couple of times, it's conveniently this Wednesday And conveniently again right across the river. But you will need to live from here a little bit earlier.

[01:48] But I will have pizza including Chicago style dip dish, so Consider, okay. I also am I have lots of inks on my plate. I'm a communication chair of SCM Chicago chapter. And I was recently elected the on the board of Linux Professional Institute. Oh my god, what I signed for, I did not even know, but now I have to do this. All right, so and or also I wrote a book about Postgres. So Monica actually reminded I forgot to include the slides. Oh, and Hetty also wrote the book. So available on Amazon. Okay You can scan quite good. So now to the actual presentation. So I actually never did talks about parameters. I mean you guys most of you do not know me, but people who know me knows.

### [02:36] Parameter Tuning vs. Query Tuning

**Speaker 1:** [02:36] I never do talks about in general tuning parameters. And I only had that's the second run of this talk in all my career. So why I am doing it So I will tell you why I'm doing it. So actually what does it mean that to tune your database? I thought I know what it means. I was tuning queries all the time before Postgres, but until I joined EDB, it was a brief moment in my career. I did not know what people mean by like tuning parameters. Okay. So what I found in ADB. I found that tuning database meaning tuning parameters and that was like

[03:22] oh my gosh, I do not know why people even want to tune parameters. So horror story. I never did it before, before I joined the DB. I never tuned parameters at all. And you know why I never did that? And that's what this talk about. Because actually at large it does not really matter how whether parameters are tuned or not. Often I was not in the position of tuning because I was not a DBA, I was a database developer, I was a consultant and coming at like What they're talking about? Why you need to tune something? That's your job. Write this like you know, export write this interface and get out of here. So I could not, and also I learned to live without tuning parameters

[04:08] So still people believe in magic of parameters, and that's what I found when I worked with EDB. So I'm wondering why people like honestly believe that there is some magic like set of perfect Postgres parameters and the switzers from EDB or other consulting companies they know this like world wisdom and they can come and like Tune the database, everything will run with the speed of light. So why people believe in this? Because that's not true. So you know why people believe? Because it's easier Because yeah, working on each model of your program, you know, working on each query is boring, it takes time and like

[04:54] What if you would have a magic? And that's why people believe in magic, because people won't believe in magic because it turns everything automagically. Okay? So my goal today is to show you that. tuning parameters almost doesn't matter. And why it's so difficult to show. I actually people who people who work with Postgres they know that it doesn't matter. But it's very difficult to demonstrate that it does not matter. You know why it's difficult? Because we are tuning holistic system through output versus individual queries. So you cannot really run a demo to show that you improve whole system output.

[05:40] It's difficult to model real-life workload, it's difficult to model real-life concurrency. So it's difficult to run an example where you can actually say A B matters does not matter And I've been struggling to do this also for a while. But again, that is the reality. Tuning parameters and ask any honest DBA tuning parameters, even if you do the best, even if parameters will not tune at all until you come first time, you can make it like Ten, twenty, fifty times better if you maximum, okay? At the same time, tuning queries can make your app application run like several tens times faster. Intuning your application structure that is

### [06:28] A Query Tuning Demonstration

**Speaker 1:** [06:28] DTRM might make it run like hundreds time faster but I was not allowed to talk about it. I actually submitted two talks, one was about this and one about D CRM and guess which one was accepted? Sorry. So we were talking about parameters versus queries All right. So let's look at one query example. So as I said, unfortunately, I cannot model like a system through output. So let's look at this query example. This query runs on the Postgres Air database, which we created for demos. of this book for this book postgre optimization. It's available on my GitHub. It's the largest publicly available PostgreS database where you can really learn and like

[07:15] tune like learn the bill indexes etc. So postgres air. Postgres air is about airlines, about flights, about passengers and all these things So let's look at this query. So this query selects for each flight which departed from JFK Airport and landed in Ohara International. Between August eighth and August twelfth, twenty twenty-three, for each of these flights, it tells you the actual departure time and number of passengers on this flight. Okay, normal query, just two joints like not a big deal. So now we are executing this query on clean database.

[08:03] We just installed it. You went to GitHub, like installed Postgres, downloaded database, loaded. Okay, go so I will make it bigger, no worries. Right. So execution time for this query is two point four seconds. And you think it's like okay two point four seconds? It's not Right, it's not okay. It's not okay. Right, right. And again, I will make it bigger. Yeah, that's it, no, that's Does it make an aye? Okay, yeah, yeah, yeah. That's that's I I'm showing you this part of the execution plan, which is bad. So you can see here there are lots of reads, and it's like buffer swapping, etc. So Why what we can improve here for this query what parameters we can improve to make it run?

[08:51] First of all, okay, there was a large table and maybe So you notice there is like single worker. So actually let me go back through. Yeah. Number of okay, sorry. Yeah, number of parallel oh sorry, I what I did, I did wrong. Let me Try to go back. Can I go back? Yes. Yes, I can go back. So number of parallel workers in the beginning was zero. Okay. Yay. What sorry, sorry, sorry, sorry Where I was, where I was. Okay, here I was. Okay. So number of parallel workers here was zero. Okay. Apologies. Moving forward. Okay, so number of parallel workers was zero, shared buffers default

[09:37] and work memory default. Okay, now let's improve it, let's make max parallel workers pergether two. Okay, and we are doing this and we are making okay we are making execution time 2. 1 seconds better still not good, right? Okay, because it's still the same problem Okay, now let's move it okay move it move it move it ay I am turning it in the wrong way sorry I am turning it in the opposite direction Okay, now we are we're talking. Okay. So now we are increasing work memory. We are increasing work memory to for half a gigabyte, then we increase work memory for one gigabyte.

[10:23] So we are improving 1. 8, still it's not good enough. So last resort, okay, let's try to increase shared buffers. We're increasing shared buffers it requires restart. So one second. By the way, we if we increase it more, it will be one point one. Oh my gosh Okay. So what we're doing? So by increasing shad buffers, that's it. So we increased it twice, right? Twice and still like okay So let's try something different. Okay? Let's look closer at the execution plan. So we have the scan of table for the departure days between August 8th and August twelfth. So here is this scan. So what we're going to do to

[11:08] we scan? Who knows what we're going to do to we scan Index, create an index, right? Creating an index, building an index, and the execution time Immediately 0. 7 seconds, hurry, but it's still not good enough. So what else is bad with this express Execution plan. Okay, that's what the bet with this execution plan. We still have another gigantic table, which we still are doing full scan. So we need to build what Another index, correct. We need to build another index. Okay, so this one built another index. Now I know it's a gigantic plan. I will highlight what changed. Okay, here is what changed. Now we have this index and execution time

### [11:55] What PostgreSQL Parameters Do

**Speaker 1:** [11:55] sixty milliseconds. Okay? So and next Do you know what will happen if we will ditch all our parameters improvements and get it back to what it was? Who knows what will happen? Nothing, right? Absolutely nothing. So forget about all these parameter tunings. We build two right indices. Booms. Execution times is like what 20 20 times faster? Okay, so good enough, right? So now yeah in texture speed everything well. So now why we need parameters? Why we need parameters at all? We need parameters because that's the only way for us to tell Postgres

[12:40] how the hardware on which it's running looks like. Without parameters, Postgres doesn't know how much memory it has. how many cores it has, what it can allocate, how many buffers it can read. So without properly communicating Postgres will use only small fraction of memories, or will never run parallel query, or will avoid using indices if it would not change random page cost. So the only role of parameters is clearly communicate, hey, Postgres, that's resources you have. Use them. And then it will use it. To the point. Other than that, it's like no magic. Okay? So application change. I know I was not allowed to talk about this, but just telling you

### [13:27] Application-Level Performance Improvements

**Speaker 1:** [13:27] There are lots of things where you can change the way how you query, not adding indices, like for example, using equal instead of approximate in 95% of time. Why you did this one? I don't know. Actually We can work on this. Avoid extra column transformations like truncate created at current date. It's column transformation. You can easily do it mo more or less. Like not committing until the end of the badge committing after this our beloved or ams love either committing after each insert Begin select rollback all this good stuff. So this might drastically improve your performance.

[14:15] Now it's almost me so where to find me? Phones, phones, phones, phones up. I'm everywhere by the way. I'm everywhere. Hetty Postgres. You can Google, you will find more than I want you to know about me. So LinkedIn, GitHub and we have Meetup. So please sign up if you are interested and yeah if you sign up I need your first last name by high security building. And that's it. And thank you so much. And yep, any questions?

### [14:59] Closing and Questions

**Speaker 2:** [14:59] So you said the main reason that parameters exist is because Postgres doesn't know how many chords you have, how much memory you have, etc. etc. Yeah. And my reaction is, well, why not? Can't it probe that stuff at startup?

**Speaker 1:** [15:13] Okay. So is there a ways to create from Postgres where you are running and find result. But the query planner at the moment when it runs the query planner relies on parameters Query planner reads. Sorry, sorry. Okay. Que a query planner reads parameters reads from a PostgreSQL. and it relies on what you communicate to it. And you know what there are many ways how we can actually artificially tell Postgres things which are not necessarily true but we want Postgres to think this way. It's like all like all separate kind of all separate theory. Then you can do different allocations based on your workload, for example.

[16:01] It's actually rarely the case 'Cause almost everybody will tell you or depending on whether you are OLAP or OLTP, you need to set up parameters differently. So, people, you all work in industry. What are our databases? Are they all CPO lab? None. They are hybrid. 95% of our databases are hybrid. So that's also kind of like questionable thing, but again, like the theory behind this that you can tune based on your specific workload. And there are situations when you have like one million one million users I had customer like this or you have like ten

[16:46] marketing analytics Who bring your database down? These ten people, you know, I love marketing analytics. You know how they build their models. They like read the whole database into their Python because they cannot do incremental models build. So yeah.

**Speaker 3:** [17:02] Thanks so much for being here. In the spirit of your other talk about application performance tuning, what re what resources would you recommend to look at?

**Speaker 1:** [17:12] for pl for application performance tuning?

**Speaker 3:** [17:15] Yes.

**Speaker 1:** [17:15] That is a good question. I actually You know again it's probably better to recommend it at Django. I do not know book about Janga performance improvement, but Andy Atkinson has amazing book about Postgres for Rails where he but it's like the same basically like dealing with Postgres when you have object-oriented system which uses ORM. That's actually one of the best books about this. So maybe you know somebody will do this for Jenga or for Python or for Bova. Right?

**Speaker 4:** [17:54] Six minutes left for questions?

**Speaker 1:** [17:57] Okay. Okay, you know where to find me, right? Okay. So that's all for this.
