Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2016 November PostgreSQL: Create Prepared Statement, to increase Session Performance

PostgreSQL: Create Prepared Statement, to increase Session Performance

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

The PostgreSQL Prepared Statement or Parameterized Statements are always good for specific purpose like: can create Prepared Statement for frequently executing query of a session, It also prevents from SQL Injections.

Prepared Statements are faster for a particular session because It does not require parsing and compiling for each execution. It is only session specific and once a session kill, Prepared Statements automatically destroy.

Here, You can access more on Prepared Statements.

Prepared or Parameterized Statements in Database System

Small demonstration of PostgreSQL Prepared Statement:

Create a table with sample records:

1
2
3
4
5
6
7
8
9
10
11
CREATE TABLE tbl_Students
(
StudID INTEGER PRIMARY KEY
,StudName CHARACTER VARYING
,StudClass CHAR(1)
);
 
INSERT INTO tbl_Students
VALUES (1,'Anvesh','A')
,(2,'Neevan','B'),(3,'Jenny','C')
,(4,'Roy','C'),(5,'Martin','C');

Create a Prepared Statement to SELECT Students data:

1
2
PREPARE pre_GetStudents (INT) AS
SELECT *FROM tbl_Students WHERE StudID = $1;

Execute a Prepared Statement:

1
EXECUTE pre_GetStudents(2);

System view to check the Prepare statements of session:

1
SELECT *FROM pg_prepared_statements;

Nov 27, 2016Anvesh Patel
PostgreSQL: Performance Test of RETURNS TABLE vs OUT ParametersPostgreSQL: Using PSQL, Disable Autocommit Globally
Comments: 1
  1. darshan shah
    May 9, 2019 at 5:59 am

    Hi Avnesh,

    I have scenario of prepared transactions. I’m thinking to make automated process to handle this.
    so used below script to identify orphan prepared transactions.

    Select * from (
    SELECT gid, DATE_PART(‘hour’,now()::timestamp-prepared::timestamp)*60+DATE_PART(‘minute’,now()::timestamp-prepared::timestamp)as TimeDiff,prepared, owner, database, transaction AS xmin
    FROM pg_prepared_xacts
    where database=current_database()) As A
    Where TimeDiff>10
    ORDER BY TimeDiff DESC LIMIT 1

    To rollback this transaction I prepared one script with DO statment/ also tried with function. However can not run rollback prepared Transactionid inside any plpgsql block.

    DO $$
    DECLARE dbname text;
    Declare str text;
    BEGIN

    str= (select gid FROM pg_prepared_xacts where database=current_database() limit 1);
    str:=’ROLLBACK PREPARED ‘||””||str||””||’;’;
    Execute (str);
    END $$ ;

    Do you have any other option for same ?

Anvesh Patel
Anvesh Patel

Database Engineer

November 27, 2016 PostgreSQLAnvesh Patel, database, database research and development, dbrnd, Parameterized Statements, performance, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, PREPARE, Prepared Statements, Session, SQL Injection
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....