Fast ALTER TABLE ... ADD COLUMN ... DEFAULT xxx?
От | Dmitry Koterov |
---|---|
Тема | Fast ALTER TABLE ... ADD COLUMN ... DEFAULT xxx? |
Дата | |
Msg-id | d7df81620905210106h33f6cad4q227279bed4d99fa9@mail.gmail.com обсуждение исходный текст |
Ответы |
Re: Fast ALTER TABLE ... ADD COLUMN ... DEFAULT xxx?
Re: Fast ALTER TABLE ... ADD COLUMN ... DEFAULT xxx? Re: Fast ALTER TABLE ... ADD COLUMN ... DEFAULT xxx? |
Список | pgsql-hackers |
Hello.<br /><br />PostgreSQL is very fast when we perform (even on a huge table)<br /><br />ALTER TABLE ... ADD COLUMN ...NULL;<br /><br />(nullable without a default value). This is because of NULL bitmap in tuples. And it's greatest featurefor a developer!<br /><br /><br />But another very common-case query like<br /><br />ALTER TABLE ... ADD COLUMN ...BOOLEAN NOT NULL DEFAULT false;<br />or<br />ALTER TABLE ... ADD COLUMN ... INT NOT NULL DEFAULT 0;<br /><br />for a hugetable is performed very slow - this is because PostgreSQL have to re-create all tuples assigning the default value tothem. If I have a table with 1 billion rows (for example), I have no chance to perform this query at all - too slow.<br/><br />(In most cases NOT NULL DEFAULT xxx fields are BOOLEAN, flags: it is not handy to have 3-way flags.)<br /><br/><br />So, are there plans to optimize such kind of queries? This could be done by many ways:<br /><br />1. Store theDEFAULT flag directly in NULL BITMAP (add a bit to NULL bitmap not only for NULLable fields, but also for NOT NULL DEFAULT... fields).<br /> 2. Add another bitmap for each tuple (DEFAULT bitmap). Bit value 0 means that there is a real valuein a cell, 1 - that the value is default.<br />3. The same as (1), but always force default value to be 0 (or falseor any other values with meaning "zero") and optimize only these cases.<br /><br /><br />
В списке pgsql-hackers по дате отправления: