Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit68dfecb

Browse files
committed
Use generateClonedIndexStmt to propagate CREATE INDEX to partitions.
When instantiating an existing partitioned index for a new childpartition, we use generateClonedIndexStmt to build a suitableIndexStmt to pass to DefineIndex. However, when DefineIndex needsto recurse to instantiate a newly created partitioned index on anexisting child partition, it was doing copyObject on the givenIndexStmt and then applying a bunch of ad-hoc fixups. This hasa number of problems, primarily that it implies fresh lookups ofreferenced objects such as opclasses and collations. Since commit2af07e2 caused DefineIndex to restrict search_path internally, thoselookups could fail or deliver different results than the original one.We can avoid those problems and save a few dozen lines of code byusing generateClonedIndexStmt in this code path too.Another thing this fixes is incorrect propagation of parent-indexcomments to child indexes (because the copyObject approach copiesthe idxcomment field while generateClonedIndexStmt doesn't). I hadnoticed this in connection with commitc01eb61, but not run theproblem to ground.I'm tempted to back-patch this further than v17, but the only thingit's known to fix in older branches is the comment issue, which ispretty minor and doesn't seem worth the risk of introducing newissues in stable branches. (If anyone does care about that,clearing idxcomment in the copied IndexStmt would be a safer fix.)Per bug #18637 from usamoi. Back-patch to v17 where the search_pathchange came in.Discussion:https://postgr.es/m/18637-f51e314546e3ba2a@postgresql.org
1 parentf9ecb57 commit68dfecb

File tree

7 files changed

+105
-51
lines changed

7 files changed

+105
-51
lines changed

‎contrib/seg/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ PGFILEDESC = "seg - line segment data type"
1414

1515
HEADERS = segdata.h
1616

17-
REGRESS = security seg
17+
REGRESS = security seg partition
1818

1919
EXTRA_CLEAN = segparse.h segparse.c segscan.c
2020

‎contrib/seg/expected/partition.out

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
--
2+
-- Test that partitioned-index operations cope with objects that are
3+
-- not in the secure search path. (This has little to do with seg,
4+
-- but we need an opclass that isn't in pg_catalog, and the base system
5+
-- has no such opclass.) Note that we need to test propagation of the
6+
-- partitioned index's properties both to partitions that pre-date it
7+
-- and to partitions created later.
8+
--
9+
create function mydouble(int) returns int strict immutable parallel safe
10+
begin atomic select $1 * 2; end;
11+
create collation mycollation from "POSIX";
12+
create table pt (category int, sdata seg, tdata text)
13+
partition by list (category);
14+
-- pre-existing partition
15+
create table pt12 partition of pt for values in (1,2);
16+
insert into pt values(1, '0 .. 1'::seg, 'zed');
17+
-- expression references object in public schema
18+
create index pti1 on pt ((mydouble(category) + 1));
19+
-- opclass in public schema
20+
create index pti2 on pt (sdata seg_ops);
21+
-- collation in public schema
22+
create index pti3 on pt (tdata collate mycollation);
23+
-- new partition
24+
create table pt34 partition of pt for values in (3,4);
25+
insert into pt values(4, '-1 .. 1'::seg, 'foo');
26+
\d+ pt
27+
Partitioned table "public.pt"
28+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
29+
----------+---------+-----------+----------+---------+----------+--------------+-------------
30+
category | integer | | | | plain | |
31+
sdata | seg | | | | plain | |
32+
tdata | text | | | | extended | |
33+
Partition key: LIST (category)
34+
Indexes:
35+
"pti1" btree ((mydouble(category) + 1))
36+
"pti2" btree (sdata)
37+
"pti3" btree (tdata COLLATE mycollation)
38+
Partitions: pt12 FOR VALUES IN (1, 2),
39+
pt34 FOR VALUES IN (3, 4)
40+
41+
\d+ pt12
42+
Table "public.pt12"
43+
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
44+
----------+---------+-----------+----------+---------+----------+--------------+-------------
45+
category | integer | | | | plain | |
46+
sdata | seg | | | | plain | |
47+
tdata | text | | | | extended | |
48+
Partition of: pt FOR VALUES IN (1, 2)
49+
Partition constraint: ((category IS NOT NULL) AND (category = ANY (ARRAY[1, 2])))
50+
Indexes:
51+
"pt12_expr_idx" btree ((mydouble(category) + 1))
52+
"pt12_sdata_idx" btree (sdata)
53+
"pt12_tdata_idx" btree (tdata COLLATE mycollation)
54+

‎contrib/seg/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ tests += {
5555
'sql': [
5656
'security',
5757
'seg',
58+
'partition',
5859
],
5960
},
6061
}

‎contrib/seg/sql/partition.sql

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
--
2+
-- Test that partitioned-index operations cope with objects that are
3+
-- not in the secure search path. (This has little to do with seg,
4+
-- but we need an opclass that isn't in pg_catalog, and the base system
5+
-- has no such opclass.) Note that we need to test propagation of the
6+
-- partitioned index's properties both to partitions that pre-date it
7+
-- and to partitions created later.
8+
--
9+
10+
createfunctionmydouble(int) returnsint strict immutable parallel safe
11+
begin atomicselect $1*2; end;
12+
13+
create collation mycollationfrom"POSIX";
14+
15+
createtablept (categoryint, sdata seg, tdatatext)
16+
partition by list (category);
17+
18+
-- pre-existing partition
19+
createtablept12 partition of pt forvaluesin (1,2);
20+
21+
insert into ptvalues(1,'0 .. 1'::seg,'zed');
22+
23+
-- expression references object in public schema
24+
createindexpti1on pt ((mydouble(category)+1));
25+
-- opclass in public schema
26+
createindexpti2on pt (sdata seg_ops);
27+
-- collation in public schema
28+
createindexpti3on pt (tdata collate mycollation);
29+
30+
-- new partition
31+
createtablept34 partition of pt forvaluesin (3,4);
32+
33+
insert into ptvalues(4,'-1 .. 1'::seg,'foo');
34+
35+
\d+ pt
36+
\d+ pt12

‎src/backend/commands/indexcmds.c

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include"optimizer/optimizer.h"
5252
#include"parser/parse_coerce.h"
5353
#include"parser/parse_oper.h"
54+
#include"parser/parse_utilcmd.h"
5455
#include"partitioning/partdesc.h"
5556
#include"pgstat.h"
5657
#include"rewrite/rewriteManip.h"
@@ -1465,55 +1466,20 @@ DefineIndex(Oid tableId,
14651466
*/
14661467
if (!found)
14671468
{
1468-
IndexStmt*childStmt=copyObject(stmt);
1469-
boolfound_whole_row;
1470-
ListCell*lc;
1469+
IndexStmt*childStmt;
14711470
ObjectAddresschildAddr;
14721471

14731472
/*
1474-
*We can't use the same index name forthe child index,
1475-
*so clear idxname to let the recursive invocation choose
1476-
*a new name. Likewise, the existing target relation
1477-
*field is wrong, and if indexOid or oldNumber are set,
1478-
*they mustn't be applied to the child either.
1473+
*Build an IndexStmt describingthedesiredchild index
1474+
*in the same way that we do during ATTACH PARTITION.
1475+
*Notably, we rely on generateClonedIndexStmt to produce
1476+
*a search-path-independent representation, which the
1477+
*original IndexStmt might not be.
14791478
*/
1480-
childStmt->idxname=NULL;
1481-
childStmt->relation=NULL;
1482-
childStmt->indexOid=InvalidOid;
1483-
childStmt->oldNumber=InvalidRelFileNumber;
1484-
childStmt->oldCreateSubid=InvalidSubTransactionId;
1485-
childStmt->oldFirstRelfilelocatorSubid=InvalidSubTransactionId;
1486-
1487-
/*
1488-
* Adjust any Vars (both in expressions and in the index's
1489-
* WHERE clause) to match the partition's column numbering
1490-
* in case it's different from the parent's.
1491-
*/
1492-
foreach(lc,childStmt->indexParams)
1493-
{
1494-
IndexElem*ielem=lfirst(lc);
1495-
1496-
/*
1497-
* If the index parameter is an expression, we must
1498-
* translate it to contain child Vars.
1499-
*/
1500-
if (ielem->expr)
1501-
{
1502-
ielem->expr=
1503-
map_variable_attnos((Node*)ielem->expr,
1504-
1,0,attmap,
1505-
InvalidOid,
1506-
&found_whole_row);
1507-
if (found_whole_row)
1508-
elog(ERROR,"cannot convert whole-row table reference");
1509-
}
1510-
}
1511-
childStmt->whereClause=
1512-
map_variable_attnos(stmt->whereClause,1,0,
1513-
attmap,
1514-
InvalidOid,&found_whole_row);
1515-
if (found_whole_row)
1516-
elog(ERROR,"cannot convert whole-row table reference");
1479+
childStmt=generateClonedIndexStmt(NULL,
1480+
parentIndex,
1481+
attmap,
1482+
NULL);
15171483

15181484
/*
15191485
* Recurse as the starting user ID. Callee will use that

‎src/test/regress/expected/alter_table.out

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,7 +2215,6 @@ select conname, obj_description(oid, 'pg_constraint') as desc
22152215
(3 rows)
22162216

22172217
alter table at_partitioned alter column name type varchar(127);
2218-
-- Note: these tests currently show the wrong behavior for comments :-(
22192218
select relname,
22202219
c.oid = oldoid as orig_oid,
22212220
case relfilenode
@@ -2232,9 +2231,9 @@ select relname,
22322231
------------------------------+----------+---------+--------------
22332232
at_partitioned | t | none |
22342233
at_partitioned_0 | t | own |
2235-
at_partitioned_0_id_name_key | f | own |parent index
2234+
at_partitioned_0_id_name_key | f | own |
22362235
at_partitioned_1 | t | own |
2237-
at_partitioned_1_id_name_key | f | own |parent index
2236+
at_partitioned_1_id_name_key | f | own |
22382237
at_partitioned_id_name_key | f | none | parent index
22392238
(6 rows)
22402239

‎src/test/regress/sql/alter_table.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1496,8 +1496,6 @@ select conname, obj_description(oid, 'pg_constraint') as desc
14961496

14971497
altertable at_partitioned alter column name typevarchar(127);
14981498

1499-
-- Note: these tests currently show the wrong behavior for comments :-(
1500-
15011499
select relname,
15021500
c.oid= oldoidas orig_oid,
15031501
case relfilenode

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp