• Tom Lane's avatar
    Avoid order-of-execution problems with ALTER TABLE ADD PRIMARY KEY. · f4a3fdfb
    Tom Lane authored
    Up to now, DefineIndex() was responsible for adding attnotnull constraints
    to the columns of a primary key, in any case where it hadn't been
    convenient for transformIndexConstraint() to mark those columns as
    is_not_null.  It (or rather its minion index_check_primary_key) did this
    by executing an ALTER TABLE SET NOT NULL command for the target table.
    
    The trouble with this solution is that if we're creating the index due
    to ALTER TABLE ADD PRIMARY KEY, and the outer ALTER TABLE has additional
    sub-commands, the inner ALTER TABLE's operations executed at the wrong
    time with respect to the outer ALTER TABLE's operations.  In particular,
    the inner ALTER would perform a validation scan at a point where the
    table's storage might be inconsistent with its catalog entries.  (This is
    on the hairy edge of being a security problem, but AFAICS it isn't one
    because the inner scan would only be interested in the tuples' null
    bitmaps.)  This can result in unexpected failures, such as the one seen
    in bug #15580 from Allison Kaptur.
    
    To fix, let's remove the attempt to do SET NOT NULL from DefineIndex(),
    reducing index_check_primary_key's role to verifying that the columns are
    already not null.  (It shouldn't ever see such a case, but it seems wise
    to keep the check for safety.)  Instead, make transformIndexConstraint()
    generate ALTER TABLE SET NOT NULL subcommands to be executed ahead of
    the ADD PRIMARY KEY operation in every case where it can't force the
    column to be created already-not-null.  This requires only minor surgery
    in parse_utilcmd.c, and it makes for a much more satisfying spec for
    transformIndexConstraint(): it's no longer having to take it on faith
    that someone else will handle addition of NOT NULL constraints.
    
    To make that work, we have to move the execution of AT_SetNotNull into
    an ALTER pass that executes ahead of AT_PASS_ADD_INDEX.  I moved it to
    AT_PASS_COL_ATTRS, and put that after AT_PASS_ADD_COL to avoid failure
    when the column is being added in the same command.  This incidentally
    fixes a bug in the only previous usage of AT_PASS_COL_ATTRS, for
    AT_SetIdentity: it didn't work either for a newly-added column.
    
    Playing around with this exposed a separate bug in ALTER TABLE ONLY ...
    ADD PRIMARY KEY for partitioned tables.  The intent of the ONLY modifier
    in that context is to prevent doing anything that would require holding
    lock for a long time --- but the implied SET NOT NULL would recurse to
    the child partitions, and do an expensive validation scan for any child
    where the column(s) were not already NOT NULL.  To fix that, invent a
    new ALTER subcommand AT_CheckNotNull that just insists that a child
    column be already NOT NULL, and apply that, not AT_SetNotNull, when
    recursing to children in this scenario.  This results in a slightly laxer
    definition of ALTER TABLE ONLY ... SET NOT NULL for partitioned tables,
    too: that command will now work as long as all children are already NOT
    NULL, whereas before it just threw up its hands if there were any
    partitions.
    
    In passing, clean up the API of generateClonedIndexStmt(): remove a
    useless argument, ensure that the output argument is not left undefined,
    update the header comment.
    
    A small side effect of this change is that no-such-column errors in ALTER
    TABLE ADD PRIMARY KEY now produce a different message that includes the
    table name, because they are now detected by the SET NOT NULL step which
    has historically worded its error that way.  That seems fine to me, so
    I didn't make any effort to avoid the wording change.
    
    The basic bug #15580 is of very long standing, and these other bugs
    aren't new in v12 either.  However, this is a pretty significant change
    in the way ALTER TABLE ADD PRIMARY KEY works.  On balance it seems best
    not to back-patch, at least not till we get some more confidence that
    this patch has no new bugs.
    
    Patch by me, but thanks to Jie Zhang for a preliminary version.
    
    Discussion: https://postgr.es/m/15580-d1a6de5a3d65da51@postgresql.org
    Discussion: https://postgr.es/m/1396E95157071C4EBBA51892C5368521017F2E6E63@G08CNEXMBPEKD02.g08.fujitsu.local
    f4a3fdfb
alter_table.out 1.14 KB