Question

I created a scholarship database with and apply table

 applyid |  studid   |    gpa    | other |  sch_id  |    date    | sem | sy
---------+-----------+-----------+-------+----------+------------+-----+----
       1 | 2010-0000 | 1.5       |       |        1 |            |     |
       2 | 2010-0001 | 1.5       |       |        7 | 2014-03-13 |     |
       3 | 2010-0003 |           |       |        1 | 2014-03-13 |     |
       4 | 2010-0003 |           |       |        1 | 2014-03-13 |     |
       5 | 2010-0003 |           |       |        1 | 2014-03-13 |     |
    2308 | 2012-0004 | 1.5       |       |        1 | 2014-03-19 |     |
    4593 | 2012-0004 | 1.5       |       |        1 | 2014-03-19 |     |
    4596 | 2012-0004 | 1.5       |       |        1 | 2014-03-19 |     |
    4597 | 2012-0004 | 1.5       |       |        1 | 2014-03-19 |     |
(9 rows)

and currently working on this trigger function that checks if a particular student has either a grade of INC, DRP, 5.00.

CREATE FUNCTION fail_check() RETURNS TRIGGER AS $$
DECLARE
one RECORD;
two RECORD;

BEGIN

   SELECT * INTO one FROM grade, registration;

   IF (SELECT COUNT(g.grade)::int  
       FROM grade g
         INNER JOIN registration r ON r.grade_id = g.grade_id
       WHERE g.grade IN ('INC', 'DRP', '5.00')
     AND studid=new.studid) <= 1 
   THEN 
    SELECT studid, gpa, sch_name INTO two
        FROM apply WHERE studid=new.studid;

    INSERT INTO apply(studid, gpa, sch_name)
    VALUES (new.studid, new.gpa, new.sch_name);

   END IF;

  RETURN NEW;

END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER fail
BEFORE INSERT ON apply
FOR EACH ROW
EXECUTE PROCEDURE fail_check();

but when I entered this:

INSERT INTO apply(studid, gpa, sch_name) 
VALUES ('2012-0004', '1.5', 1);

The student with the student id of "2012-0004" has a grade of INC DRP and 5.00. the SELECT query works perfectly fine and returns the value 3. Since 3 is greater than 1, which is contrary to the IF statement that says IF .... <= 1, I'm expecting an error that says something like that it can't be inserted because the "student" has more than 1 grade of either INC, DRP, 5.00.

But instead I got this error:

ERROR:  stack depth limit exceeded 
HINT:  Increase the configuration parameter "max_stack_depth" (currently 2048kB), after ensuring the platform's stack depth limit is adequate. 
CONTEXT:  SQL statement "SELECT (SELECT COUNT(g.grade) 
       FROM grade g
         INNER JOIN registration r ON r.grade_id = g.grade_id
       WHERE g.grade IN ('INC', 'DRP', '5.00')   AND studid=new.studid) <= 1" PL/pgSQL function fail_check() line 12 at IF SQL statement "INSERT INTO apply(studid, gpa, sch_name)  VALUES (new.studid, new.gpa, new.sch_name)" PL/pgSQL function fail_check() line 21 at SQL statement SQL statement "INSERT INTO apply(studid, gpa, sch_name)     VALUES (new.studid, new.gpa, new.sch_name)"

Where did I go wrong?? and what does this max_stack_depth exactly mean?? Which part of my code caused this max_stack_depth error??

Currently using PostgreSQL 9.3.2

Was it helpful?

Solution

Your trigger BEFORE INSERT triggers more INSERTs to the same table, which causes an endless loop. Hence the stack overflow. You seem to be under the impression that you need this in your trigger:

INSERT INTO apply(studid, gpa, sch_name)   
VALUES (new.studid, new.gpa, new.sch_name);

But you don't. RETURN NEW; is enough to let the original INSERT go through.

The rest of your trigger doesn't seem to do anything useful, but that's probably just a simplification.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top