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

Commit19c47e7

Browse files
committed
Factor error generation out of ExecPartitionCheck.
At present, we always raise an ERROR if the partition constraintis violated, but a pending patch for UPDATE tuple routing willconsider instead moving the tuple to the correct partition.Refactor to make that simpler.Amit Khandekar, reviewed by Amit Langote, David Rowley, and me.Discussion:http://postgr.es/m/CAJ3gD9cue54GbEzfV-61nyGpijvjZgCcghvLsB0_nL8Nm8HzCA@mail.gmail.com
1 parent84a6f63 commit19c47e7

File tree

6 files changed

+74
-55
lines changed

6 files changed

+74
-55
lines changed

‎src/backend/commands/copy.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2731,7 +2731,7 @@ CopyFrom(CopyState cstate)
27312731

27322732
/* Check the constraints of the tuple */
27332733
if (cstate->rel->rd_att->constr||check_partition_constr)
2734-
ExecConstraints(resultRelInfo,slot,estate);
2734+
ExecConstraints(resultRelInfo,slot,estate, true);
27352735

27362736
if (useHeapMultiInsert)
27372737
{

‎src/backend/executor/execMain.c

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1849,16 +1849,12 @@ ExecRelCheck(ResultRelInfo *resultRelInfo,
18491849
* ExecPartitionCheck --- check that tuple meets the partition constraint.
18501850
*
18511851
* Exported in executor.h for outside use.
1852+
* Returns true if it meets the partition constraint, else returns false.
18521853
*/
1853-
void
1854+
bool
18541855
ExecPartitionCheck(ResultRelInfo*resultRelInfo,TupleTableSlot*slot,
18551856
EState*estate)
18561857
{
1857-
Relationrel=resultRelInfo->ri_RelationDesc;
1858-
TupleDesctupdesc=RelationGetDescr(rel);
1859-
Bitmapset*modifiedCols;
1860-
Bitmapset*insertedCols;
1861-
Bitmapset*updatedCols;
18621858
ExprContext*econtext;
18631859

18641860
/*
@@ -1886,60 +1882,78 @@ ExecPartitionCheck(ResultRelInfo *resultRelInfo, TupleTableSlot *slot,
18861882
* As in case of the catalogued constraints, we treat a NULL result as
18871883
* success here, not a failure.
18881884
*/
1889-
if (!ExecCheck(resultRelInfo->ri_PartitionCheckExpr,econtext))
1890-
{
1891-
char*val_desc;
1892-
Relationorig_rel=rel;
1885+
returnExecCheck(resultRelInfo->ri_PartitionCheckExpr,econtext);
1886+
}
1887+
1888+
/*
1889+
* ExecPartitionCheckEmitError - Form and emit an error message after a failed
1890+
* partition constraint check.
1891+
*/
1892+
void
1893+
ExecPartitionCheckEmitError(ResultRelInfo*resultRelInfo,
1894+
TupleTableSlot*slot,
1895+
EState*estate)
1896+
{
1897+
Relationrel=resultRelInfo->ri_RelationDesc;
1898+
Relationorig_rel=rel;
1899+
TupleDesctupdesc=RelationGetDescr(rel);
1900+
char*val_desc;
1901+
Bitmapset*modifiedCols;
1902+
Bitmapset*insertedCols;
1903+
Bitmapset*updatedCols;
18931904

1894-
/* See the comment above. */
1895-
if (resultRelInfo->ri_PartitionRoot)
1905+
/*
1906+
* Need to first convert the tuple to the root partitioned table's row
1907+
* type. For details, check similar comments in ExecConstraints().
1908+
*/
1909+
if (resultRelInfo->ri_PartitionRoot)
1910+
{
1911+
HeapTupletuple=ExecFetchSlotTuple(slot);
1912+
TupleDescold_tupdesc=RelationGetDescr(rel);
1913+
TupleConversionMap*map;
1914+
1915+
rel=resultRelInfo->ri_PartitionRoot;
1916+
tupdesc=RelationGetDescr(rel);
1917+
/* a reverse map */
1918+
map=convert_tuples_by_name(old_tupdesc,tupdesc,
1919+
gettext_noop("could not convert row type"));
1920+
if (map!=NULL)
18961921
{
1897-
HeapTupletuple=ExecFetchSlotTuple(slot);
1898-
TupleDescold_tupdesc=RelationGetDescr(rel);
1899-
TupleConversionMap*map;
1900-
1901-
rel=resultRelInfo->ri_PartitionRoot;
1902-
tupdesc=RelationGetDescr(rel);
1903-
/* a reverse map */
1904-
map=convert_tuples_by_name(old_tupdesc,tupdesc,
1905-
gettext_noop("could not convert row type"));
1906-
if (map!=NULL)
1907-
{
1908-
tuple=do_convert_tuple(tuple,map);
1909-
ExecSetSlotDescriptor(slot,tupdesc);
1910-
ExecStoreTuple(tuple,slot,InvalidBuffer, false);
1911-
}
1922+
tuple=do_convert_tuple(tuple,map);
1923+
ExecSetSlotDescriptor(slot,tupdesc);
1924+
ExecStoreTuple(tuple,slot,InvalidBuffer, false);
19121925
}
1913-
1914-
insertedCols=GetInsertedColumns(resultRelInfo,estate);
1915-
updatedCols=GetUpdatedColumns(resultRelInfo,estate);
1916-
modifiedCols=bms_union(insertedCols,updatedCols);
1917-
val_desc=ExecBuildSlotValueDescription(RelationGetRelid(rel),
1918-
slot,
1919-
tupdesc,
1920-
modifiedCols,
1921-
64);
1922-
ereport(ERROR,
1923-
(errcode(ERRCODE_CHECK_VIOLATION),
1924-
errmsg("new row for relation \"%s\" violates partition constraint",
1925-
RelationGetRelationName(orig_rel)),
1926-
val_desc ?errdetail("Failing row contains %s.",val_desc) :0));
19271926
}
1927+
1928+
insertedCols=GetInsertedColumns(resultRelInfo,estate);
1929+
updatedCols=GetUpdatedColumns(resultRelInfo,estate);
1930+
modifiedCols=bms_union(insertedCols,updatedCols);
1931+
val_desc=ExecBuildSlotValueDescription(RelationGetRelid(rel),
1932+
slot,
1933+
tupdesc,
1934+
modifiedCols,
1935+
64);
1936+
ereport(ERROR,
1937+
(errcode(ERRCODE_CHECK_VIOLATION),
1938+
errmsg("new row for relation \"%s\" violates partition constraint",
1939+
RelationGetRelationName(orig_rel)),
1940+
val_desc ?errdetail("Failing row contains %s.",val_desc) :0));
19281941
}
19291942

19301943
/*
19311944
* ExecConstraints - check constraints of the tuple in 'slot'
19321945
*
1933-
* This checks the traditional NOT NULL and check constraints,as well as
1934-
* the partition constraint, if any.
1946+
* This checks the traditional NOT NULL and check constraints,and if
1947+
*requested, checksthe partition constraint.
19351948
*
19361949
* Note: 'slot' contains the tuple to check the constraints of, which may
19371950
* have been converted from the original input tuple after tuple routing.
19381951
* 'resultRelInfo' is the original result relation, before tuple routing.
19391952
*/
19401953
void
19411954
ExecConstraints(ResultRelInfo*resultRelInfo,
1942-
TupleTableSlot*slot,EState*estate)
1955+
TupleTableSlot*slot,EState*estate,
1956+
boolcheck_partition_constraint)
19431957
{
19441958
Relationrel=resultRelInfo->ri_RelationDesc;
19451959
TupleDesctupdesc=RelationGetDescr(rel);
@@ -2055,8 +2069,9 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
20552069
}
20562070
}
20572071

2058-
if (resultRelInfo->ri_PartitionCheck)
2059-
ExecPartitionCheck(resultRelInfo,slot,estate);
2072+
if (check_partition_constraint&&resultRelInfo->ri_PartitionCheck&&
2073+
!ExecPartitionCheck(resultRelInfo,slot,estate))
2074+
ExecPartitionCheckEmitError(resultRelInfo,slot,estate);
20602075
}
20612076

20622077

‎src/backend/executor/execPartition.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ ExecFindPartition(ResultRelInfo *resultRelInfo, PartitionDispatch *pd,
167167
* First check the root table's partition constraint, if any. No point in
168168
* routing the tuple if it doesn't belong in the root table itself.
169169
*/
170-
if (resultRelInfo->ri_PartitionCheck)
171-
ExecPartitionCheck(resultRelInfo,slot,estate);
170+
if (resultRelInfo->ri_PartitionCheck&&
171+
!ExecPartitionCheck(resultRelInfo,slot,estate))
172+
ExecPartitionCheckEmitError(resultRelInfo,slot,estate);
172173

173174
/* start with the root partitioned table */
174175
parent=pd[0];

‎src/backend/executor/execReplication.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ ExecSimpleRelationInsert(EState *estate, TupleTableSlot *slot)
401401

402402
/* Check the constraints of the tuple */
403403
if (rel->rd_att->constr)
404-
ExecConstraints(resultRelInfo,slot,estate);
404+
ExecConstraints(resultRelInfo,slot,estate, true);
405405

406406
/* Store the slot into tuple that we can inspect. */
407407
tuple=ExecMaterializeSlot(slot);
@@ -466,7 +466,7 @@ ExecSimpleRelationUpdate(EState *estate, EPQState *epqstate,
466466

467467
/* Check the constraints of the tuple */
468468
if (rel->rd_att->constr)
469-
ExecConstraints(resultRelInfo,slot,estate);
469+
ExecConstraints(resultRelInfo,slot,estate, true);
470470

471471
/* Store the slot into tuple that we can write. */
472472
tuple=ExecMaterializeSlot(slot);

‎src/backend/executor/nodeModifyTable.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ ExecInsert(ModifyTableState *mtstate,
487487

488488
/* Check the constraints of the tuple */
489489
if (resultRelationDesc->rd_att->constr||check_partition_constr)
490-
ExecConstraints(resultRelInfo,slot,estate);
490+
ExecConstraints(resultRelInfo,slot,estate, true);
491491

492492
if (onconflict!=ONCONFLICT_NONE&&resultRelInfo->ri_NumIndices>0)
493493
{
@@ -1049,7 +1049,7 @@ lreplace:;
10491049
* tuple-routing is performed here, hence the slot remains unchanged.
10501050
*/
10511051
if (resultRelationDesc->rd_att->constr||resultRelInfo->ri_PartitionCheck)
1052-
ExecConstraints(resultRelInfo,slot,estate);
1052+
ExecConstraints(resultRelInfo,slot,estate, true);
10531053

10541054
/*
10551055
* replace the heap tuple

‎src/include/executor/executor.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,12 @@ extern ResultRelInfo *ExecGetTriggerResultRel(EState *estate, Oid relid);
187187
externvoidExecCleanUpTriggerState(EState*estate);
188188
externboolExecContextForcesOids(PlanState*planstate,bool*hasoids);
189189
externvoidExecConstraints(ResultRelInfo*resultRelInfo,
190-
TupleTableSlot*slot,EState*estate);
191-
externvoidExecPartitionCheck(ResultRelInfo*resultRelInfo,
190+
TupleTableSlot*slot,EState*estate,
191+
boolcheck_partition_constraint);
192+
externboolExecPartitionCheck(ResultRelInfo*resultRelInfo,
192193
TupleTableSlot*slot,EState*estate);
194+
externvoidExecPartitionCheckEmitError(ResultRelInfo*resultRelInfo,
195+
TupleTableSlot*slot,EState*estate);
193196
externvoidExecWithCheckOptions(WCOKindkind,ResultRelInfo*resultRelInfo,
194197
TupleTableSlot*slot,EState*estate);
195198
externLockTupleModeExecUpdateLockMode(EState*estate,ResultRelInfo*relinfo);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp