Changed ExecConstraints() and ExecRelCheck() to cache the constraints
authorJan Wieck
Sun, 7 Feb 1999 16:17:14 +0000 (16:17 +0000)
committerJan Wieck
Sun, 7 Feb 1999 16:17:14 +0000 (16:17 +0000)
qualification expression trees in the execution state. Prevents from
memory exhaustion on INSERT, UPDATE or COPY to tables that have CHECK
constraints. Speedup against the variant using freeObject() is more than
factor 2.

Jan

src/backend/commands/copy.c
src/backend/executor/execMain.c
src/include/executor/executor.h
src/include/nodes/execnodes.h

index ff8555e39dd6b2644211a459fa03f7e47063350b..9df08908c2313dab47f2039a9cd2f140100e6305 100644 (file)
@@ -6,7 +6,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.71 1999/02/03 21:16:03 momjian Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/commands/copy.c,v 1.72 1999/02/07 16:17:10 wieck Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -505,6 +505,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
    Node      **indexPred = NULL;
    TupleDesc   rtupdesc;
    ExprContext *econtext = NULL;
+   EState      *estate = makeNode(EState); /* for ExecConstraints() */
 
 #ifndef OMIT_PARTIAL_INDEX
    TupleTable  tupleTable;
@@ -805,7 +806,7 @@ CopyFrom(Relation rel, bool binary, bool oids, FILE *fp, char *delim)
             */
 
            if (rel->rd_att->constr)
-               ExecConstraints("CopyFrom", rel, tuple);
+               ExecConstraints("CopyFrom", rel, tuple, estate);
 
            heap_insert(rel, tuple);
 
index d511e5f6e56907ef362fea884d356b1f5f49a3b0..5ce90902b4cabea63239cde7700c8e692e78f0c9 100644 (file)
@@ -26,7 +26,7 @@
  *
  *
  * IDENTIFICATION
- *   $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.74 1999/02/07 14:20:11 wieck Exp $
+ *   $Header: /cvsroot/pgsql/src/backend/executor/execMain.c,v 1.75 1999/02/07 16:17:11 wieck Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -1124,7 +1124,7 @@ ExecAppend(TupleTableSlot *slot,
 
    if (resultRelationDesc->rd_att->constr)
    {
-       ExecConstraints("ExecAppend", resultRelationDesc, tuple);
+       ExecConstraints("ExecAppend", resultRelationDesc, tuple, estate);
    }
 
    /******************
@@ -1327,7 +1327,7 @@ ExecReplace(TupleTableSlot *slot,
 
    if (resultRelationDesc->rd_att->constr)
    {
-       ExecConstraints("ExecReplace", resultRelationDesc, tuple);
+       ExecConstraints("ExecReplace", resultRelationDesc, tuple, estate);
    }
 
    /*
@@ -1472,7 +1472,7 @@ ExecAttrDefault(Relation rel, HeapTuple tuple)
 #endif
 
 static char *
-ExecRelCheck(Relation rel, HeapTuple tuple)
+ExecRelCheck(Relation rel, HeapTuple tuple, EState *estate)
 {
    int         ncheck = rel->rd_att->constr->num_check;
    ConstrCheck *check = rel->rd_att->constr->check;
@@ -1505,14 +1505,24 @@ ExecRelCheck(Relation rel, HeapTuple tuple)
    econtext->ecxt_param_exec_vals = NULL;      /* exec param values */
    econtext->ecxt_range_table = rtlist;        /* range table */
 
+   if (estate->es_result_relation_constraints == NULL)
+   {
+       estate->es_result_relation_constraints =
+               (List **)palloc(ncheck * sizeof(List *));
+
+       for (i = 0; i < ncheck; i++)
+       {
+           qual = (List *) stringToNode(check[i].ccbin);
+           estate->es_result_relation_constraints[i] = qual;
+       }
+   }
+
    for (i = 0; i < ncheck; i++)
    {
-       qual = (List *) stringToNode(check[i].ccbin);
+       qual = estate->es_result_relation_constraints[i];
 
        res = ExecQual(qual, econtext);
 
-       freeObject(qual);
-
        if (!res)
            return check[i].ccname;
    }
@@ -1528,7 +1538,7 @@ ExecRelCheck(Relation rel, HeapTuple tuple)
 }
 
 void
-ExecConstraints(char *caller, Relation rel, HeapTuple tuple)
+ExecConstraints(char *caller, Relation rel, HeapTuple tuple, EState *estate)
 {
 
    Assert(rel->rd_att->constr);
@@ -1549,7 +1559,7 @@ ExecConstraints(char *caller, Relation rel, HeapTuple tuple)
    {
        char       *failed;
 
-       if ((failed = ExecRelCheck(rel, tuple)) != NULL)
+       if ((failed = ExecRelCheck(rel, tuple, estate)) != NULL)
            elog(ERROR, "%s: rejected due to CHECK constraint %s", caller, failed);
    }
 
index 33774d21f41db63a6d7e7a8f4c36e4c5d8f184f9..68d89897d092ece35616ead1290bb44da64b8c61 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: executor.h,v 1.28 1998/11/27 19:33:32 vadim Exp $
+ * $Id: executor.h,v 1.29 1999/02/07 16:17:12 wieck Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -85,7 +85,8 @@ extern HeapTuple ExecRemoveJunk(JunkFilter *junkfilter, TupleTableSlot *slot);
 extern TupleDesc ExecutorStart(QueryDesc *queryDesc, EState *estate);
 extern TupleTableSlot *ExecutorRun(QueryDesc *queryDesc, EState *estate, int feature, int count);
 extern void ExecutorEnd(QueryDesc *queryDesc, EState *estate);
-extern void ExecConstraints(char *caller, Relation rel, HeapTuple tuple);
+extern void ExecConstraints(char *caller, Relation rel, HeapTuple tuple, 
+                                           EState *estate);
 #ifdef QUERY_LIMIT
 extern int ExecutorLimit(int limit);
 extern int ExecutorGetLimit(void);
index d9cdd2509defa4f8623aee8be11344f39e0363aa..45625c62e7b8957865361ddc309a22223d33c7ee 100644 (file)
@@ -6,7 +6,7 @@
  *
  * Copyright (c) 1994, Regents of the University of California
  *
- * $Id: execnodes.h,v 1.22 1999/01/29 09:23:13 vadim Exp $
+ * $Id: execnodes.h,v 1.23 1999/02/07 16:17:14 wieck Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -199,6 +199,7 @@ typedef struct EState
    Snapshot        es_snapshot;
    List           *es_range_table;
    RelationInfo   *es_result_relation_info;
+   List          **es_result_relation_constraints;
    Relation        es_into_relation_descriptor;
    ParamListInfo   es_param_list_info;
    ParamExecData  *es_param_exec_vals; /* this is for subselects */