Skip to content

Commit 1b47857

Browse files
committed
[IMP] util.alter_column_type
Detect the case where there is a lot of rows with a NULL value and always change the type using a temporary column. Part-of: #27
1 parent ed13b31 commit 1b47857

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/util/pg.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,19 @@ def alter_column_type(cr, table, column, type, using=None, logger=_logger):
420420
[model_of_table(cr, table), column],
421421
)
422422
if not using:
423-
# Simple case. Use general SQL syntax
424-
cr.execute('ALTER TABLE "%s" ALTER COLUMN "%s" TYPE %s' % (table, column, type))
425-
return
423+
# if there is a high number of NULL entries, it will be faster to just ignore those
424+
cr.execute(format_query(cr, "ANALYZE {}({})", table, column))
425+
cr.execute(
426+
"SELECT null_frac FROM pg_stats WHERE schemaname = current_schema() AND tablename = %s AND attname = %s",
427+
[table, column],
428+
)
429+
[null_frac] = cr.fetchone() or (0.0,)
430+
if null_frac <= 0.70:
431+
# Simple case. Use general SQL syntax
432+
cr.execute(format_query(cr, "ALTER TABLE {} ALTER COLUMN {} TYPE {}", table, column, sql.SQL(type)))
433+
return
434+
435+
using = "{{0}}::{}".format(type)
426436

427437
# else, create a new column and parallel update queries.
428438
cr.execute(

0 commit comments

Comments
 (0)