so imagine your database is humming along things seem to be operating normally and then maybe a developer comes by and says hey you know I've got this query that sometimes it's slow and maybe it's a pretty simple query so it should always be returning fast but for some reason a small percentage of the time it's returning very slowly at least from the information they're providing you well that's what we're going to be covering this week in the first post but I hope you your friend friends family and co-workers continue to do well our first piece of content is sometimes it's slow this is from hosa. org at least if it's within postres but she was investigating and trying to figure out what the cause of the sometimes slowness was she said there were two contributing factors one is the tendency for people if they need to load a lot of data into the table following the advice to drop indexes and constraints before loading the data and then recreating them afterwards and the second thing that contributed to it is how most OMS wrap their statements in transactions so they do a begin they send the statement they're going to be sending to the database system and then they do a commit or roll back and this is even for select queries as well and both of these cases contributed to what you was seeing so she says why is it a bad idea to delete indexes and constraints on a table that you're trying to load in well if you have a case where a table already has 10 million rows and you just need to add 50,000 it makes zero sense to drop the indexes and the constraints number one you're losing your referential integrity with the constraints and number two losing the indexes if there are any queries that are ongoing they're going to be really poor performing during this time so I would never drop constraints and indexes if I was bulk loading unless that table was empty I might consider doing some form of partitioning to be able to get to that single empty table that I'm loading a lot in and then add the indexes and constraints afterwards but I wouldn't drop indexes and constraints from a table that already has so much data in it the second issue with regard to moving constraints such as foreign key constraints is that it requires a lock on both tables that are involved in that relationship and even though it's fast it is still an exclusive lock on both tables so basically nothing needs to be going on in those tables for it to acquire that exclusive lock the third issue is there happen to be an open transaction reading from the the parent table therefore this exclusive lock couldn't be obtained until the open transaction was closed and lastly there were apparently more than one table that had a foreign key referencing the same parent table and it was tracking the files being loaded in which was doing selects which presumably were wrapped in a transaction and they were dropping constraints so there was basically a whole lot of locking going on that apparently resulted in this sometimes it was slow now apparently she's going to be talking about this more at an upcoming presentation but I'll also recount an issue I ran into where a developer brought something to me and said hey this query is sometimes slow why is that and I went and looked in the database at PG stat statements and everything seemed to be super fast it was a real outlier how slow this query was compared to how PG stat statements was reporting it for its like average execution time so so as I looked into this further it was actually going through PG bouncer that allowed a whole lot of client connections but it server backing connections were relatively constrained and once I looked at the PG bouncer stats I realized that there is some client event weights happening so basically when things got busy there weren't enough connections available in the server pool on the server side of PG Bouncer and those client connections had to wait and essentially that was introducing greater latency execution latency for these queries because they were using an APM to track the performance of those queries so they were seeing that full latency but postc is reporting that everything was fine because the latency was actually happening in PG bouncer so definitely another fact to consider when you're seeing things are sometimes slow but if you want to learn more check out this blog post next piece of content post GRL backup best practice this is from enterpris db. com and the first recommendation he gives is that basically your backup strategy should be part of a more complete Disaster Recovery plan so basically plan for the disasters that you believe could potentially happen and design your backup strategy around that but the particular best practice he highlights is basically use well-known tools for managing backups and the particular tools he has in mind are Barman and PG back PR now I definitely agree these are the most often used comprehensive tool sets for backing up post cres but they also come with a lot of sophistication so I think if you have a large complicated database that isn't being managed by a thirdparty service and they're doing the backups for you these two tools are great options but if you're just starting out with a small database and your recovery needs aren't that robust frankly I don't think there's necessarily any anything wrong with using PG dumps and then when you get more sophisticated you could move it to PG based backup or when you want that point in time recovery you can move up to that as well he definitely doesn't Advocate going this route because he says you don't want to try to write essentially the equivalent functionality of these two using these two postgress backup tools but I would go based upon what is your needs what is your recovery requirements as he states in the first paragraph here and if PG dump get you what you need it's very simple to do because to me the main best practice is can you restore like whatever tool you use as soon as you back up your database you do a restore of it to make sure you can do that that's the main best practice is restore after your backup and if you can restore successfully using PG Dum pgb backup you know any of these other tools then that's the route I would go but what do you think what tool do you use to back up your database or using pg-based backup Barman BG backrest I guess let me know in the comments section next piece of content there was another episode of postr FM last week this one was called the postgres emergency room so basically Nikolai and Michael got together and they discussed when there's some sort of emergency going on with postres it could be the system is down it could be you're trying to restore from back up and it's not working you could have things like txid wraparound issues or an integer being overrun or heaven forbid some type of corruption issue or maybe there's a bad deploy and now suddenly your performance has tanked across the board basically what do you do in emergency situations and they talk about it both from a technical side and from a psychological side so for example if you're having some sort of emergency who is the best person to handle that or call the shots who has the least stress dealing in stressful situations maybe they should be the ones who make the call with regard to what you should do to resolve a particular issue it may not be always be the person who's the most technically minded because maybe they're not the best person to make a call when they're under stress he talked about the importance of monitoring situations and then once you've actually resolved it thoroughly documenting what happened and how to address it and they also listed some resources as guide books or handbooks such as the site reliability engineering resources from Google or the gitlab handbook Sr but if you want to learn more you can definitely listen to the episode here or watch the Youtube video down here next piece of content the 2024 state of postcar SQL survey is now open this is at timescale decom so you can definitely click here to take the survey this year and it will be up until September 30th so basically you have almost a whole month to go ahead and take it but probably best to do it sooner rather than later so you don't forget next piece of content there's another survey this one by Redgate and it's the state of the database landscape and this is at software and.
com so you can click here to take part in the survey so this one doesn't look like it's necessarily postc specific but you can check this out if you're interested next piece of content vacuum managing and detecting bloat this is from cyber techy poost chris. com and this is a pretty comprehensive post about vacuum basically the purpose of vacuum is due to post cr's mvcc basically its multiversion concurrency control and it's how it allows different processes connecting to the database can see different versions of the data based upon what's going on and because it stores that in the actual rows of the database it doesn't have a roll back area like Oracle or Microsoft SQL Server it stores each of those versions in rows within the database so once those versions of data are no longer needed they need to be vacuumed so that's the big point of vacuum is to clean up these dead rows and because of how it operates keep in mind that long transactions that are keeping a connection open and rows being accessed can prevent rows from getting vacuum so basically you want to avoid long transactions as much as you can the other thing he talks about is that when you vacuum something it actually doesn't return space to the operating system there's a chance at the end of the files for some space to be returned but nothing in the middle of the file will be returned and he shows an examples of being able to look at that here so how do you actually free up that space well he mentions one utility PG squeeze which apparently uses logical decoding to basically copy one table to another new table and then change the names another utility you can use is PG repack so that's available as well you can do a vacuum full but of course that locks all access to the table so you want to be careful about using vacuum fold as a means to reduce bloat of the table and lastly he covers how to detect bloat and mainly it's using the PG stat tupal extension here to be able to track how much bloat is present and he closes out talking about hm how can bloat be helpful well when you set up a table you can define a fill factor meaning how empty are the data rows within it and this can actually help with updates particularly Heap only duple updates to give you faster efficient updates to that table so it's something to keep in mind if you're going to be updating frequently a table with a little bit of a Lo could actually be an advantage but if you want to learn more definitely check out this blog post next piece of content understanding the postr hackers mailing list language this is from crunchy dat. com and the postr hackers mailing list this is where development is discussed and takes place with regard to postgres as he says here is very very busy and basically seems like it's drinking from the fire hose so he has some guidance here you know don't try to read everything just skin the subject lines make sure you're using a good email client to go through the mailing list set a routine maybe once a day you're checking out particular portions of messages uh be willing to let threads go and if your backlog gets too big just say all right Mark all of them as red but then he gets into the different jargon that's present here so first he talks about verion control jargon such as diff and commit IDs what rebasing is and head different language with regard to building testing and patching such as GCC and clang what's a commit Fest and then all these different acronyms like CF is the commit Fest CFM is the commit Fest manager and a ton more and then he talks about acronyms used specifically in post development and there seems to be about 50 to 60 different ones here I'll enourage enage you to check out this blog post if you want to learn what those mean as well as common postgres acronyms that you may experience just in talking about the product itself so this is a pretty great reference to have if you're going to be looking at the postcg mailing list to try and translate what some of those things mean so go ahead and check this out if you're interested next piece of content what is PG vector and how can it help you this is from enterpris db.
com and this is a quick summary post of what are vectors what is the PG Vector extension how do you get started using it as well as generating embeddings to be able to do semantic like search with postgres so this is another basic introductory post about that so check it out if you're interested next piece of content in browser semantics AI search with PG light and transformers. JS this is from super. com and this is putting a bunch of things together they're using the embedded data B PG light that can run in a browser they're combining it with the built-in PG Vector of course as well as Transformers JS to do embedding generation and they're wanting to build an in browser product search I don't know if I would necessarily implement this but it is a proof of concept that could use more resources on the client system as opposed to using server resources to do AI based searching but check this out if you're interested and last piece of content is implementing filtered semantic search using PG vector and JavaScript so again using vectors for doing semantic search work although this one is implementing it in JavaScript so check it out if you're interested I hope you enjoyed this episode be sure to check out scaling post.