PostgreSQL

Postgresql stored procedure example

Postgresql database is more reliable than others because it can create and store the data in the form of relations, procedures, etc. Postgresql is a customizable database, as we can modify the storage containers according to our requirements. The data in Postgresql is managed by the schemas and catalogs. Postgresql supports many languages, which means that we can execute queries in any programming language either in the psql (shell) or on the pgAdmin side.

Just like the temporary tables, we also use some other features to involve the storage capacity. These are called the “STORED PROCEDURES”. These are not shown like the tables. But silently works with the tables.

In Postgresql or any other database management system, we use functions to perform operations on the data. These functions are user-created or user-defined. One major drawback of these functions is that we are unable to execute transactions inside the functions. We cannot commit or rollback. That’s why we use the stored procedures. By using these procedures, application performance is increased. Moreover, we can use more than one SQL statement inside a single procedure. There are three types of parameters.

IN: It is the input parameter. It is used to insert the data from the procedure into the table.

OUT: It is the output parameter. It is used to return the value.

INOUT: It represents both input and output parameters. As they can pass and returns the value.

SYNTAX

CREATE OR REPLACE PROCEDURE procedure-name (parameters-list)  

LANGUAGE plpgsql  

AS  $$  

DECLARE  

(variable names of procedure)

BEGIN  

    --- SQL statements / logic /condition.  
   
END  $$

Install Postgresql in your system. After successful configuration, we are now able to access the database. We have two choices to apply the queries. One is psql shell, whereas the other one is the pgAdmin dashboard. We have used pgAdmin for this purpose. Open the dashboard, now provide the password to maintain the connection with the server.

Procedure Creation

To understand the working of the stored procedure, we need to create a relationship by using a create statement.

>> create table clothes ( id serial, name varchar(100), city varchar(100), color varchar(100), price integer);

Usually, we enter the values in the table by using the “insert” statement, but here we use a stored procedure that will use as the temp table. Firstly the data will be stored in them, and then they will further transfer the data in the table.

Create a stored procedure name “Addclothes”. This procedure will act as a medium between the query and the table. Because all the values are first inserted in this procedure and are then inserted through the insert command directly to the table.

>> Create OR REPLACE PROCEDURE Addclothes (c_ID INOUT INT, C_Name varchar(100),c_city varchar(100),c_color varchar(100),c_price integer)

LANGUAGE plpgsql AS

$$ BEGIN

INSERT INTO clothes (Name, city,color,Price ) Values (c_Name, c_city, c_color, c_price ) RETURNING  ID INTO c_ID;

END $$;

Now the values from the stored procedure are entered in the table clothes. From the query, it is clear that first, we have defined the store procedure with the attribute of slightly different column names with the same data types. Then, using an insert statement, the values from the values of the stored procedure are entered into the table.

Just like a simple function, we use a function call to send the values as arguments in the parameter so that the procedure accepts these values.

>> CALL Addclothes (null, ‘T-shirt’, ‘red’, ‘New York’, 1500);

As the name of the procedure is “Addclothes”, so it is written with the values in the same way as we directly write them in the insert statement. The output is shown as 1; as we used the returning method, this shows that one row is filled. We will see the data inserted by using a select statement.

>> select * from clothes;

Repeat the above procedure up to the extent you want to enter the values.

Stored Procedure and the “UPDATE” clause

Now create the procedure to update the already existed data in the table “clothes”. The first part of the query in entering values to the stored procedure is the same.

$$ BEGIN

UPDATE clothes SET Name = c_name, city = c_city, color =c_color, price = c_price WHERE ID = c_ID;

END $$

Now we will call the stored procedure. The call syntax is the same, as it only uses the values in the parameters as arguments.

A message is displayed that shows that the query is successfully executed with the time of execution. Use the select statement to fetch all the records to see the values that are replaced.

The procedure with the“DELETE” clause

The next store procedure we will use here is the “deleteclothes”. This procedure will get only the id as an input, and then the variable is used to match the id with the id present in the table. When the match is found, the row is deleted respectively.

>> CREATE OR REPLACE PROCEDURE Deleteclothes

(c_ID INT

)

LANGUAGE plpgsql AS

$$ BEGIN

DELETE from clothes WHERE ID =c_Id;

END $$

Now we will call the procedure. This time only a single id is used. This id will locate the row that is to be deleted.

>> call Deleteclothes(2)

The row having the id “2” will be deleted from the table.

>> select * from clothes;

There were 3 rows in the table. Now you can see that only two rows are left because the row with “2” id is deleted from the table.

Function Creation

After the complete discussion of the stored procedure, we will now consider how the user-defined functions are introduced and used.

>> CREATE OR REPLACE FUNCTION GetAllclothes() RETURNS clotes

LANGUAGE SQL

AS $$

SELECT * FROM clothes;

$$;

A function is created with the same name as the stored procedure. All the data from the table “clothes” is displayed in the result data output portion. This return function will not take any argument in the parameter. By using this function, we got the data as shown above in the image.

The other function is used to get the clothes data from the specific id. A variable in the integer is introduced in the parameter. This id will be matched with the id in the table. Where the match is found, the specific row is displayed.

>> CREATE OR REPLACE FUNCTION GetclothesById(c_ID INT) RETURNS clothes

LANGUAGE SQL

AS $$

SELECT * FROM clothes WHERE ID = c_ID;

$$;

As an argument, we will call the function having the id we want to fetch the records from the table.

>> select * from FerclothesById(3);

Hence from the output, you can see that only a single row is fetched from the table “clothes”.

Conclusion

“Postgresql stored procedure example” elaborates the examples with the creation and operations of the procedures. Functions had a drawback that was removed by the Postgresql stored procedures. Examples regarding procedures and functions are elaborated that will be sufficient for gaining knowledge regarding the procedures.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.