Re: speedup COPY TO for partitioned table. - Mailing list pgsql-hackers

From Álvaro Herrera
Subject Re: speedup COPY TO for partitioned table.
Date
Msg-id 202507141402.rot5glg7movr@alvherre.pgsql
Whole thread Raw
In response to Re: speedup COPY TO for partitioned table.  (jian he <jian.universality@gmail.com>)
Responses Re: speedup COPY TO for partitioned table.
List pgsql-hackers
On 2025-Jul-02, jian he wrote:

> @@ -673,11 +680,34 @@ BeginCopyTo(ParseState *pstate,
>                       errmsg("cannot copy from sequence \"%s\"",
>                              RelationGetRelationName(rel))));
>          else if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
> -            ereport(ERROR,
> -                    (errcode(ERRCODE_WRONG_OBJECT_TYPE),
> -                     errmsg("cannot copy from partitioned table \"%s\"",
> -                            RelationGetRelationName(rel)),
> -                     errhint("Try the COPY (SELECT ...) TO variant.")));
> +        {
> +            children = find_all_inheritors(RelationGetRelid(rel),
> +                                           AccessShareLock,
> +                                           NULL);
> +
> +            foreach_oid(childreloid, children)
> +            {
> +                char         relkind = get_rel_relkind(childreloid);
> +
> +                if (relkind == RELKIND_FOREIGN_TABLE)
> +                {
> +                    char       *relation_name;
> +
> +                    relation_name = get_rel_name(childreloid);
> +                    ereport(ERROR,
> +                            errcode(ERRCODE_WRONG_OBJECT_TYPE),
> +                            errmsg("cannot copy from foreign table \"%s\"", relation_name),
> +                            errdetail("Partition \"%s\" is a foreign table in the partitioned table \"%s\"",
> +                                      relation_name, RelationGetRelationName(rel)),
> +                            errhint("Try the COPY (SELECT ...) TO variant."));
> +                }

This code looks like it's duplicating what you could obtain by using
RelationGetPartitionDesc and then observe the ->isleaf bits.  Maybe have
a look at the function RelationHasForeignPartition() in the patch at
https://postgr.es/m/CANhcyEW_s2LD6RiDSMHtWQnpYB67EWXqf7N8mn7dOrnaKMfROg@mail.gmail.com
which looks very similar to what you need here.  I think that would also
have the (maybe dubious) advantage that the rows will be output in
partition bound order rather than breadth-first (partition hierarchy)
OID order.

-- 
Álvaro Herrera               48°01'N 7°57'E  —  https://www.EnterpriseDB.com/
"The saddest aspect of life right now is that science gathers knowledge faster
 than society gathers wisdom."  (Isaac Asimov)



pgsql-hackers by date:

Previous
From: Dmitry Dolgov
Date:
Subject: Re: Changing shared_buffers without restart
Next
From: Aleksander Alekseev
Date:
Subject: Re: Missing NULL check after calling ecpg_strdup