               case CONSTR_PRIMARY:
						{
                            char  *iname = constr->name;
                            bool  istemp = is_temp_rel_name(relationName);
                            Relation rel;
                            List	 *indexoidlist;
                            List     *indexoidscan;
                            Form_pg_attribute *rel_attrs;
                            int num_keys = 0;
                            int keys_matched = 0;
                            bool index_found = false;
                            bool index_found_primary = false;

                            /* If the constraint name is not specified, generate a name */
                            if (iname == NULL) {
                               Oid	indoid;
                               int   pass = 0;
                               char  *typename = palloc(NAMEDATALEN);
                               Ident *key;

                               /* Assume that the length of the attr list is already > 0 */

                               /* Get the first attribute so we can use its name */
                               key = (Ident *)lfirst(constr->keys);

                               /* Initialise typename to 'pkey' */
                               snprintf(typename, NAMEDATALEN, "pkey");

                               for (;;)
                               {
                                  iname = makeObjectName(relationName, key->name, typename);

                                  /* Check for a conflict */
                                  indoid = RelnameFindRelid(iname);

                                  /* If the oid was not found, then we have a safe name */
                                  if ((!istemp && !OidIsValid(indoid)) ||
                                     (istemp && !is_temp_rel_name(iname)))
                                     break;

                                  /* Found a conflict, so try a new name component */
                                  pfree(iname);
                                  snprintf(typename, NAMEDATALEN, "pkey%d", ++pass);
                               }
                            }

                            /* Need to check for primary key already on field(s) */
                            rel = heap_openr(relationName, AccessExclusiveLock);

                            /*
                             * First we check for limited correctness of the
                             * constraint
                             */

                            rel_attrs = rel->rd_att->attrs;

                            /* Retrieve the oids of all indices on the relation */
                            indexoidlist = RelationGetIndexList(rel);
                            index_found = false;
                            index_found_primary = false;

                            /* Loop over all indices on the relation */
                            foreach(indexoidscan, indexoidlist)
                            {
                               Oid			indexoid = lfirsti(indexoidscan);
                               HeapTuple	indexTuple;
                               Form_pg_index indexStruct;
                               List	   *keyl;
                               int         i;

                               indexTuple = SearchSysCache(INDEXRELID,
                                                 ObjectIdGetDatum(indexoid),
                                                    0, 0, 0);

                               if (!HeapTupleIsValid(indexTuple))
                                  elog(ERROR, "ALTER TABLE/ADD CONSTRAINT: Index \"%u\" not found",
                                     indexoid);
                               indexStruct = (Form_pg_index) GETSTRUCT(indexTuple);

                               index_found_primary = indexStruct->indisprimary;
							   /* @@ This check is always false @@ */
						       if (index_found_primary) {
		                             ReleaseSysCache(indexTuple);
						             break;
						       }
			
                               /*
						       * If it's not a primary index, then check to see if it overlaps
						       * the new constraint.
                               * Make sure this index has the same number of
                               * keys as the constraint -- It obviously won't match otherwise.
                               */
                               for (i = 0; i < INDEX_MAX_KEYS && indexStruct->indkey[i] != 0; i++);
                               num_keys = length(constr->keys);
                               keys_matched = 0;

                               if (i == num_keys)
                               {
                                  /* Loop over each key in the constraint and check that there is a
                                     corresponding key in the index. */
                                  i = 0;
                                  foreach(keyl, constr->keys)
                                  {
                                     Ident    *key = lfirst(keyl);

                                     /* Look at key[i] in the index and check that it is over the same column
                                        as key[i] in the constraint.  This is to differentiate between (a,b)
                                        and (b,a) */
                                     if (i < INDEX_MAX_KEYS && indexStruct->indkey[i] != 0)
                                     {
                                        int	   keyno = indexStruct->indkey[i];

                                        if (keyno > 0)
                                        {
                                           char  *name = NameStr(rel_attrs[keyno - 1]->attname);
                                           if (strcmp(name, key->name) == 0) keys_matched++;
                                        }
                                     }
                                     else elog(ERROR, "ALTER TABLE/ADD CONSTRAINT: Key \"%u[%u]\" not found", indexoid, i);
                                     i++;
                                  }
                                  if (keys_matched == num_keys) {
                                     index_found = true;
	                             ReleaseSysCache(indexTuple);
                                     break;
                                  }
                               }
                               ReleaseSysCache(indexTuple);
                            }

                            freeList(indexoidlist);

                            if (index_found_primary)
                               elog(ERROR, "Primary key already defined on relation \"%s\"", relationName);

                            /* If everything is ok, create the new index (constraint) */
                            DefineIndex(
                               relationName,
                               iname,
                               "btree",
                               constr->keys,
                               true,
                               true,
                               NULL,
                               NIL);

                            /* Issue notice */
                            elog(NOTICE, "ALTER TABLE/ADD PRIMARY KEY will create implicit index '%s' for table '%s'",
                               iname, relationName);
                            if (index_found)
                               elog(NOTICE, "Primary Key constraint supercedes existing index on relation \"%s\".  Drop the existing index to remove redundancy.", relationName);
                            pfree(iname);

                            /* Finally, close relation */
                            heap_close(rel, NoLock);

           		    break;
			}

