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

Commit1bb5e78

Browse files
committed
Move code for managing PartitionDescs into a new file, partdesc.c
This is similar in spirit to the existing partbounds.c file in thesame directory, except that there's a lot less code in the new filecreated by this commit. Pending work in this area proposes to add abunch more code related to PartitionDescs, though, and this will giveus a good place to put it.Discussion:http://postgr.es/m/CA+TgmoZUwPf_uanjF==gTGBMJrn8uCq52XYvAEorNkLrUdoawg@mail.gmail.com
1 parent9eefba1 commit1bb5e78

File tree

17 files changed

+273
-218
lines changed

17 files changed

+273
-218
lines changed

‎src/backend/catalog/heap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#include"parser/parse_collate.h"
7070
#include"parser/parse_expr.h"
7171
#include"parser/parse_relation.h"
72+
#include"partitioning/partdesc.h"
7273
#include"storage/lmgr.h"
7374
#include"storage/predicate.h"
7475
#include"storage/smgr.h"

‎src/backend/catalog/partition.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -253,22 +253,6 @@ has_partition_attrs(Relation rel, Bitmapset *attnums, bool *used_in_expr)
253253
return false;
254254
}
255255

256-
/*
257-
* get_default_oid_from_partdesc
258-
*
259-
* Given a partition descriptor, return the OID of the default partition, if
260-
* one exists; else, return InvalidOid.
261-
*/
262-
Oid
263-
get_default_oid_from_partdesc(PartitionDescpartdesc)
264-
{
265-
if (partdesc&&partdesc->boundinfo&&
266-
partition_bound_has_default(partdesc->boundinfo))
267-
returnpartdesc->oids[partdesc->boundinfo->default_index];
268-
269-
returnInvalidOid;
270-
}
271-
272256
/*
273257
* get_default_partition_oid
274258
*

‎src/backend/catalog/pg_constraint.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include"catalog/dependency.h"
2525
#include"catalog/indexing.h"
2626
#include"catalog/objectaccess.h"
27-
#include"catalog/partition.h"
2827
#include"catalog/pg_constraint.h"
2928
#include"catalog/pg_operator.h"
3029
#include"catalog/pg_type.h"

‎src/backend/commands/indexcmds.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include"catalog/catalog.h"
2525
#include"catalog/index.h"
2626
#include"catalog/indexing.h"
27-
#include"catalog/partition.h"
2827
#include"catalog/pg_am.h"
2928
#include"catalog/pg_constraint.h"
3029
#include"catalog/pg_inherits.h"
@@ -46,6 +45,7 @@
4645
#include"parser/parse_coerce.h"
4746
#include"parser/parse_func.h"
4847
#include"parser/parse_oper.h"
48+
#include"partitioning/partdesc.h"
4949
#include"rewrite/rewriteManip.h"
5050
#include"storage/lmgr.h"
5151
#include"storage/proc.h"

‎src/backend/commands/tablecmds.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include"parser/parse_utilcmd.h"
7575
#include"parser/parser.h"
7676
#include"partitioning/partbounds.h"
77+
#include"partitioning/partdesc.h"
7778
#include"pgstat.h"
7879
#include"rewrite/rewriteDefine.h"
7980
#include"rewrite/rewriteHandler.h"

‎src/backend/commands/trigger.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include"parser/parse_func.h"
4343
#include"parser/parse_relation.h"
4444
#include"parser/parsetree.h"
45+
#include"partitioning/partdesc.h"
4546
#include"pgstat.h"
4647
#include"rewrite/rewriteManip.h"
4748
#include"storage/bufmgr.h"

‎src/backend/executor/execPartition.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include"miscadmin.h"
2525
#include"nodes/makefuncs.h"
2626
#include"partitioning/partbounds.h"
27+
#include"partitioning/partdesc.h"
2728
#include"partitioning/partprune.h"
2829
#include"rewrite/rewriteManip.h"
2930
#include"utils/lsyscache.h"

‎src/backend/optimizer/util/inherit.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include"optimizer/inherit.h"
2424
#include"optimizer/planner.h"
2525
#include"optimizer/prep.h"
26+
#include"partitioning/partdesc.h"
2627
#include"utils/rel.h"
2728

2829

‎src/backend/optimizer/util/plancat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
#include"catalog/catalog.h"
2828
#include"catalog/dependency.h"
2929
#include"catalog/heap.h"
30-
#include"catalog/partition.h"
3130
#include"catalog/pg_am.h"
3231
#include"catalog/pg_proc.h"
3332
#include"catalog/pg_statistic_ext.h"
@@ -41,6 +40,7 @@
4140
#include"optimizer/plancat.h"
4241
#include"optimizer/prep.h"
4342
#include"partitioning/partbounds.h"
43+
#include"partitioning/partdesc.h"
4444
#include"parser/parse_relation.h"
4545
#include"parser/parsetree.h"
4646
#include"rewrite/rewriteManip.h"

‎src/backend/partitioning/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ subdir = src/backend/partitioning
1212
top_builddir = ../../..
1313
include$(top_builddir)/src/Makefile.global
1414

15-
OBJS =partprune.opartbounds.o
15+
OBJS =partbounds.opartdesc.o partprune.o
1616

1717
include$(top_srcdir)/src/backend/common.mk

‎src/backend/partitioning/partbounds.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
* src/backend/partitioning/partbounds.c
1111
*
1212
*-------------------------------------------------------------------------
13-
*/
13+
*/
14+
1415
#include"postgres.h"
1516

1617
#include"access/heapam.h"
@@ -23,8 +24,9 @@
2324
#include"nodes/makefuncs.h"
2425
#include"nodes/nodeFuncs.h"
2526
#include"parser/parse_coerce.h"
26-
#include"partitioning/partprune.h"
2727
#include"partitioning/partbounds.h"
28+
#include"partitioning/partdesc.h"
29+
#include"partitioning/partprune.h"
2830
#include"utils/builtins.h"
2931
#include"utils/datum.h"
3032
#include"utils/fmgroids.h"

‎src/backend/partitioning/partdesc.c

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* partdesc.c
4+
*Support routines for manipulating partition descriptors
5+
*
6+
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
* IDENTIFICATION
10+
* src/backend/partitioning/partdesc.c
11+
*
12+
*-------------------------------------------------------------------------
13+
*/
14+
15+
#include"postgres.h"
16+
17+
#include"catalog/partition.h"
18+
#include"catalog/pg_inherits.h"
19+
#include"partitioning/partbounds.h"
20+
#include"partitioning/partdesc.h"
21+
#include"utils/builtins.h"
22+
#include"utils/lsyscache.h"
23+
#include"utils/memutils.h"
24+
#include"utils/rel.h"
25+
#include"utils/partcache.h"
26+
#include"utils/syscache.h"
27+
28+
/*
29+
* RelationBuildPartitionDesc
30+
*Form rel's partition descriptor
31+
*
32+
* Not flushed from the cache by RelationClearRelation() unless changed because
33+
* of addition or removal of partition.
34+
*/
35+
void
36+
RelationBuildPartitionDesc(Relationrel)
37+
{
38+
PartitionDescpartdesc;
39+
PartitionBoundInfoboundinfo=NULL;
40+
List*inhoids;
41+
PartitionBoundSpec**boundspecs=NULL;
42+
Oid*oids=NULL;
43+
ListCell*cell;
44+
inti,
45+
nparts;
46+
PartitionKeykey=RelationGetPartitionKey(rel);
47+
MemoryContextoldcxt;
48+
int*mapping;
49+
50+
/* Get partition oids from pg_inherits */
51+
inhoids=find_inheritance_children(RelationGetRelid(rel),NoLock);
52+
nparts=list_length(inhoids);
53+
54+
if (nparts>0)
55+
{
56+
oids=palloc(nparts*sizeof(Oid));
57+
boundspecs=palloc(nparts*sizeof(PartitionBoundSpec*));
58+
}
59+
60+
/* Collect bound spec nodes for each partition */
61+
i=0;
62+
foreach(cell,inhoids)
63+
{
64+
Oidinhrelid=lfirst_oid(cell);
65+
HeapTupletuple;
66+
Datumdatum;
67+
boolisnull;
68+
PartitionBoundSpec*boundspec;
69+
70+
tuple=SearchSysCache1(RELOID,inhrelid);
71+
if (!HeapTupleIsValid(tuple))
72+
elog(ERROR,"cache lookup failed for relation %u",inhrelid);
73+
74+
datum=SysCacheGetAttr(RELOID,tuple,
75+
Anum_pg_class_relpartbound,
76+
&isnull);
77+
if (isnull)
78+
elog(ERROR,"null relpartbound for relation %u",inhrelid);
79+
boundspec=stringToNode(TextDatumGetCString(datum));
80+
if (!IsA(boundspec,PartitionBoundSpec))
81+
elog(ERROR,"invalid relpartbound for relation %u",inhrelid);
82+
83+
/*
84+
* Sanity check: If the PartitionBoundSpec says this is the default
85+
* partition, its OID should correspond to whatever's stored in
86+
* pg_partitioned_table.partdefid; if not, the catalog is corrupt.
87+
*/
88+
if (boundspec->is_default)
89+
{
90+
Oidpartdefid;
91+
92+
partdefid=get_default_partition_oid(RelationGetRelid(rel));
93+
if (partdefid!=inhrelid)
94+
elog(ERROR,"expected partdefid %u, but got %u",
95+
inhrelid,partdefid);
96+
}
97+
98+
oids[i]=inhrelid;
99+
boundspecs[i]=boundspec;
100+
++i;
101+
ReleaseSysCache(tuple);
102+
}
103+
104+
/* Now build the actual relcache partition descriptor */
105+
rel->rd_pdcxt=AllocSetContextCreate(CacheMemoryContext,
106+
"partition descriptor",
107+
ALLOCSET_DEFAULT_SIZES);
108+
MemoryContextCopyAndSetIdentifier(rel->rd_pdcxt,
109+
RelationGetRelationName(rel));
110+
111+
oldcxt=MemoryContextSwitchTo(rel->rd_pdcxt);
112+
partdesc= (PartitionDescData*)palloc0(sizeof(PartitionDescData));
113+
partdesc->nparts=nparts;
114+
/* oids and boundinfo are allocated below. */
115+
116+
MemoryContextSwitchTo(oldcxt);
117+
118+
if (nparts==0)
119+
{
120+
rel->rd_partdesc=partdesc;
121+
return;
122+
}
123+
124+
/* First create PartitionBoundInfo */
125+
boundinfo=partition_bounds_create(boundspecs,nparts,key,&mapping);
126+
127+
/* Now copy boundinfo and oids into partdesc. */
128+
oldcxt=MemoryContextSwitchTo(rel->rd_pdcxt);
129+
partdesc->boundinfo=partition_bounds_copy(boundinfo,key);
130+
partdesc->oids= (Oid*)palloc(partdesc->nparts*sizeof(Oid));
131+
partdesc->is_leaf= (bool*)palloc(partdesc->nparts*sizeof(bool));
132+
133+
/*
134+
* Now assign OIDs from the original array into mapped indexes of the
135+
* result array. The order of OIDs in the former is defined by the
136+
* catalog scan that retrieved them, whereas that in the latter is defined
137+
* by canonicalized representation of the partition bounds.
138+
*/
139+
for (i=0;i<partdesc->nparts;i++)
140+
{
141+
intindex=mapping[i];
142+
143+
partdesc->oids[index]=oids[i];
144+
/* Record if the partition is a leaf partition */
145+
partdesc->is_leaf[index]=
146+
(get_rel_relkind(oids[i])!=RELKIND_PARTITIONED_TABLE);
147+
}
148+
MemoryContextSwitchTo(oldcxt);
149+
150+
rel->rd_partdesc=partdesc;
151+
}
152+
153+
/*
154+
* equalPartitionDescs
155+
*Compare two partition descriptors for logical equality
156+
*/
157+
bool
158+
equalPartitionDescs(PartitionKeykey,PartitionDescpartdesc1,
159+
PartitionDescpartdesc2)
160+
{
161+
inti;
162+
163+
if (partdesc1!=NULL)
164+
{
165+
if (partdesc2==NULL)
166+
return false;
167+
if (partdesc1->nparts!=partdesc2->nparts)
168+
return false;
169+
170+
Assert(key!=NULL||partdesc1->nparts==0);
171+
172+
/*
173+
* Same oids? If the partitioning structure did not change, that is,
174+
* no partitions were added or removed to the relation, the oids array
175+
* should still match element-by-element.
176+
*/
177+
for (i=0;i<partdesc1->nparts;i++)
178+
{
179+
if (partdesc1->oids[i]!=partdesc2->oids[i])
180+
return false;
181+
}
182+
183+
/*
184+
* Now compare partition bound collections. The logic to iterate over
185+
* the collections is private to partition.c.
186+
*/
187+
if (partdesc1->boundinfo!=NULL)
188+
{
189+
if (partdesc2->boundinfo==NULL)
190+
return false;
191+
192+
if (!partition_bounds_equal(key->partnatts,key->parttyplen,
193+
key->parttypbyval,
194+
partdesc1->boundinfo,
195+
partdesc2->boundinfo))
196+
return false;
197+
}
198+
elseif (partdesc2->boundinfo!=NULL)
199+
return false;
200+
}
201+
elseif (partdesc2!=NULL)
202+
return false;
203+
204+
return true;
205+
}
206+
207+
/*
208+
* get_default_oid_from_partdesc
209+
*
210+
* Given a partition descriptor, return the OID of the default partition, if
211+
* one exists; else, return InvalidOid.
212+
*/
213+
Oid
214+
get_default_oid_from_partdesc(PartitionDescpartdesc)
215+
{
216+
if (partdesc&&partdesc->boundinfo&&
217+
partition_bound_has_default(partdesc->boundinfo))
218+
returnpartdesc->oids[partdesc->boundinfo->default_index];
219+
220+
returnInvalidOid;
221+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp