Postgres 19 REPACK (CONCURRENTLY): RIP pg_repack?
Kirk [00:00:13]: All right. Welcome to Postgres.FM. I'm your co-host, Kirk, and I'm going to introduce Nik from PostgresAI. Nik, how you doing today?
Nik [00:00:22]: Hi, Kirk. Doing great. How are you?
Kirk [00:00:25]: Fabulous. We got an interesting, one today. We're going to talk on our continuing saga of Postgres 19 enhancements and new features. Today, I think we're going to be talking about the REPACK (CONCURRENTLY) feature. And how about we introduce the concept a little bit first so that people understand where this comes from? Maybe, I'll throw it over to you to explain it.
Nik [00:00:48]: Yeah, let's first start with very basics, because I know some people pass by who are even new to Postgres or something. First thing I wanted to say that it's, one of the brightest feature in Postgres 19, and the story behind it is also amazing because it survived pressure and there are blog post... There is blog post by Álvaro Herrera who described the battle behind keeping this feature in Postgres 19 after pressure. And I just learned before the podcast I was mentioned there because my AI delivered a few bug reports. But if we talk about, why it's all needed, many people heard about MVCC and bloat in Postgres, how...
And the scariest word, vacuum and autovacuum, and it's garbage collection, right? So if, we, delete or update rows, old versions called tuples, physical row versions, are preserved, and then we... then autovacuum or manual vacuum deletes them. And if, it doesn't do it immediately, so sometimes a lot of that tuples accumulated, vacuum, autovacuum deletes them, leaving a lot of empty space. And if, some of listeners used Windows and remember defragmentation, colorful, right? I remember it like from 25 or 30 years ago when I was using Windows. So this is exactly that, like garbage collection leaving empty space called bloat.
And sometimes it's like 90% of everything, and we keep, keep paying for not only disks, but these days we should think about memory more because memory is super expensive, right? And to load a page which contains only 10% of data, right?
Kirk [00:02:49]: Yep. That's a good comparison. I like it. Defragging. You're kind of getting rid of the fragmentation inside the file. And just so everyone else gets this, if you have a file and there's a lot of empty space in the middle of the file, vacuum can't truncate the file. Not allowed. It can only truncate unused pages at the end. And I think some really bright guy wrote a solution that helps with that.
What do you got?
Nik [00:03:13]: Right. There are several guys... It was 15 plus years ago. Several guys did it. One of them is Maxim Boguk, who created pgcompacttable. And the idea was simple. It was, I think, pg_repack didn't exist that time, actually. There was pg_reorg, which was not... which had limitations, then what it was converted to pg_repack project. But the idea was let's just, update those rows which are sit at the end.
Kirk [00:03:43]: So they move the rows that were at the end, and they move them further away from the end so vacuum can get rid—
Nik [00:03:48]: Yeah, Postgres, when you need... When Postgres needs to create new version with insert or with update, it uses so-called free space map to find free space in some other pages to avoid, new page creation when, when it's not necessary, right? So if you update, some tuple which sits at, at the last page, likely it will go to some free space in the middle, basically, right? Or, or at the beginning, somewhere there, according to free space map, FSM.
And the old version will become dead and eventually will be cleaned up, converting to free space. And once we, at the last page, have no more tuples at all, this page will be cleaned, right?
Kirk [00:04:35]: And once it's clean, completely clean at the end of the file, truncate can actually make... or the vacuum can make the file smaller by getting it back—
Nik [00:04:43]: Yeah, regular, autovacuum does this periodically. By the way, it's quite invasive operation. It requires brief ACCESS EXCLUSIVE lock as well to truncate. So that's why sometimes we run vacuum with truncate option turned off to avoid this invasive nature, and it can be adjusted per table level. So, and but yeah, truncate means that page will be just removed, and this is how slowly you can start shrinking your table. This is like— (Kirk: Nice.) We can call it lazy repack, you know? Like, this is like I'm doing slowly—
Kirk [00:05:16]: But I think defrag, (Nik: Yeah, yeah.) Yeah, yeah. Lazy repack or defrag. So that was one of the first solutions. That's been around, like you said, for a while now. (Nik: Yeah.) And it doesn't need a lot of extra space to do this magic.
Nik [00:05:29]: Right, right. This is solution for, like, some edge cases when we have, like you said, we don't have a lot of free space, or we cannot afford copying the whole table because it's a lot of disk I/O. The magic there is that we can throttle it. If we are not... Like, we can say, "Okay, we are going to lazily repack this table during, like, a week." Like, nobody will— (Kirk: Right.) But the most common solution was VACUUM FULL.
Which is super invasive, right? It blocks everything. But this is-- this, until Postgres 19, was actually the only solution in Postgres, officially maintained. Not third-party tool— (Kirk: Wow.) Officially maintained, right? Cluster command is the same as VACUUM FULL, but it also reorders, tuples— (Kirk: Right.) According to some index. So index becomes super efficient in terms of how many pages are touched. So VACUUM FULL and cluster, super invasive, blocking table. So even selects cannot be running, right?
Until Postgres 19. Oh, of course, we forgot the most common third-party tool, pg_repack, right?
Kirk [00:06:41]: Thank you.
Nik [00:06:41]: As I said, pg_reorg was converted to pg_repack, and this tool is quite advanced. Like, I remember I dove into details many times, and we used it for multi-terabyte tables under load. (Kirk: Wow.) Every time I learn something new. Actually, honestly, I learned something new yesterday with DBLab Forge with these experiments we are going to discuss. So what it does, it creates trigger to capture all changes to a special, I call it usually delta table. There is some original, terminology, but I prefer it calling delta table.
And then it, copies, table just using insert select, single command, single thread, just copies. And you cannot throttle, unfortunately, but it's a single thread. And when it, when it's done, it accumu— it, replays all the accumulated changes. And it does it not in one transaction, as one might expect. It does it quite, in a quite smart way, in multiple steps.
Kirk [00:07:49]: Hmm.
Nik [00:07:50]: And if you think it's impossible, it's possible, and there is logic behind it, which is quite advanced. Like, it's interesting because it uses the fact that some changes you can reapply. And then it's, just so, like, you don't need a single transaction to replay all delta.
Kirk [00:08:07]: Oh, okay. Good.
Nik [00:08:08]: Yeah, yeah. And this, only the last transaction should be a single, like when you already lock table to do swap. And swap is done not in using DDL, so it doesn't change catalog at the end. It just replaces so-called real file node, so it basically swaps the file.
Kirk [00:08:26]: Oh, so it changes the pointer to which file it's working— (Nik: Right.) From this point forward. Got it.
Nik [00:08:32]: The same thing actually new REPACK (CONCURRENTLY) does as well. So it doesn't—
Kirk [00:08:36]: Okay, so slow down for a second. I just want to recap. So we had the pg_repack table or compact table, the first one. Then we moved up to say we had pg_repack. So we had the compact table version, the pg_repack, which is a, an external extension you install to be able to use this, right? And now we're moving into the next layer, which is what they gave us now, which is REPACK (CONCURRENTLY). And good?
Nik [00:09:07]: Yeah. So there was one more tool called pg_squeeze, which was created— (Kirk: Right.) Antonín Houska. And we had an episode, Michael and I had an episode with Antonín, in the beginning of 2025, at the very end of which I... It was a surprise for both of us when he said that there are plans to implement VACUUM FULL concurrently at that time, VACUUM FULL concurrently, in Postgres core. So bringing pg_sque— pg_squeeze to Postgres.
And pg_squeeze idea was like, let's do the same as pg_repack does, but instead of triggers, let's use logical replication.
Kirk [00:09:48]: Ah, okay.
Nik [00:09:49]: Right? That's it. Like, let's just have this install replication and just create table with logical replication, and then, then, switch to new table, replaying all the changes to it. And this was a great idea, and it worked, but with some... I remember bug reports our clients delivered, and actually Antonín very quickly solved them. I must say, all of these tools are quite difficult. They are hard to implement. They have a lot of nuances, which you meet in production in large systems with slow disks, fast disks. Like, it's not easy to just, "Oh, we have idea, let's go." Over years, you find new problems and fix them. This is how all of them existed.
But what I was amazed first, like, this tool, pg_squeeze, indeed went to Postgres 19. Because usually such complex tool, it's, it takes years to deliver to Postgres.
Kirk [00:10:53]: Wow, okay. So good. So now we have pg_squeeze. So everyone can see this theme is building. And the goal of good ideas, once they're kind of well tested, is to actually get them into core. And getting them to core is kind of the gold standard for saying, "Hey, we believe this is mature and ready to use." Like, so now we have the REPACK (CONCURRENTLY). And I guess they did that instead of vacuum concurrently.
Nik [00:11:21]: We can, slight— like, slightly, add on here. I remember we all used pg_repack to fight with bloat in indexes as well, because there was no such thing as index concurrently So we used pg_repack. It had option, still has option, to take care only of one index or all indexes, not touching heap at all. And you know this topic, well, like, B-tree bloat, right? (Kirk: Yeah.) So, but then reindex index concurrently came to Postgres, and it was a big story in Postgres 14 when it was leading to corruption, multiple minor versions.
Then it was fixed. Because it's not easy. Like, these topics are hard. But what, why is it so needed? Because the bloat is a real problem. Like, over time, you accumulate bloat, performance degrades, and you pay too much for, as I said, storage and memory. Everyone suffers. And everyone suffers not like boom, and that's it. It accumulates— (Kirk: Yeah.) Accumulates, accumulates. Yeah. So we usually say, like, bloat should be below 50%, 40%. And sometimes some bloat is good because it helps, to have good performance in some cases. You know, indexes have fill factor 90, 90%, which is basically 10% official bloat by design.
But reindex, index concurrently went to core and became very good, reliable, well-maintained tool to mitigate bloat.
Kirk [00:13:01]: Used it myself. (Nik: Right.) Right.
Nik [00:13:04]: What about heap? Heap is still a problem, and you need, like, the most battle-proven tool over last years was pg_repack, right? (Kirk: Yeah.) But it needs extension. You need to have proper runbook. There are edge cases. There are problems still. We need something in core. And finally, we have it. This is REPACK (CONCURRENTLY).
Kirk [00:13:29]: So that means, to me then, REPACK (CONCURRENTLY) is as safe to use as index concurrently.
Nik [00:13:35]: Well, here's the deal. We, like usually recently, we started doing, using DBLab Forge system to study something in detail. I saw a lot of surprises over the last few days. And the biggest surprise for me was, we should understand the word concurrently very differently compared to create index concurrently and reindex index concurrently. Moreover, I'm going to publish a proposal to improve documentation this week because there is real danger.
The word concurrently, one might expect that the word concurrently means that this is fully online operation. What's the difference between VACUUM FULL and cluster, which were the only official tools to mitigate bloat before Postgres 19, and pgcompacttable, pg_repack, pg_squeeze, and REPACK (CONCURRENTLY)? The difference is the last four tools are considered online. So you... it means that you can avoid any maintenance windows. You can just run them during normal traffic. Nobody will notice.
This is ideal goal, right? But it's not so. If you compare REPACK (CONCURRENTLY) to index reindex index concurrently, reindex index concurrently, you can just run, and it won't need ACCESS EXCLUSIVE lock. (Kirk: Yeah.) Right. You only... The only problem that might block you is some transaction which changed schema, DDL, right? (Kirk: Right.) And it's still not finished, holding this ACCESS EXCLUSIVE lock on the table, and that's why our reindex index concurrently or create index concurrently cannot finish. (Kirk: Right.)
But even if it cannot finish, it will be waiting with attempt to acquire this level of lock you mentioned, and it won't block others. Unlike the case, we talked with Michael a lot about this. If you add a column which requires ACCESS EXCLUSIVE lock for just a second, like just a millisecond actually, just a moment, there is a big rake. Every, steps on this rake when load increases.
So you try to just add a column, which is a breeze, right? But there is some long-running transaction, some select, like dump or something, holding share lock on this time. And you cannot acquire this lock. With default settings, lock_timeout 0, times, transaction_timeout 0, you keep waiting. The problem— (Kirk: Yeah.) All queries which started after your attempt, even selects, they are blocked.
Kirk [00:16:39]: Yeah, so they get blocked. That can happen now in the area where we're doing REPACK (CONCURRENTLY) because this thing can run for like five hours, and at the end of its process, it needs to acquire a lock. (Nik: Yeah.) And it goes, and it starts waiting for that lock. And God forbid if five seconds before it tried to acquire that lock, someone ran pg_dump, and it's going to take—
Nik [00:17:03]: Right. Or, or a logical query, anything. Yeah. (Kirk: Right.) So for me, like DBLab Forge, as usual, we follow methodology, collecting all the evidence and so on. We start usually with some, like, you know, like— Simple, like smoke tests, spikes and so on, or smaller tables. But already on smaller tables right now on the screen, you can see the case T1, smaller 20-something gigabytes table, right? It's... Yeah.
Kirk [00:17:31]: And the link is in the link is in the show notes. (Nik: Okay.) And we're going to have a QR code pop up. So if you actually can, you know, shine your phone on the QR code, you can get straight to that link as well.
Nik [00:17:42]: Right. All the details in that big HTML report for humans and their agents. But in the center of the screen right now, you see the profile, ASH, active session history, plus Gantt diagram. And this is new, like finding with our fourth system we started doing. So you see, active sessions, and how they map to each phase, pg_repack was doing, and in this case, REPACK (CONCURRENTLY) was doing. So we have this kind of analysis for all of them. And even on this small case, I already, I was already concerned that REPACK (CONCURRENTLY) at the very end, when we, as we just discussed, it swaps files.
The duration of this red area is many seconds, like seven seconds or how many. I'm not okay even if it's one second. It's too long for a small table to spend that much time for swapping. And the number on X, Y, active sessions, holding, like being in that state, red state, means many sessions were waiting on lock acquisition. Those sessions are not our REPACK. Those sessions are background workload we had with 16 pgbench workers. They were waiting on lock acquisition. Why? Because, because they couldn't do normal work.
They were waiting because we were waiting. (Kirk: Yeah.) That's the problem. And if this was even without introduction of long-running transaction. In subsequent cases, we started to introduce long-running transaction. For example, we have some select count running one, once, one minute, right? And it started right before our REPACK (CONCURRENTLY) needed to do this, that final swap phase. Whole minute we have downtime. Boom.
Kirk [00:19:37]: Right. Because once you say... I call it the cascading locks or raining locks. All of a sudden it starts raining locks and just all these locks come in because everyone who tries to do something is locked waiting for somebody else who's locked waiting for... And this all gets triggered, and then it just comes down. Even if you look at that red, it almost looks like a the, an umbrella, like of red, like, you know, because it was, it's just raining locks in that area.
Nik [00:20:06]: Yeah, and there was a lot—
Kirk [00:20:06]: And it's a significant amount of time.
Nik [00:20:08]: As a tree or chains, like it's basically usually a tree of blocking or locking trees. And I, in our analysis, I say usually it's a forest of trees because there are multiple cases where multiple roots. But yeah, there are like good queries which you can use to analyze that. We jump to the problems, I feel not well because REPACK (CONCURRENTLY), as I said, it's amazing story of tool which will existed as third-party tool for some time, and then it was brought to the core.
Two guys especially worked, really hard on that, and Antonín and Alvaro. And it was almost removed, but it was saved. And it was, it has some limitations documented. We should talk about them as well. And it has a lot of good properties. But this is like, this is a danger I just see. Like, I, you know, like always when I start talking about problems, people say, "Oh, you hate, you just hate Postgres."
I feel, just understand me properly. Like, I love the feature and Postgres itself. I just see this danger, and we need to warn about it in the documentation and maybe improve in the future because it's not okay. Like for me, like, I just think some people might expect, "Oh, this is concurrently, this is online operation." (Kirk: Right.) And I expect people will be like, yeah.
Kirk [00:21:39]: If you walk away with one thing today, okay, REPACK (CONCURRENTLY) is 100 times more risk than re-index concurrently. (Nik: Right.) That's what I want you to know. Now, is it great? Yes, it's a wonderful tool. Before you go doing it, okay, I can, you can re-index concurrently almost willy-nilly. But before you go using it, understand it has a time and a place. You don't let it run if you have some kind of pg_dump going. You don't have it run during some heavy duty long-running queries. You need to do this carefully, okay?
It's similar at certain times to the impacts of anything that acquires an exclusive lock. So be careful with it. Read up on it before you go run it. Don't use it on your heaviest run table first. I will encourage you not to do that. But get used to it and understand it. We're glad that it's here. We look forward to using it. And there's also a secondary note we'll get to about it not being MVCC safe, okay?
Nik [00:22:42]: Yeah, before that.
Kirk [00:22:43]: And some—
Nik [00:22:44]: Like, to be fair, so this is like, imagine you purchase a ticket on train. You arrive, but they don't let you enter, and all people around, behind you also cannot enter because you're like somehow occupying this space and so on. The problem is also, like there is a mitigation, obviously, lock_timeout. You can set it, say, to second, right? But if your table is huge, you did the whole job. REPACK (CONCURRENTLY) did the whole job, copied everything, applied changes, and started swapping. But then because of some analytical query pgdump, it blocked during one second, it will decide to cancel itself and all the job... Like, you had only one attempt.
That's the problem. Because repack, pg_repack, it has a loop when it tries to acquire ACCESS EXCLUSIVE lock, starting with lock_timeout 100 milliseconds, 200 milliseconds, up to one second. So it, it's more, it's more graceful.
Kirk [00:23:50]: Right. Yeah, it's not just gonna fail on the first attempt. It gets a timeout, and it goes away, and all the work it did up to that point is gone. Now, in our test, all the work it did was measured in seconds to minutes. But in a large production table, okay, like, you know, some of the ones Andrey talks about, this could have taken 5 or 30 hours.
Nik [00:24:14]: Hold on.
Kirk [00:24:14]: And could you imagine at the end of that?
Nik [00:24:16]: We had a 900 gigabyte case. We measured it. It took a lot of hours, right? It's in the center of screen.
Kirk [00:24:25]: Oh, yeah.
Nik [00:24:27]: This is the case T2, 900 gigabyte table. And that thing took, a lot of hours. It took 5 hours, 19 minutes.
Kirk [00:24:38]: That was the five-hour example. Sorry.
Nik [00:24:40]: Which is fast because it was local NVMe, so it was quite fast. So yeah, and we measured everything under 30,000 TPS as well, compared pg_repack, repack. There is a lot of interesting findings, in this research.
Kirk [00:24:58]: No, no, good, good correction on me. That's what I was getting to. This may not be a minutes problem. You could have spent five hours, created a window in your operation, and then outside of your control, something somebody else did caused your lock to fail at the very end of a five-hour process. (Nik: Mm-hmm.) And now you have to schedule it again some other time.
Nik [00:25:19]: Yeah, basically you have two options. Either you have one attempt not blocking everyone for too long, like setting lock_timeout, or you have infinite or very high, high lock_timeout being set. In this case, you're risking to block everyone and have downtime. (Kirk: Yeah.) So what I would do right now, I would just use it with unset or very high lock_timeout as protection, maybe even unset. But I would have terminator script which would shoot off all transactions which,
Kirk [00:25:55]: Might be interfering with you.
Nik [00:25:57]: Right, right, right. If I see I'm blocked by someone, or in the same terminator, maybe I will decide to shoot my attempt. I would just have priorities. If it's autovacuum running in regular mode or even if it's to prevent transaction ID wraparound, but we have a lot of capacity, we are very far from wraparound actually, I would just terminate the autovacuum. If it's some transaction like dump, maybe yes. Our work is more important. But if it's something super important, we cannot cancel, and we have logic behind it, I would probably abandon my attempt and reschedule, right? So this is what I would do right now, and I'm just afraid people will step on the rake and will have downtime, unfortunately, expecting concurrently work as it does. (Kirk: Exactly.)
Right.
Kirk [00:26:45]: And just to mention, in our production environment, we have logic for our scheduled tasks that run and do all this kind of heavy lifting stuff. Some of it can run for an hour, okay? No problem here. But we purposely check at the beginning of our scheduled items if something like a dump is running or certain other things we know we don't want to be running against, we will automatically just exit, and then we know that the cron job will pick back up in a while and do this anyways. So it's something we do recurringly, like refreshing a materialized view or something. We're not going to do it.
Nik [00:27:20]: Concurrent.
Kirk [00:27:20]: Yeah, yeah, yeah. Right, right. Concurrently.
Nik [00:27:23]: Right. So, but beyond that, of course, I think pg_repack is to be used for quite some time. But you know what I learned about pg_repack and realized yesterday, today? It has problem—
Kirk [00:27:35]: Please tell. Go ahead.
Nik [00:27:37]: It has a problem with autovacuum as well. First of all, if autovacuum is running in transaction ID wraparound prevention mode, it won't yield, as we know, right? It won't kill itself because his job is more important, right? So, but moreover, if, it's running in regular mode, it yields only after deadlock_timeout, which is by default one second. But so... And repack starts trying to acquire exclusive lock starting with lock_timeout 100 millisecond, which is 10 times smaller. It has no chances. Autovacuum won't yield.
And when it increases it, by the way, it increases it without any breaks. So on our ash diagram, as you can see right now in the center of screen, it looks terrible, or it looks like all our pgbench sessions were suffering. It's actually, by the way, a good case for, remember, Dmitry's W-E-T, wet tool, wait event tracing, because ash here fails. Ash here says all pgbench was in trouble, but it doesn't show that we... after first 100 milliseconds, we gave up and had chances to breathe for our pgbench clients.
Right? (Kirk: Mm-hmm.) Because— (Kirk: Yeah.) there is a big difference. You hold them for all the time, and all sessions are blocked, or you hold them, release, and hold, try, hold again, right? So transactions actually don't drop to zero. TPS don't-- doesn't drop to zero, right? So I wish still there are some breaks between attempts inside that loop, pg_repack does. But the problem is, the biggest problem is that it only increases lock_timeout to one second, and it's quite an invasive. The very first, episode of this podcast, happened 199 episodes ago, was about what is slow in terms of queries. And we agreed that 100, 100 milliseconds is not slow, 200 already not noticeable.
One second is slow because human perception recognizes it. So one second lock_timeout is already noticeable for humans, definitely noticeable for agents, right? And then the problem is, this is exactly where deadlock_timeout, and I think it's a kind of gambling. Will autovacuum triggering on deadlock_timeout, it's all like check if it's blocking anyone. Notice that—
Kirk [00:30:05]: Can that be, can that be changed?
Nik [00:30:08]: Well, you can change deadlock_timeout, to two seconds, for example, like some people do, or five seconds, and then repack won't have any chances.
Kirk [00:30:17]: Right. I meant in the other direction. Can I, can I lower it? But that—
Nik [00:30:22]: You can. (Kirk: Yeah.) You can. You will have, some more overhead of deadlock checks and lock, like blocking state checks and more logging. (Kirk: Right.) In Postgres 19, you know, log_lock_waits is enabled, so you will have more logs. But anyway, in our tests, it was proved that pg_repack was failing to acquire that lock completely, and autovacuum didn't yield. So this I learned like in experiments, and I suspect some of the failures we had in production, which we didn't study proper in terms of root cause analysis, are, are associated exactly with this. And I just offer, let's study this topic separately in another episode because I think it deserves proper attention.
Kirk [00:31:08]: No, good, good. And I agree. Also, I did have one other question. Is there a way to say, on this table, before I start this in a separate transaction, turn off autovacuum and then do this process? And then after you're done, turn autovacuum back on for that table?
Nik [00:31:27]: Well, there is a problem also with aggressive autovacuum mode, which is to prevent transaction ID wraparound. If you, if you alter table to adjust storage parameters to set autovacuum off, it will switch off only regular autovacuum, but not that aggressive one.
Kirk [00:31:45]: Not the transaction wraparound. (Nik: Right.) Right.
Nik [00:31:47]: And I just prefer to have a terminator logic, which is just— (Kirk: Yeah.) says, "I don't need autovacuum," even if it's aggressive mode, like, because I see it's far and it's safe to terminate it right now. I think there is a big potential here to improve autovacuum behavior, you know? Because—
Kirk [00:32:03]: Oh, and they're-- they've made some changes with regard to that too in 19, and we'll get to that at another time. And yeah, let's make a plan to jump into— (Nik: Right.) the autovacuum and add a wish list that we can add to that.
Nik [00:32:16]: Right, right. There are interesting things here related to how we make schema changes and how repack works. And as for REPACK (CONCURRENTLY), it still will need to get this logic. I see already some patches delivered to Postgres 20, and you wanted to attach to other limitations which are covered in docs. One of them is that it's not MVCC safe, so all transaction might see table suddenly empty while it's working. (Kirk: Right.)
And there are plans—
Kirk [00:32:45]: And just to give somebody an example, you have a repeatable read transaction type where you're allowed to reread the transaction and see the same data at the beginning of the transaction. This is very standard. Anyways, and you look, and there's 10,000 rows in this table, 10 million, whatever. And now at the end of your transaction, you do a select, and behind the scenes, it renamed the relnode. Like, it pointed to a different file that has now been there. And now when you go to look at it, it's the Xmin stuff. It doesn't know what... You end up getting zero rows back. (Nik: Right.) And so that's...
So it's a red flag. Just know that can happen. If I had an accounting packet software package, I would— (Nik: Right.) not sure I would ever use this on the main table, like on a general ledger table, without some kind of caveats that prevent things, because you can get a bad answer.
Nik [00:33:37]: Yeah, yeah. And pg_repack doesn't have that. And another thing is that I was surprised to learn from DBLab experiments, DBLab Forge experiments, that unlike pg_squeeze, which, accumulates changes basically in logical replication slot while initial copy cloning of table happens, it was seen as big lag in replication slot. And if you have max, wal, max, slot wal bytes setting set, it might just cancel your operation, and you can lose slot because it's not allowed, for example, in this system to have more than, for example, one gigabyte or, or something. So we, I didn't see that in REPACK (CONCURRENTLY). Obviously, it was improved. (Kirk: Right.)
It, decodes changes on the fly, so we didn't see a lag beyond few dozen of megabytes, which is good for, even for large table. So, and it just decodes it and keeps changes in some temporary directory, which raises the question of observability, who, like, I guess there is a big potential here to improve Have something like pg_stat_progress_repack, something like this. But then I was thinking, okay, great. If, it's consuming from the slot all the time, maybe it's not pinning xmin horizon. But then I realized, oh, no, no, no. It still pins xmin horizon because initial copy is still copy, right? (Kirk: Yeah.)
Right. But I was surprised to see there is work to avoid that. And there is-- there are already, like, I think patches proposed already. This idea is expressed how xmin horizon pinning could be resolved, which is great. (Kirk: Yeah.) Yeah. So horizon will be shifting during initial copy as well. This is super interesting. But I just wish this attempt to avoid xpin pinning, xmin horizon pinning will be... I wish it will be more successful than attempt which was done for create index concurrently, reindex index concurrently in Postgres 14.
In infamous Postgres 14.0, where it was implemented, but until Postgres 14.4, it was leading to corruption in indexes. And then it— (Kirk: Right.) So xmin horizon pinning is... Why it's important? Because you are fighting with bloat in one table, but pinning horizon database-wide produces more bloat in all tables. (Kirk: Right.) While you're working. Can you imagine? That's why you still need to schedule all these things during low traffic periods, right?
Kirk [00:36:19]: Fabulous.
Nik [00:36:20]: So many things.
Kirk [00:36:20]: Yeah. No, that's good. It's... Well, and this is it. Here's the good news. Once it made it into core, it's here, and we know it'll be improved. These issues are going to be addressed, and it'll become more usable and more safe to use over time. You couldn't convince me otherwise, right? And this is what we like about this process and that it made it... This is why we love Postgres and the community. So my... So I'm doing a hat tip to everyone involved in pulling this off, right? I did focus, and I wanted to focus on the negatives people might not be aware of, because I don't want someone using it inappropriately and getting burned, right?
And then talking about that, and we see these blog posts, right? I got fired because I ran, you know, REPACK (CONCURRENTLY) over the weekend while my boss was running his reports, whatever. So yeah, I don't want to see those blog posts.
Nik [00:37:14]: I guess we will need a part two because I guess that I posted a video about Postgres 19, 10-minute, about all things I like the most about Postgres 19, and I promised to create this episode about limits of pg_repack and REPACK (CONCURRENTLY) to explore where are the limits. We talked about the nature of problems, but I guess people want to say, "Okay, you cannot use it beyond 3 terabytes or something." And this thing we could not deliver today. The post, reached much, much many more likes than I requested. So I feel obligated to have part two maybe another time.
Kirk [00:37:51]: Nice. Excellent. This is any other takeaways for the people listening?
Nik [00:37:57]: Yeah, I think the key thing is, like, number one thing is use reindex index concurrently to fight bloat in indexes still, right? Like over— (Kirk: Yes.) Ideally, automatically. This is a zero thing. First thing is pg_repack is going to be used for quite some time with additional, additional things to be keeping in mind we touched today. And REPACK (CONCURRENTLY), I would use if... I will start using in systems, in smaller systems first. If you have less than 1 terabyte, it's much more convenient because it's in core, you know? (Kirk: Yeah.)
I would just either have terminator or set lock_timeout and be okay with that my attempt failed at the last step.
Kirk [00:38:45]: Well, let's talk about this for a second. What if you got... You do have, like, we have one big logging table, but it's mostly... It's not even a read table. It's logging. So there's two hours during the day when it gets touched. The other 22 hours, it's almost not touched because we summarize the data out of it. So I could run this on that table. The challenge is it has one drawback. It needs duplicate space.
It needs to recreate— (Nik: Yes.) this entire table. (Nik: Yes.) Very much. And if that's the case, I've been just using the, you know, the VACUUM FULL on this table.
Nik [00:39:21]: I'm amazed at how you are confident saying it's not touched. I never know when autovacuum comes.
Kirk [00:39:29]: Oh, yeah, yeah. So that's interesting about that. But since this table doesn't change much, yeah, you're right.
Nik [00:39:37]: Who knows?
Kirk [00:39:37]: I guess—
Nik [00:39:38]: We don't have ability to say autovacuum don't work in these hours. We don't have it. You need to program some logic around how you fire your REPACK (CONCURRENTLY), you know? Yeah, yeah, yeah. Great. It was pleasure. Again, as usual, all the links below. Yeah. And watch us on YouTube more, than on other sources because we have animations and illustrations and information.
Kirk [00:40:07]: And by that, he doesn't just mean that I talk with my hands, just to be clear. No. Thank you, Nik.
Nik [00:40:14]: Thank you.
Kirk [00:40:15]: Excellent episode.