In a very old but still relevant article titled, "A Theory of Moby Dick," author William S. Gleim argues that for Ahab, "the only escape from the torture of consciousness, as he felt it, was through either faith or insanity. He was the victim of his own cogitations concerning the unknowable; yet he could not resist the urge of inquiry as he confesses: "I love to sail forbidden seas and land on barbarous coasts." This is a typical line with a double meaning, for literally it describes his personal experience; but figuratively, it reveals his passion for speculating on the mysteries of the infinite."
"A Theory of Moby Dick"William S. Gleim The New England Quarterly, Vol. 2, No. 3. (Jul., 1929), pp. 402-419.
The response made by jamie-wheeler is good but makes a critical error. William S. Gleim is not talking about Ahab in this quotation but about Herman Melville's thoughts on philosophy and insanity. Although it could apply it is incorrect to assume that it does necessarily.
That said, I believe that the relation of insanity to the story is partly rooted in Melville's justification for the journey, shown in Ahab through his monomaniacal vengeance. Also insanity is present to depict the follies and fall of man. Pip exhibits this quite clearly in that he cannot reconcile his fear of death when floating in the ocean with his reality, which causes him to lose his ego and go mad.
Obviously this is a bit limited as I am writing a long paper on a very similar topic and there is a lot of worth in Melville's descriptions of insanity