Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uN28U-00ABaD-1X for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jun 2025 04:23:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1uN28Q-003YJQ-0l for pgsql-hackers@arkaria.postgresql.org; Thu, 05 Jun 2025 04:23:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uN28P-003YJI-Gv for pgsql-hackers@lists.postgresql.org; Thu, 05 Jun 2025 04:23:22 +0000 Received: from mail-ua1-x935.google.com ([2607:f8b0:4864:20::935]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uN28O-000I4G-1I for pgsql-hackers@lists.postgresql.org; Thu, 05 Jun 2025 04:23:21 +0000 Received: by mail-ua1-x935.google.com with SMTP id a1e0cc1a2514c-87decd1c427so197437241.2 for ; Wed, 04 Jun 2025 21:23:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1749097399; x=1749702199; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=0J4Rrj1UoIdHxHLa4/12z6T9tZ5xHIXsFJRUOgfNIyg=; b=UYlC36JPfQ/qydEiJQqepdxaAmn939EeGsXeJhXPRXET0JuEsR8hjZDZU9xo2J7Fjp hhgsYMd4whhRnLez2ExvTGDrx0Vaf7R5XAOw9kalawWGLlwAK78YCogx8jrfcJMTdmjf 3DD0/eMSScR718Am+2P6K/dWJjeiD5uOhoc4MMvYa6yXnw0LZIXGVUIGM4fp8IWE6y6g xRvzSRXjgL4LTrI2vu+R8auU8Y3x8DVvUlYNkzPxoC4WhwpDsfzFHqZnfdqEJCNSjq0/ aS+ZoFP0Gj7Jdlemz7zhlFmSo/Y4026tFK4m6hCqizc5DnmFadgf4+1YY7F/bXy/OV0b kf9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1749097399; x=1749702199; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=0J4Rrj1UoIdHxHLa4/12z6T9tZ5xHIXsFJRUOgfNIyg=; b=i6zbt+dBtFQcMaEBzX6xDGZQTKkCJvjXmnFD5xEWZI/bOTbBt66u+pqZgeF9oNxAFo wDWCxIJSvlAKGH48OUCkaQ1iFTe2NXwIaEWaWmukFNGR73bPOLlEpd8QPLRs6BEFthOF Ge/gyDQSUHRt0+v0y1XcqCaB12sY/td2EZQXSmRvF/WxZupJXjhGBKMUPckJvdjiK3RP 5hPBBL+dOEiuVsQjysM7Ud/WPI3Ym7MQnjeeRYOsrF0W2PChop6j1cBbcGTOpiAOO/7K xMomR3lVpdLLfrePVuSGKfPnZBCjrEz8YAcOMM2iQA7NDEm2+skI7QKntCdTTxQ+7XA+ 2deg== X-Gm-Message-State: AOJu0YwoU1t78CT2i3onW+QMIT1GKbysTu8gVhCnRVVF1Pw7mJ2ku0vh MHM00+BqvK3Txv6OBUKsdahW/lvAXp2x8kUZLBu3eCgDmU8a5MyG3bZXmDWZpxMMIlB4Fw5YqbU XojKPPXi3TRhtdijA57aYlY748HseCLA= X-Gm-Gg: ASbGncvgnKptDRDQSTLS0aE17S1yMx2tRCYIeMAeCt34h9Hoq1tBk1vpdky4ZL8C6Bi Ixp+GKnZLSWrRz+fMDQSZq6nYEN0wJuaCk5GS6B+dqkOKJ7l1rkJxeV7NKctzpqcSqQPM0BHI8k 0qCq1AB3nRRtm2uyqIsWBqa1cqKZT3t6YbwGwlNlfJphwlbYx2mnJXS5HujtkjT5WJdA9j5xvAv A== X-Google-Smtp-Source: AGHT+IH0hFIw/L7xDLWJjXIoo0l8koEdps2bxaRMqasI2NLIkToSblmRNYw1oQZ1Ybdxd2X+wyO9Uc2IdK93jk0eLnk= X-Received: by 2002:a05:6102:4406:b0:4de:c7fe:34b3 with SMTP id ada2fe7eead31-4e7468a7a97mr4722744137.0.1749097398885; Wed, 04 Jun 2025 21:23:18 -0700 (PDT) MIME-Version: 1.0 References: <171085360143.2046436.7217841141682511557.pgcf@coridan.postgresql.org> <859476bf-3cb0-455e-b093-b8ab5ef17f0e@postgrespro.ru> <4e2764f7-f6be-478f-8d7f-f921dbd10f5f@postgrespro.ru> <7fdaf3e8-4c05-4739-bee6-98acc068d1c7@postgrespro.ru> <4433b492-9e97-4334-96d9-ddaaa5083099@postgrespro.ru> <67ecf5fb-e63d-4284-be75-d0479eef81aa@postgrespro.ru> In-Reply-To: <67ecf5fb-e63d-4284-be75-d0479eef81aa@postgrespro.ru> From: jian he Date: Thu, 5 Jun 2025 12:22:40 +0800 X-Gm-Features: AX0GCFtuv4m39-0PnMM9EN7sSKdgxhNpif3k9y8uWsyoHIPGiO9L69ihxUl3qiw Message-ID: Subject: Re: Add SPLIT PARTITION/MERGE PARTITIONS commands To: Dmitry Koval Cc: pgsql-hackers@lists.postgresql.org, Alexander Korotkov Content-Type: multipart/mixed; boundary="00000000000023aa880636cb7913" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --00000000000023aa880636cb7913 Content-Type: text/plain; charset="UTF-8" hi. the following are review of v40-0001-Implement-ALTER-TABLE-.-MERGE-PARTITIONS-.-comma.patch ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022) INTO sales_feb_mar_apr2022; There are no tests when sales_feb2022 or sales_mar2022 have any constraints. a partition can have its own constraint. What should we do when any to be merged partition has constraints? ---------------------------------------------------------------- DROP TABLE IF EXISTS sales_range cascade; CREATE TABLE sales_range (salesperson_id INT, salesperson_name text, sales_amount INT generated always as (1) stored, sales_date DATE) PARTITION BY RANGE (sales_date); CREATE TABLE sales_feb2022 PARTITION OF sales_range FOR VALUES FROM ('2022-02-01') TO ('2022-03-01'); CREATE TABLE sales_mar2022 PARTITION OF sales_range FOR VALUES FROM ('2022-03-01') TO ('2022-04-01'); ALTER TABLE sales_feb2022 ALTER COLUMN sales_amount SET EXPRESSION AS (10); ALTER TABLE sales_mar2022 ALTER COLUMN sales_amount SET EXPRESSION AS (20); ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022) INTO sales_feb_mar2022; with v40, sales_feb_mar2022 column sales_int generated expression is (generated always as (1) stored) maybe this is what we expected. but we should have some tests on it. ---------------------------------------------------------------- DROP TABLE IF EXISTS sales_range cascade; CREATE TABLE sales_range (salesperson_id INT, salesperson_name text, sales_amount INT, sales_date DATE) PARTITION BY RANGE (sales_date); CREATE TABLE sales_feb2022 PARTITION OF sales_range FOR VALUES FROM ('2022-02-01') TO ('2022-03-01'); CREATE TABLE sales_mar2022 PARTITION OF sales_range FOR VALUES FROM ('2022-03-01') TO ('2022-04-01'); CREATE VIEW x AS SELECT * FROM sales_mar2022; ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022) INTO sales_feb_mar2022; ERROR: cannot drop table public.sales_mar2022 because other objects depend on it DETAIL: view public.x depends on table public.sales_mar2022 HINT: Use DROP ... CASCADE to drop the dependent objects too. Maybe this is expected, but we need to mention it somewhere and have some tests on it. saying that MERGE PARTITIONS will effectively drop the partitions, so if any object depends on that partition then MERGE PARTITIONS can not be done. ---------------------------------------------------------------- + */ +static Relation +createPartitionTable(RangeVar *newPartName, Relation modelRel, Oid ownerId) +{ + Relation newRel; + Oid newRelId; + TupleDesc descriptor; + List *colList = NIL; + Oid relamId; + Oid namespaceId; + + /* If existing rel is temp, it must belong to this session */ + if (modelRel->rd_rel->relpersistence == RELPERSISTENCE_TEMP && + !modelRel->rd_islocaltemp) + ereport(ERROR, + errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("cannot create as partition of temporary relation of another session")); Looking at it, modelRel is the partitioned table we called ALTER TABLE. for example: ALTER TABLE sales_range MERGE PARTITIONS (sales_feb2022, sales_mar2022) INTO sales_feb_mar2022; modelRel is sales_range. so this error check can be performed as early as the transformPartitionCmdForMerge stage? ---------------------------------------------------------------- + /* Look up the access method for new relation. */ + relamId = (modelRel->rd_rel->relam != InvalidOid) ? modelRel->rd_rel->relam : HEAP_TABLE_AM_OID; looking at the output of "select * from pg_am;". i think, we can do the following way: if (modelRel->rd_rel->relam) elog(ERROR, "error"); relamId = modelRel->rd_rel->relam; ---------------------------------------------------------------- Attached is some refactoring in moveMergedTablesRows, hope it's straightforward. for example: + /* Extract data from old tuple. */ + slot_getallattrs(srcslot); + + if (tuple_map) + { + /* Need to use map to copy attributes. */ + insertslot = execute_attr_map_slot(tuple_map->attrMap, srcslot, dstslot); + } execute_attr_map_slot will call "slot_getallattrs(srcslot);" so the first one is unncessary. + srcslot = MakeSingleTupleTableSlot(RelationGetDescr(mergingPartition), + table_slot_callbacks(mergingPartition)); can change to srcslot = table_slot_create(mergingPartition, NULL); --00000000000023aa880636cb7913 Content-Type: application/octet-stream; name="v40-minor_refactor_moveMergedTablesRows.no_cfbot" Content-Disposition: attachment; filename="v40-minor_refactor_moveMergedTablesRows.no_cfbot" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_mbivecgq0 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2NvbW1hbmRzL3RhYmxlY21kcy5jIGIvc3JjL2JhY2tl bmQvY29tbWFuZHMvdGFibGVjbWRzLmMKaW5kZXggYjRlYzY2OTFkOGYuLjllMWFjMWU2MjM1IDEw MDY0NAotLS0gYS9zcmMvYmFja2VuZC9jb21tYW5kcy90YWJsZWNtZHMuYworKysgYi9zcmMvYmFj a2VuZC9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