Re: [HACKERS] A bug in mapping attributes in ATExecAttachPartition()

Поиск
Список
Период
Сортировка
От Amit Langote
Тема Re: [HACKERS] A bug in mapping attributes in ATExecAttachPartition()
Дата
Msg-id 6605e527-cc51-0f1d-3877-4f3d3214ccd3@lab.ntt.co.jp
обсуждение исходный текст
Ответ на Re: [HACKERS] A bug in mapping attributes in ATExecAttachPartition()  (Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>)
Ответы Re: [HACKERS] A bug in mapping attributes in ATExecAttachPartition()  (Ashutosh Bapat <ashutosh.bapat@enterprisedb.com>)
Список pgsql-hackers
Thanks for the review.

On 2017/06/15 16:08, Ashutosh Bapat wrote:
> On Thu, Jun 15, 2017 at 10:46 AM, Amit Langote wrote:
>> If we end up having to perform the validation scan and the table being
>> attached is a partitioned table, we will scan its leaf partitions.  Each
>> of those leaf partitions may have different attribute numbers for the
>> partitioning columns, so we will need to do the mapping, which actually we
>> do even today.  With this patch however, we apply mapping to the generated
>> partition constraint so that it no longer bears the original parent's
>> attribute numbers but those of the table being attached.  Down below where
>> we map to the leaf partition's attribute numbers, we still pass the root
>> partitioned table as the parent.  But it may so happen that the attnos
>> appearing in the Vars can no longer be matched with any of the root
>> table's attribute numbers, resulting in the following code in
>> map_variable_attnos_mutator() to trigger an error:
>>
>> if (attno > context->map_length || context->attno_map[attno - 1] == 0)
>>     elog(ERROR, "unexpected varattno %d in expression to be mapped",
>>                  attno);
>>
>> Consider this example:
>>
>> root: (a, b, c) partition by list (a)
>> intermediate: (b, c, ..dropped.., a) partition by list (b)
>> leaf: (b, c, a) partition of intermediate
>>
>> When attaching intermediate to root, we will generate the partition
>> constraint and after mapping, its Vars will have attno = 4.  When trying
>> to map the same for leaf, we currently do map_partition_varattnos(expr, 1,
>> leaf, root).  So, the innards of map_variable_attnos will try to look for
>> an attribute with attno = 4 in root which there isn't, so the above error
>> will occur.  We should really be passing intermediate as parent to the
>> mapping routine.  With the previous patch's approach, we would pass root
>> as the parent along with partConstraintOrig which would bear the root
>> parent's attnos.
> 
> Thanks for the explanation. So, your earlier patch did map Vars
> correctly for the leaf partitions of the table being attached.

Yes I think.

>> Please find attached the updated patch.  In addition to the already
>> described fixes, the patch also rearranges code so that a redundant AT
>> work queue entry is avoided.  (Currently, we end up creating one for
>> attachRel even if it's partitioned, although it's harmless because
>> ATRewriteTables() knows to skip partitioned tables.)
> 
> We are calling find_all_inheritors() on attachRel twice in this function, once
> to avoid circularity and second time for scheduling a scan. Why can't call it
> once and reuse the result?

Hmm, avoiding calling it twice would be a good idea.

Also, I noticed that there might be a deadlock hazard here due to the
lock-strength-upgrade thing happening here across the two calls.  In the
first call to find_all_inheritors(), we request AccessShareLock, whereas
in the second, an AccessExclusiveLock.  That ought to be fixed anyway I'd
think.

So, the first (and the only after this change) call will request an
AccessExclusiveLock, even though we may not scan the leaf partitions if a
suitable constraint exists on the table being attached.  Note that
previously, we would not have exclusive-locked the leaf partitions in such
a case, although it was deadlock-prone/buggy anyway.

The updated patch includes this fix.

> On the default partitioning thread [1] Robert commented that we should try to
> avoid queueing the subpartitions which have constraints that imply the new
> partitioning constraint. I think that comment applies to this code, which the
> refactoring patch has moved into a function. If you do this, instead of
> duplicating the code to gather existing constraints, please create a function
> for gathering constraints of a given relation and use it for the table being
> attached as well as its partitions. Also, we should avoid matching
> constraints for the table being attached twice, when it's not
> partitioned.

I guess you are talking about the case where the table being attached
itself does not have a check constraint that would help avoid the scan,
but its individual leaf partitions (if any) may.

> Both of the above comments are not related to the bug that is being fixed, but
> they apply to the same code where the bug exists. So instead of fixing it
> twice, may be we should expand the scope of this work to cover other
> refactoring needed in this area. That might save us some rebasing and commits.

Are you saying that the patch posted on that thread should be brought over
and discussed here?  I tend to agree if that helps avoid muddying the
default partition discussion with this refactoring work.

> 
> +            /*
> +             * Adjust the constraint to match this partition.
> +             *
> +             * Since partConstraint contains attachRel's attnos due to the
> +             * mapping we did just before attempting the proof above, we pass
> +             * attachRel as the parent to map_partition_varattnos, not 'rel'
> +             * which is the root parent.
> +             */
> May be reworded as "Adjust the partition constraints constructed for the table
> being attached for the leaf partition being validated."

Done, although worded slightly differently: Adjust the constraint that we
constructed above for the table being attached so that it matches this
partition's attribute numbers.

> +CREATE TABLE part_7 (
> +    a int,
> +    b char,
> +    CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 7)
> +) PARTITION BY LIST (b);
> +CREATE TABLE part_7_a_null (
> +    c int,
> +    d int,
> +    b char,
> +    a int,    -- 'a' will have attnum = 4
> +    CONSTRAINT check_b CHECK (b IS NULL OR b = 'a'),
> +    CONSTRAINT check_a CHECK (a IS NOT NULL AND a = 7)
> +);
> Why not to use LIKE list_parted as you have done for part_6?

Sure, done.

Please find the updated patch.

Thanks,
Amit

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Вложения

В списке pgsql-hackers по дате отправления:

Предыдущее
От: Etsuro Fujita
Дата:
Сообщение: [HACKERS] Missing comment for create_modifytable_path
Следующее
От: Ashutosh Bapat
Дата:
Сообщение: Re: [HACKERS] A bug in mapping attributes in ATExecAttachPartition()