The Shulchan Aruch · Orach Chayim
אורח חיים תקפ״בOrach Chayim 582
Siman 582 of 697 in Orach Chayim — 9 se'ifim
הלכות ראש השנהLaws of Rosh Hashanah· siman 582
Se’if 1א׳
בַּעֲשֶׂרֶת יְמֵי תְּשׁוּבָה אוֹמֵר: הַמֶּלֶךְ הַקָּדוֹשׁ, הַמֶּלֶךְ הַמִּשְׁפָּט; וְאִם טָעָה אוֹ שֶׁהוּא מְסֻפָּק אִם הוּא בְּהַמֶּלֶךְ הַקָּדוֹשׁ, חוֹזֵר לְרֹאשׁ; וְאִם הוּא בְּהַמֶּלֶךְ הַמִּשְׁפָּט, אִם נִזְכַּר קֹדֶם שֶׁעָקַר רַגְלָיו, חוֹזֵר לְבִרְכַּת הָשִׁיבָה וְאוֹמֵר מִשָּׁם וָאֵילָךְ עַל הַסֵדֶר; וְאִם לֹא נִזְכַּר עַד שֶׁעָקַר רַגְלָיו עַיֵּן לְעֵיל סוֹף סִימָן קי"ז, חוֹזֵר לְרֹאשׁ עַיֵּן לְעֵיל סִימָן קי"ח.
The Order of Prayers for the Ten Days of Repentance and Rosh HaShana. And in it is 9 paragraphs. In the Ten Days of Repentance say The Holy King and The King of Judgment. If one made a mistake or even if one is uncertain, (the rule is as follows:) If it is with "HaMelech HaKadosh" he should go back to the beginning. If it is HaMelech HaMishpat if prior to uprooting his feet, he should go back to the Birchas Hasheveinu and say from there and on according to the order. If he did not remember until he uprooted his feet (see before the end of Siman 117) he should got back to the beginning (i.e. repeat). [Also see Siman 118.]
Se’if 2ב׳
אִם אָמַר: הָאֵל הַקָּדוֹשׁ, וְתוֹךְ כְּדֵי דִּבּוּר נִזְכַּר וְאָמַר: הַמֶּלֶךְ הַקָּדוֹשׁ, אֵינוֹ צָרִיךְ לַחֲזֹר; וְכֵן הַדִּין בְּהַמֶּלֶךְ הַמִּשְׁפָּט.
If one said The Holy God and within a moment he remembered and says The Holy King, he does not need to go return (the beginning). The same rule applies with The Holy Judge.
Se’if 3ג׳
בְּשַׁבָּת בֵּינְתַיִם, עַרְבִית יֹאמַר בְּבִרְכַּת מֵעֵין שֶׁבַע: הַמֶּלֶךְ הַקָּדוֹשׁ. הַגָּה: וְה"ה אִם חָל רֹאשׁ הַשָּׁנָה בְּשַׁבָּת; וְחוֹתֵם בְּשֶׁל שַׁבָּת לְבַד, וְכֵן כְּשֶׁחָל יוֹם כִּפּוּר בְּשַׁבָּת (מַהֲרִי"ל וּמִנְהָגִים).
In the Shabbos between (R'H and Yom Kippur), at Maariv say in the Birchas Mein Sheva The Holy King. Rema: So to if Rosh HaShana was on Shabbos but the conclusion is Shabbos alone. And the same is if Yom Kippur was on Shabbos [Maharil and Minhagim]
Shulchan Aruch, Orach Chayim — Torat Emet 363 edition, via Sefaria — public domain
English: Sefaria Community Translation — CC0