Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 68dfecb

Browse files
committed
Use generateClonedIndexStmt to propagate CREATE INDEX to partitions.
When instantiating an existing partitioned index for a new child partition, we use generateClonedIndexStmt to build a suitable IndexStmt to pass to DefineIndex. However, when DefineIndex needs to recurse to instantiate a newly created partitioned index on an existing child partition, it was doing copyObject on the given IndexStmt and then applying a bunch of ad-hoc fixups. This has a number of problems, primarily that it implies fresh lookups of referenced objects such as opclasses and collations. Since commit 2af07e2 caused DefineIndex to restrict search_path internally, those lookups could fail or deliver different results than the original one. We can avoid those problems and save a few dozen lines of code by using generateClonedIndexStmt in this code path too. Another thing this fixes is incorrect propagation of parent-index comments to child indexes (because the copyObject approach copies the idxcomment field while generateClonedIndexStmt doesn't). I had noticed this in connection with commit c01eb61, but not run the problem to ground. I'm tempted to back-patch this further than v17, but the only thing it's known to fix in older branches is the comment issue, which is pretty minor and doesn't seem worth the risk of introducing new issues 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_path change came in. Discussion: https://postgr.es/m/18637-f51e314546e3ba2a@postgresql.org
1 parent f9ecb57 commit 68dfecb

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+
create function mydouble(int) returns int strict immutable parallel safe
11+
begin atomic select $1 * 2; end;
12+
13+
create collation mycollation from "POSIX";
14+
15+
create table pt (category int, sdata seg, tdata text)
16+
partition by list (category);
17+
18+
-- pre-existing partition
19+
create table pt12 partition of pt for values in (1,2);
20+
21+
insert into pt values(1, '0 .. 1'::seg, 'zed');
22+
23+
-- expression references object in public schema
24+
create index pti1 on pt ((mydouble(category) + 1));
25+
-- opclass in public schema
26+
create index pti2 on pt (sdata seg_ops);
27+
-- collation in public schema
28+
create index pti3 on pt (tdata collate mycollation);
29+
30+
-- new partition
31+
create table pt34 partition of pt for values in (3,4);
32+
33+
insert into pt values(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-
bool found_whole_row;
1470-
ListCell *lc;
1469+
IndexStmt *childStmt;
14711470
ObjectAddress childAddr;
14721471

14731472
/*
1474-
* We can't use the same index name for the 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 describing the desired child 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
alter table at_partitioned alter column name type varchar(127);
14981498

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

0 commit comments

Comments
 (0)