Your question addresses the dramatic (or theatrical) purpose of the scene rather than the thematic purpose. Many debate/discuss the possibilities of the scene involving the Porter who holds the keys to the castle door as a metaphor for the gates of Hell, and the Porter does mention "hell-gate" and "Beelzebub" (another name for the devil). But these observations don't answer the question of how the scene operates in a theatrical or dramatic sense, merely a thematic one.
The scene is certainly meant to be funny, and the actor playing the Porter would have been one of the "clowns" in Shakespeare's company of actors, but the scene is not simply one of comic relief. It operates on a much more cohesive level dramatically than this.
In Act II, scene ii, knocking is introduced. The audience can't be sure what this knocking is, and, at first, neither can Macbeth and Lady Macbeth. The tension and suspense of this scene are running very high, since the scene, rather than being a presentation of Duncan's murder, is a scene all about the possibility that Macbeth and Lady Macbeth might get caught.
They both begin the scene hearing things: an owl, the bellman, the crickets. They also might be hearing voices:
Did not you speak?
As I descended?
This creating of suspense through real and imagined offstage sounds is a tactic still used today and one you can find in many horror and suspense movies.
So, this scene is all about the tension of whether they will be caught or not. When Lady Macbeth leaves to return the daggers to the murder scene, Macbeth hears knocking. And at this moment, he isn't sure what it is, but Lady Macbeth enters and says, "I hear a knocking / At the south entry," identifying it as someone beating to enter the castle.
Again, it sends the message of urgency to the audience. Is it someone who knows that Duncan has been murdered? It certainly seems that way, because of the focus that Shakespeare has placed in the scene upon the possibility of Lady Macbeth and Macbeth being discovered.
The knocking builds and continues into scene iii, and, though the Porter is funny, dramatically, the tension of the scene is not relieved because the audience still really wants to know who is out there and whether they might expose Macbeth as the murderer.
Interestingly, once the door is finally opened to admit Macduff (his first entrance into the play), he is already, because of the association made between the knocking and the murder, planted by Shakespeare in the audience's mind as someone who knows what Macbeth has done. And he, in fact, will be the one thane who refuses to attend Macbeth's banquet in Act III.
So, though the scene is a funny one, it actually serves dramatically to heighten the audience's anticipation concerning the question of whether Macbeth and Lady Macbeth will get caught. And this is all because the knocking is established as part of the suspense of Act II, scene ii.
Many questions have been posted in eNotes about the drunken porter scene in Macbeth(Act 2, Scene 3). Many of the answers deal with the concept of comic relief. It seems likely that Shakespeare inserted comic scenes in some of his tragedies because his audiences liked to laugh. The plays were entertainment, diversion. One might compare the comic interludes to the shows that used to be offered in movie theters. There would be a serious drama plus a cartoon featuring characters like Bugs Bunny or Tom and Jerry, and there might be a short subject with comedians like the Three Stooges. Since Shakespeare was only presenting a single feature (so to speak). he might have been inclined to insert comedy as a sort of bonus to please the members of his diverse audience who wanted that sort of thing.
No doubt Shakespeare had at least one member of his company who was especially good at making people laugh. He couldn't just assign any actor to a role like that of the drunken Porter. Good comedians are rare. If he had a talented comic he would be thinkinig of using him often, and it well might be that the same actor who played the drunken Porter in Macbeth also played the gravedigger in Hamlet. Critics who have questioned the purpose of the drunken Porter scene have also questioned the purpose of the gravedigger scene. Both may have been devised purely to utilize the talents of a particular member of Shakespeare's company.
Furthermore, the same comedian might have played the part of the Fool in King Lear, of Touchstone in As You Like It, and many other parts, including the clownish peasant who brings the two poison adders to Cleopatra in Antony and Cleopatra. In Julius Caesar there is a part for a comic who calls himself a cobbler and is leading a group of Commoners in the very first scene. One might visualize this actor as someone who looked and acted like Bert Lahr in The Wizard of Oz--a natural comedian who could make audiences laugh just with facial expressions and tones of voice.
Shakepeare may not have been so much concerned about "comic relief" as he was about simply providing a little comedy and making use of the special talents of one of the members of his company.
I think you are right in indicating that this important scene plays an important function in terms of giving comic relief after a very fraught scene with Macbeth actually doing the deed and killing Duncan. However, consider its other purpose: this scene really helps to maintain tension by the knocking and by our feeling that the discovery of the murder is merely being delayed by the rambling talk. You might want to consider too how the horror of the murder is somehow intensified by the coarse vulgarity of the Porter; it seems, in his comic bad taste, to be a gruesome attempt to cover up the truth. However, note too, how when we read his comments alongside what Lennox says about the night, his words give a contemporary and a universal significance to Macbeth's crime.
It is important therefore not to simply underestimate the Porter as mere comic relief when Shakespeare seems to use him for so many other reasons as well.
And, from a purely technical point of view, it gives the actors time to change costumes and clean up from the bloody murder. Shakespeare was nothing if not practical.
The porter provides comic relief to an otherwise very serious play. He also introduces to the audience the idea of equivocation, which is mentioned quite frequently after the porter scene.