title: Loop violating Primary Key Value
body: <p>I have a part of my function mentioned below.
  When I execute the function I get an error saying primary key violation for alarm_id</p>
  
  <pre><code>SELECT COUNT(DISTINCT exception_sub_type) INTO v_count FROM mdas.alarm_configuration;
  
  IF v_count &gt; 0 THEN
  
  FOR i IN 1..v_count
  LOOP
  
  SELECT ((MAX(alarm_id))+1) INTO v_alarm_id FROM mdas.alarm_configuration;
  
  RAISE NOTICE 'ALARM ID (%)',v_alarm_id;
  
  INSERT INTO mdas.alarm_configuration
      (alarm_id,
       exception_sub_type,
       exception_type,
       from_range,
       groups,
       priority,
       sub_group,
       to_range,
       org_unit_id,
       status)
  
  SELECT DISTINCT ON(exception_sub_type) v_alarm_id,
  exception_sub_type,
  exception_type,
  from_range,
  v_group_name,
  priority,
  v_subgroup_name,
  to_range,
  v_org_unit_id,
  status
  FROM mdas.alarm_configuration 
  WHERE groups = v_group_name AND mdas.alarm_configuration.org_unit_id = v_org_unit_id;
  
  END LOOP;   
  
  END IF;
  </code></pre>
  
  <p>Note: This is only a part of the function. The function has three arguments v_org_unit_id, v_group_name and v_subgroup_name. My primary key is on alarm_id, which I want to increment by 1 and insert based on the MAX which is available in the table.</p>