From f896904feb254ca0866b58ed7261d5bf4498815a Mon Sep 17 00:00:00 2001 From: Ishaan Adarsh Date: Sat, 16 Dec 2023 15:45:20 +0530 Subject: [PATCH] plpyhton-plpgsql-docu-changes --- doc/src/sgml/plpgsql.sgml | 191 + doc/src/sgml/plpython.sgml | 181 + 2 files changed, 372 insertions(+) diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 5977534a62..8522415c44 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -6105,4 +6105,195 @@ $$ LANGUAGE plpgsql STRICT IMMUTABLE; + + Quick Start: Creating a PostgreSQL Extension using PL/pgSQL + + + In this quick start guide, we will create a simple PostgreSQL extension using PL/pgSQL. + + + + Prerequisites + + + + + PostgreSQL installed and running on your system. + + + + + Basic knowledge of SQL and PL/pgSQL programming. + + + + + make utility for building and compiling software. + + + + + Knowledge about the Postgres Development Libraries. + + + + + + + Create the Extension Directory + + + Create a new directory for your extension project. + + + + $ mkdir ~/pg_plpgsql_ext + $ cd ~/pg_plpgsql_ext + + + + + Create the SQL Script File + + + Create a file named pg_plpgsql_ext--1.0.0.sql in the pg_plpgsql_ext directory with the following content: + + + + -- pg_plpgsql_ext--1.0.0.sql + -- Create the function that uses PL/pgSQL + CREATE OR REPLACE FUNCTION subtract_numbers(a integer, b integer) + RETURNS integer + LANGUAGE plpgsql + AS $$ + BEGIN + RETURN a - b; + END; + $$; + + + + Explanation of the SQL Script: + + + + CREATE OR REPLACE FUNCTION subtract_numbers(a integer, b integer) RETURNS integer LANGUAGE plpgsql AS $$: + This statement creates the function subtract_numbers with two integer arguments (a and b). The function will return an integer. The LANGUAGE plpgsql specifies that the function is written in PL/pgSQL. + + + + + BEGIN: + This is the beginning of the PL/pgSQL code block. + + + + + RETURN a - b;: + This line calculates the subtraction of b from a and returns the result. + + + + + + + + Create the Control File + + + Create a file named pg_plpgsql_ext.control in the pg_plpgsql_ext directory with the following content: + + + + # pg_plpgsql_ext.control + + # Extension name + comment = 'A simple PostgreSQL extension using PL/pgSQL.' + + # Default version of the extension + default_version = '1.0.0' + + # Extension is relocatable + relocatable = true + + + + + Create the Makefile + + + Create a Makefile in the pg_plpgsql_ext directory with the following content: + + + + # Makefile for pg_plpgsql_ext + EXTENSION = pg_plpgsql_ext + DATA = pg_plpgsql_ext--1.0.0.sql + + PG_CONFIG = pg_config + PGXS := $(shell $(PG_CONFIG) --pgxs) + include $(PGXS) + + + + + Build and Install the Extension + + + Let's build and install the pg_plpgsql_ext extension: + + + + # Build the extension + $ make + + # Install the extension + $ make install + + + + For more information on the installation procedures, you can refer to the + + + + + Enable the Extension in PostgreSQL + + + Connect to your PostgreSQL database using psql: + + + + $ psql -U your_username -d your_database + + + + Inside the PostgreSQL interactive terminal, enable the extension: + + + + CREATE EXTENSION pg_plpgsql_ext; + + + + For more information on the CREATE EXTENSION command, you can refer to the PostgreSQL documentation on . + + + + + Test the Extension + + + -- Example usage of the function + SELECT subtract_numbers(10, 5); + + -- Output: + subtract_numbers + ----------------- + 5 + (1 row) + + + + diff --git a/doc/src/sgml/plpython.sgml b/doc/src/sgml/plpython.sgml index e05e607aba..2590fff14c 100644 --- a/doc/src/sgml/plpython.sgml +++ b/doc/src/sgml/plpython.sgml @@ -1394,4 +1394,185 @@ plpy.execute("UPDATE tbl SET %s = %s WHERE key = %s" % ( command-line interpreter and not an embedded Python interpreter.) + + + + Quick Start: Creating a PostgreSQL Extension using PL/Python + + + In this quick start guide, we will create a simple PostgreSQL extension using PL/Python. + + + + Prerequisites + + + + PostgreSQL installed and running on your system. + + + plpython3u procedural language available in your PostgreSQL installation. + + + Basic knowledge of Python programming. + + + make utility for building and compiling software. + + + Knowledge about the Postgres Development Libraries. + + + + + + + Create the Extension Directory + + + Create a new directory for your extension project. + + + + $ mkdir ~/pg_py_ext + $ cd ~/pg_py_ext + + + + + Create the SQL Script File + + + Create a file named pg_py_ext--1.0.0.sql in the pg_py_ext directory with the following content: + + + + -- pg_py_ext--1.0.0.sql + -- Create the function that uses the Python script + CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) + RETURNS integer + LANGUAGE plpython3u + AS $$ + def add_numbers(a, b): + return a + b + return add_numbers(int(a), int(b)) + $$; + + + Explanation of the SQL Script: + + + + CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer LANGUAGE plpython3u AS $$: + This statement creates the function add_numbers with two integer arguments (a and b). The function will return an integer. The LANGUAGE plpython3u specifies that the function is written in PL/Python3U. + + + + + def add_numbers(a, b):: + This is the Python code block that defines the add_numbers function. The function takes two arguments (a and b) and returns their sum. + + + + + + + + Create the Control File + + + Create a file named pg_py_ext.control with the following content: + + + + # pg_py_ext.control + + # Extension name + comment = 'A simple PostgreSQL extension using PL/Python3U.' + + # Default version of the extension + default_version = '1.0.0' + + # Extension is relocatable + relocatable = true + + + + + Create the Makefile + + + Create a file named Makefile in the pg_py_ext directory with the following content: + + + + # Makefile for pg_py_ext + EXTENSION = pg_py_ext + DATA = pg_py_ext--1.0.0.sql + + PG_CONFIG = pg_config + PGXS := $(shell $(PG_CONFIG) --pgxs) + include $(PGXS) + + + + + Build and Install the Extension + + + Let's build and install the pg_py_ext extension: + + + + # Build the extension + $ make + + # Install the extension + $ make install + + + + For more information on the installation procedures, you can refer to the + + + + + Enable the Extension in PostgreSQL + + + Connect to your PostgreSQL database using psql: + + + + $ psql -U your_username -d your_database + + + + Inside the PostgreSQL interactive terminal, enable the extension: + + + + CREATE EXTENSION pg_py_ext; + + + + For more information on the CREATE EXTENSION command, you can refer to the PostgreSQL documentation on . + + + + + Test the Extension + + + -- Example usage of the function + SELECT add_numbers(3, 5); + + -- Output: + add_numbers + ------------- + 8 + (1 row) + + +