Re: BUG #16276: Server crash on an invalid attempt to attach a partition to an index

Amit Langote <amitlangote09@gmail.com>

From: Amit Langote <amitlangote09@gmail.com>
To: Michael Paquier <michael@paquier.xyz>
Cc: exclusion@gmail.com, PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Date: 2020-02-27T08:14:28Z
Lists: pgsql-bugs
On Thu, Feb 27, 2020 at 11:31 AM Michael Paquier <michael@paquier.xyz> wrote:
> On Wed, Feb 26, 2020 at 05:06:24PM +0900, Michael Paquier wrote:
> > Attempting to attach a table to a partitioned index?  Nice thought.
> > Without the assertion, RangeVarCallbackForAttachIndex complains that
> > the relation is not an index, which is right, so I would be tempted to
> > just remove the culprit assertion.  Any thoughts?
>
> FWIW, one can note that ALTER TABLE ATTACH PARTITION with indexes
> correctly registers partition indexes without partition bounds in
> pg_class, even if the grammar cannot accept ALTER TABLE without any
> bounds defined:
> create table idxpart(a int) partition by list (a);
> create table idxpart1 partition of idxpart for values in (1);
> create index idxpart_idx on only idxpart(a);
> create index idxpart1_idx on only idxpart1(a);
> alter table idxpart_idx attach partition idxpart1_idx for values in (0);
>
> So I would rather not issue an error in that case on compatibility
> ground.

Simply dropping the culprit assertion might be enough for
back-branches, but it seems misleading to silently ignore the bound
IMO.  Maybe, report an error in newer versions, as follows:

@@ -3698,8 +3698,17 @@ transformPartitionCmd(CreateStmtContext *cxt,
PartitionCmd *cmd)
                                                          cmd->bound);
             break;
         case RELKIND_PARTITIONED_INDEX:
-            /* nothing to check */
-            Assert(cmd->bound == NULL);
+            /*
+             * ALTER INDEX, which does not allow partition bound to be
+             * specified, must be used when trying to attach partition of an
+             * index.
+             */
+            if (cmd->bound != NULL)
+                ereport(ERROR,
+                        (errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+                         errmsg("\"%s\" is not a partitioned table",
+                                RelationGetRelationName(parentRel)),
+                         errhint("Use ALTER INDEX to attach index
partition.")));

?

So,

create table idxpart(a int) partition by list (a);
create index idxpart_idx on idxpart (a);
create table idxpart1(a int);
alter table idxpart_idx attach partition idxpart1 for values in (0);
ERROR:  "idxpart_idx" is not a partitioned table
HINT:  Use ALTER INDEX to attach index partition.

Thanks,
Amit



Commits

  1. Fix assertion failure with ALTER TABLE ATTACH PARTITION and indexes