{"id":1420,"date":"2025-01-04T09:15:06","date_gmt":"2025-01-04T02:15:06","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=1420"},"modified":"2025-03-09T16:45:59","modified_gmt":"2025-03-09T09:45:59","slug":"plpgsql","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/plpgsql\/","title":{"rendered":"PL\/pgSQL"},"content":{"rendered":"\n<p>PL\/pgSQL is the default <em>procedural language<\/em> of PostgreSQL. PL\/pgSQL allows you to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create reusable database <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-function\/\">functions<\/a> and <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-procedure\/\">stored procedures<\/a> with complex business logic, including if-else and loops.<\/li>\n\n\n\n<li>Automate repetitive PostgreSQL database tasks.<\/li>\n<\/ul>\n\n\n\n<p class=\"note\">PL\/pgSQL in PostgreSQL shares a similar syntax with <a href=\"https:\/\/www.oracletutorial.com\/plsql-tutorial\/\" target=\"_blank\" rel=\"noreferrer noopener\">PL\/SQL in Oracle Database<\/a>, making it easier for Oracle Database developers to transit to PostgreSQL.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='plpgsql-key-features'>PL\/pgSQL key features <a href=\"#plpgsql-key-features\" class=\"anchor\" id=\"plpgsql-key-features\" title=\"Anchor for PL\/pgSQL key features\">#<\/a><\/h2>\n\n\n\n<p>PL\/pgSQL has the following key features:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>SQL integration:<\/strong> PL\/pgSQL can execute SQL statements seamlessly within its code.<\/li>\n\n\n\n<li><strong>Block structure<\/strong>: PL\/pgSQL offers a block structure that organizes the code into blocks, making it easier to develop.<\/li>\n\n\n\n<li><strong>Control structures<\/strong>: PL\/pgSQL support conditionals and loops.<\/li>\n\n\n\n<li><strong>Error handling<\/strong>: PL\/pgSQL provides a robust error-handling mechanism.<\/li>\n\n\n\n<li><strong>Dynamic SQL<\/strong>: PL\/pgSQL can construct SQL queries flexibly and execute them dynamically.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='advantages-of-plpgsql'>Advantages of PL\/pgSQL <a href=\"#advantages-of-plpgsql\" class=\"anchor\" id=\"advantages-of-plpgsql\" title=\"Anchor for Advantages of PL\/pgSQL\">#<\/a><\/h2>\n\n\n\n<p>PL\/pgSQL makes PostgreSQL more powerful by:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Boosting PostgreSQL performance and reducing latency by avoiding multiple client-server round trips.<\/li>\n\n\n\n<li>Centralizing the business logic at the database layer for consistency.<\/li>\n\n\n\n<li>Bringing an imperative programming model to the database programming.<\/li>\n<\/ul>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='getting-started-with-plpgsql'>Section 1. Getting Started with PL\/pgSQL <a href=\"#getting-started-with-plpgsql\" class=\"anchor\" id=\"getting-started-with-plpgsql\" title=\"Anchor for Section 1. Getting Started with PL\/pgSQL\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll start with the PL\/pgSQL procedure language:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-blocks\/\">PL\/pgSQL blocks<\/a> &#8211; Learn how to organize the PL\/pgSQL code into blocks.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-variables\/\">Variables<\/a> &#8211; Show you how to declare variables in PL\/pgSQL and help you understand variable scopes.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-select-into\/\">Select Into<\/a> &#8211; Select data from a table and assign it to variables.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='user-defined-functions-procedures'>Section 2. User-defined functions &amp; procedures <a href=\"#user-defined-functions-procedures\" class=\"anchor\" id=\"user-defined-functions-procedures\" title=\"Anchor for Section 2. User-defined functions &amp; procedures\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to define functions and stored procedures using PL\/pgSQL.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-functions\/\">PL\/pgSQL functions<\/a> &#8211; Learn how to create a user-defined function using PL\/pgSQL.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-procedure\/\">PL\/pgSQL procedures<\/a> &#8211; Show how to define procedures using PL\/pgSQL.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='control-structures'>Section 3. Control Structures <a href=\"#control-structures\" class=\"anchor\" id=\"control-structures\" title=\"Anchor for Section 3. Control Structures\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn the basic control structure, including if, case, and loop statements.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-if\/\">IF statement<\/a> &#8211; Add conditional logic to the PL\/pgSQL block using the if-else statements.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-case\/\">CASE statement<\/a> &#8211; Learn how to use the CASE statement to execute code based on one or more conditions.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-loop\/\">LOOP statement<\/a> &#8211; Perform a code block repeatedly using the loop statement.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-while\/\">WHILE LOOP statement<\/a> &#8211; Create a pretest loop using the while loop statement.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-for-loop\/\">FOR LOOP statement<\/a> &#8211; Use for loop to iterate over rows of a result set and process each individually.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-exit\/\">EXIT<\/a> &#8211; Learn how to terminate a loop prematurely.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-continue\/\">CONTINUE<\/a> &#8211; Learn how to use the continue statement to skip the current iteration and immediately start the next one.<\/li>\n<\/ul>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-group\"><div class=\"wp-block-group__inner-container is-layout-constrained wp-block-group-is-layout-constrained\">\n<h2 class=\"wp-block-heading\" id='exceptions'>Section 4. Exceptions <a href=\"#exceptions\" class=\"anchor\" id=\"exceptions\" title=\"Anchor for Section 4. Exceptions\">#<\/a><\/h2>\n\n\n\n<p>In this section, you&#8217;ll learn how to handle exceptions, raise errors, and use assertions.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-exception\/\">Handling exceptions<\/a> &#8211; Catch and handle exceptions using the EXCEPTION block.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-raise\/\">Raising exceptions<\/a> &#8211; Learn how to raise exceptions using the RAISE statement.<\/li>\n\n\n\n<li><a href=\"https:\/\/www.pgtutorial.com\/plpgsql\/plpgsql-assert\/\">Assert<\/a> &#8211; Add debugging checks to the program using the assert statement.<\/li>\n<\/ul>\n<\/div><\/div>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1420\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/plpgsql\/\"\n\t\t\t\tdata-post-title=\"PL\/pgSQL\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1420\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/plpgsql\/\"\n\t\t\t\tdata-post-title=\"PL\/pgSQL\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>You&#8217;ll learn how to use the PL\/pgSQL procedure language to create reusable functions and stored procedures in PostgreSQL <\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1420","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PL\/pgSQL Tutorial<\/title>\n<meta name=\"description\" content=\"You&#039;ll learn how to use the PL\/pgSQL procedure language to create reusable functions and stored procedures in PostgreSQL\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/plpgsql\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PL\/pgSQL Tutorial\" \/>\n<meta property=\"og:description\" content=\"You&#039;ll learn how to use the PL\/pgSQL procedure language to create reusable functions and stored procedures in PostgreSQL\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/plpgsql\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-03-09T09:45:59+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/\",\"name\":\"PL\\\/pgSQL Tutorial\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2025-01-04T02:15:06+00:00\",\"dateModified\":\"2025-03-09T09:45:59+00:00\",\"description\":\"You'll learn how to use the PL\\\/pgSQL procedure language to create reusable functions and stored procedures in PostgreSQL\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/plpgsql\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PL\\\/pgSQL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PL\/pgSQL Tutorial","description":"You'll learn how to use the PL\/pgSQL procedure language to create reusable functions and stored procedures in PostgreSQL","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pgtutorial.com\/plpgsql\/","og_locale":"en_US","og_type":"article","og_title":"PL\/pgSQL Tutorial","og_description":"You'll learn how to use the PL\/pgSQL procedure language to create reusable functions and stored procedures in PostgreSQL","og_url":"https:\/\/www.pgtutorial.com\/plpgsql\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-03-09T09:45:59+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/plpgsql\/","url":"https:\/\/www.pgtutorial.com\/plpgsql\/","name":"PL\/pgSQL Tutorial","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2025-01-04T02:15:06+00:00","dateModified":"2025-03-09T09:45:59+00:00","description":"You'll learn how to use the PL\/pgSQL procedure language to create reusable functions and stored procedures in PostgreSQL","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/plpgsql\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/plpgsql\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/plpgsql\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PL\/pgSQL"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1420","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=1420"}],"version-history":[{"count":20,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1420\/revisions"}],"predecessor-version":[{"id":2355,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/1420\/revisions\/2355"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=1420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}