The main aim of the new patch is to make DBA’s life easier, and make the addition of the new column with defaults as lightweight as possible. It’s important to note that all these steps wouldn’t block access to the table, though, of course, they do take more time than a single heavy ALTER. Third is updating the existing rows and setting the default value into the added column. Second, setting a default value which will be used for new inserted or updated rows. First, adding a new column with no DEFAULT value – this is inexpensive operation. To avoid a prolonged block DBAs often organize such migrations into three stages. Obviously, it’s not acceptable in many cases. Within this time, access to the table is completely blocked, thus any query will get stuck as it’s trying to access the table. But take a closer look at the command’s timing – 4 minutes, this is the time of table’s rewriting. As you can see, file inode has been changed and it means that as a result the table is completely rewritten. In this example, a new integer column has been added with default value. # alter table products add column payload int not null default 0 ALTER TABLE Time: 242439,750 ms (04:02,440) # select count(*),pg_relation_filenode('products'),pg_size_pretty(pg_relation_size('products')) from products count | pg_relation_filenode | pg_size_pretty # select count(*),pg_relation_filenode('products'),pg_size_pretty(pg_relation_size('products')) from products count | pg_relation_filenode | pg_size_pretty But also, as a result of this ALTER, the table is completely rewritten because all tuples must have default value in the new added column. If you do this with one command, it might easily block an access to an altered table. Some of those migrations sometimes are quite painful, for example, adding a new column with automatic default value – ‘ALTER TABLE … ADD COLUMN column_name data_type DEFAULT value‘. It’s not a secret that database lifecycle includes migrations – tasks which change the database schema, such as adding new, changing or removing existing tables, indexes, columns and so on. Reviewed by Tom Lane, Andres Freund, Tomas Vondra and David Rowley. catalog relations) NULL can be passed for thisĪndrew Dunstan, heavily modified from an original patch from Serge In many cases where it is known that there will The most visible code change from this is in heap_attisnull, whichĪcquires a third TupleDesc argument, allowing it to detect a missing Settings for the attributes are cleared, as they are no longer needed. New rows will have the supplied value or the default andĪny time the table is rewritten all the atthasmissing and attmissingval Any existing row when fetched will be supplied with theĪttmissingval. (attmissingval) in pg_attribute, and a new column (atthasmissing) is set The time of the ALTER TABLE and the result stored in a new column This patch removes the need for the rewrite as long as theĭefault value is not volatile. For large tables this can be both expensive andĭisruptive. Commit 16828d5c0273b4fe5f10f42588005f16b415b2d8įast ALTER TABLE ADD COLUMN with a non-NULL defaultĬurrently adding a column to a table with a non-NULL default results inĪ rewrite of the table.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |