8000 Changed ExecConstraints() and ExecRelCheck() to cache constraint · linearregression/postgres@cff91fe · GitHub
[go: up one dir, main page]

Skip to content

Commit cff91fe

Browse files
Jan WieckJan Wieck
Jan Wieck
authored and
Jan Wieck
committed
Changed ExecConstraints() and ExecRelCheck() to cache constraint
qualification trees in the execution state to avoid memory exhaustion on INSERT, UPDATE and COPY to tables with check constraints. This also speeds up those operations substantial because the nodeToString() for the constraints ccbin is only performed once per query. Jan
1 parent ccf330d commit cff91fe

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

src/backend/commands/copy.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
*
88
* IDENTIFICATION
9-
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.63 1998/10/26 00:59:21 tgl Exp $
9+
* $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.63.2.1 1999/02/07 16:50:51 wieck Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -402,6 +402,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
402402
Node **indexPred = NULL;
403403
TupleDesc rtupdesc;
404404
ExprContext *econtext = NULL;
405+
EState *estate = makeNode(EState);
405406

406407
#ifndef OMIT_PARTIAL_INDEX
407408
TupleTable tupleTable;
@@ -709,7 +710,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
709710
{
710711
HeapTuple newtuple;
711712

712-
newtuple = ExecConstraints("CopyFrom", rel, tuple);
713+
newtuple = ExecConstraints("CopyFrom", rel, tuple, estate);
713714

714715
if (newtuple != tuple)
715716
{

src/backend/executor/execMain.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
*
2727
*
2828
* IDENTIFICATION
29-
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.58 1998/10/14 05:10:00 momjian Exp $
29+
* $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.58.2.1 1999/02/07 16:50:52 wieck Exp $
3030
*
3131
*-------------------------------------------------------------------------
3232
*/
@@ -965,7 +965,8 @@ ExecAppend(TupleTableSlot *slot,
965965
{
966966
HeapTuple newtuple;
967967

968-
newtuple = ExecConstraints("ExecAppend", resultRelationDesc, tuple);
968+
newtuple = ExecConstraints("ExecAppend", resultRelationDesc,
969+
tuple, estate);
969970

970971
if (newtuple != tuple) /* modified by DEFAULT */
971972
{
@@ -1148,7 +1149,8 @@ ExecReplace(TupleTableSlot *slot,
11481149
{
11491150
HeapTuple newtuple;
11501151

1151-
newtuple = ExecConstraints("ExecReplace", resultRelationDesc, tuple);
1152+
newtuple = ExecConstraints("ExecReplace", resultRelationDesc,
1153+
tuple, estate);
11521154

11531155
if (newtuple != tuple) /* modified by DEFAULT */
11541156
{
@@ -1279,7 +1281,7 @@ ExecAttrDefault(Relation rel, HeapTuple tuple)
12791281
#endif
12801282

12811283
static char *
1282-
ExecRelCheck(Relation rel, HeapTuple tuple)
1284+
ExecRelCheck(Relation rel, HeapTuple tuple, EState *estate)
12831285
{
12841286
int ncheck = rel->rd_att->constr->num_check;
12851287
ConstrCheck *check = rel->rd_att->constr->check;
@@ -1312,14 +1314,24 @@ ExecRelCheck(Relation rel, HeapTuple tuple)
13121314
econtext->ecxt_param_exec_vals = NULL; /* exec param values */
13131315
econtext->ecxt_range_table = rtlist; /* range table */
13141316

1317+
if (estate->es_result_relation_constraints == NULL)
1318+
{
1319+
estate->es_result_relation_constraints =
1320+
(List **)palloc(ncheck * sizeof(List *));
1321+
1322+
for (i = 0; i < ncheck; i++)
1323+
{
1324+
qual = (List *) stringToNode(check[i].ccbin);
1325+
estate->es_result_relation_constraints[i] = qual;
1326+
}
1327+
}
1328+
13151329
for (i = 0; i < ncheck; i++)
13161330
{
1317-
qual = (List *) stringToNode(check[i].ccbin);
1331+
qual = estate->es_result_relation_constraints[i];
13181332

13191333
res = ExecQual(qual, econtext);
13201334

1321-
pfree(qual);
1322-
13231335
if (!res)
13241336
return check[i].ccname;
13251337
}
@@ -1335,7 +1347,7 @@ ExecRelCheck(Relation rel, HeapTuple tuple)
13351347
}
13361348

13371349
HeapTuple
1338-
ExecConstraints(char *caller, Relation rel, HeapTuple tuple)
1350+
ExecConstraints(char *caller, Relation rel, HeapTuple tuple, EState *estate)
13391351
{
13401352
HeapTuple newtuple = tuple;
13411353

@@ -1362,7 +1374,7 @@ ExecConstraints(char *caller, Relation rel, HeapTuple tuple)
13621374
{
13631375
char *failed;
13641376

1365-
if ((failed = ExecRelCheck(rel, tuple)) != NULL)
1377+
if ((failed = ExecRelCheck(rel, tuple, estate)) != NULL)
13661378
elog(ERROR, "%s: rejected due to CHECK constraint %s", caller, failed);
13671379
}
13681380

src/include/executor/executor.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: executor.h,v 1.27 1998/10/14 05:10:05 momjian Exp $
9+
* $Id: executor.h,v 1.27.2.1 1999/02/07 16:50:54 wieck Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -85,7 +85,8 @@ extern HeapTuple ExecRemoveJunk(JunkFilter *junkfilter, TupleTableSlot *slot);
8585
extern TupleDesc ExecutorStart(QueryDesc *queryDesc, EState *estate);
8686
extern TupleTableSlot *ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, int count);
8787
extern void ExecutorEnd(QueryDesc *queryDesc, EState *estate);
88-
extern HeapTuple ExecConstraints(char *caller, Relation rel, HeapTuple tuple);
88+
extern HeapTuple ExecConstraints(char *caller, Relation rel, HeapTuple tuple,
89+
EState *estate);
8990
#ifdef QUERY_LIMIT
9091
extern int ExecutorLimit(int limit);
9192
extern int ExecutorGetLimit(void);

src/include/nodes/execnodes.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*
77
* Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: execnodes.h,v 1.18 1998/09/01 04:36:35 momjian Exp $
9+
* $Id: execnodes.h,v 1.18.2.1 1999/02/07 16:50:55 wieck Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -199,6 +199,7 @@ typedef struct EState
199199
Snapshot es_snapshot;
200200
List *es_range_table;
201201
RelationInfo *es_result_relation_info;
202+
List **es_result_relation_constraints;
202203
Relation es_into_relation_descriptor;
203204
ParamListInfo es_param_list_info;
204205
ParamExecData *es_param_exec_vals; /* this is for subselects */

0 commit comments

Comments
 (0)
0