![]() However, the situation starts to change if the value we want to add to the column is not constant anymore. Again, there is no need to persist this entry. In this case it can expect the value to be 10. However, if nothing can be found inside a row, PostgreSQL will know what the value is supposed to be. The engine knows that more columns are supposed to be around. ![]() Test=# ALTER TABLE t_sample ADD COLUMN a3 int DEFAULT 10 Īgain the operation is really quick and it does NOT affect the amount of storage we need because this additional column does not make it to disk in the first place: The system column knows that one more column should be on disk – as nothing is present the engine can safely assume that the value is indeed NULL.Ī similar trick can be applied if we add a constant default value: We do need a full table lock BUT it is a really short lock because PostgreSQL does not actually write this column to disk. The important point here is: This operation is really fast because all it does is to run some magic on the system catalog. Test=# ALTER TABLE t_sample ADD COLUMN a2 int The simplest one is to add a column without any default values: We have to address various scenarios here. But what if columns are added? How does it impact the database? Adding a column to a table in PostgreSQL Public | t_sample | table | hs | permanent | heap | 3458 MB | Schema | Name | Type | Owner | Persistence | Access method | Size | Description The initial table is around 3.4 GB in size as shown in the next listing: The following statement creates a simple table which is going to serve as a test dummy for the operations we plan to run:įROM generate_series(1, 100000000) AS a1 Getting started with ALTER TABLE… ADD COLUMN Let’s dive in and see how to run ALTER TABLE … ADD COLUMN in a way that doesn’t hurt operations due to locking or extensive I/O. ![]() Changing data structures is an important issue and happens often, therefore it’s important to understand what is really going on. Running ALTER TABLE … ADD COLUMN can have side effects which have the potential to cause serious issues in production. ADD COLUMN ALTER TABLE data structure lock postgresql random() ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |