EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL caching

By Sohel SayyadSohel Sayyad

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL caching

PostgreSQL caching

Definition of PostgreSQL caching

Data caching in PostgreSQL is not suitable or we can say not pre-allocated and it can be calculated only and it depends on workload. The main purpose of caching in PostgreSQL is to increase the performance of the server. The caching means when we execute a query it goes from different stages in which there is one stage we called the planning stage. In the planning stage, we select the best and suitable plan for query execution. Caching is temporary memory and it is used to increase the performance of query as well as the server. Cache is allocated per query execution plan once the query is executed then the cache is vanished.

Syntax:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

explain (analyze, buffers) select * from table name order by column name limit;

Explanation:

In the above syntax, we use an explanatory statement to show the performance of the database table as well as a query execution tool. Where analysis and buffer is used to show details structure of query statement, after that we use select statement for table selection with different parameters such as order by clause.

How caching works in PostgreSQL?

We must install PostgreSQL in your system. We required basic knowledge about PostgreSQL. We must require a database table to perform the caching operation in PostgreSQL. We just need basic knowledge about caching which means how it is worked.

PostgreSQL process based on the system that is when the PostgreSQL process requests for any access through SQL query statement at that time PostgreSQL requests for the buffer allocation. If the memory block is available then it directly returns the result. If a memory block is not available then it goes for buffer allocation to get a free block for query execution.

Examples

Let’s see a different example to better understand PostgreSQL caching as follows.

Example #1

To see the performance of the query we required a table to check the performance of the query statement. So let’s create a table first by using the following query statement as follows.

create table empp (emp_id serial PRIMARY KEY, emp_name varchar(30), emp_dept varchar[],emp_city varchar[],emp_salary text[]);

Explanation:

In the above example, we create an emp table with different attributes such as emp_id, emp_name, emp_dept, emp_city, and emp_salary with different data types as shown in the above statement.

Now insert some records into an emp table by using the following statement as follows.

Insert into empp (emp_name, emp_dept, emp_city, emp_salary) Values ('Jacson','{"comp"}' , '{"City W"}', '{"40000"}'), ('Paul', '{"mech"}', '{"City A"}', '{"20000"}'), ('sam', '{"Account"}','{"City B"}','{"10000"}'), ('John', '{"Purchase"}', '{"City C"}','{"30000"}');
select * from empp;

Explanation:

With the help of the above statement, we insert 4 records into an empp table. Illustrate the end result of the above declaration by using the following snapshot.

postgreSQL Caching 1

Now let’s see the performance test of the above statement by using the caching concept as follows.

Example #2

explain (analyze,buffers) select * from empp order by emp_salary limit 2;

 Explanation:

See in the above example we execute a select query that means data manipulation query. When we execute a select statement, in which if that page is available in memory then it is able to be written otherwise they get the page from disk to written. When we execute the above statement it shows the detailed structure of the select statement. Here we use buffers to analyze the select query. Illustrate the end result of the above declaration by using the following snapshot.

Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)2 Online Courses | 1 Hands-on Project | 7+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (3,311 ratings)
Course Price

View Course

Related Courses

postgreSQL Caching 2

The above snapshot shows the shared hit means cache configuration is correct. It shows the actual time, planning time, and execution time.

Let’s see what happens when we execute the same query statement as follows.

 explain (analyze,buffers) select * from empp order by emp_salary limit 2;

Explanation:

Here we execute the same query statement then it shows the different values of shared hit, actual time, planning time, and execution time. Illustrate the end result of the above declaration by using the following snapshot.

postgreSQL Caching 3

Example #3: Sequential Scan

A sequential scan means there is no index in PostgreSQL and has to fetch all records or data from disk, so it creates a problem for cache to fetch the data. When we use a single scan it erases all data from the cache so it is handled by using a different way. Instead of using LRU algorithm, it uses a series of buffers as follows.

explain (analyze,buffers) select count(*) from empp;

Explanation:

In the above example, we show a sequential buffer concept in which the shared hit is 1 that means it used a single buffer for execution. This value may vary, that is it depends on the query statement. Illustrate the end result of the above declaration by using the following snapshot.

postgreSQL Caching 4

How to Improve the performance of caching in PostgreSQL?

When we think about caching that means it increases the speed of operations, increases the retrieval speed of data. Let’s discuss different scenarios to increase the performance of caching as follows.

1. Bottleneck situation in the query performance

Before discussing the solution to the above problem let’s understand why queries are slow. The first reason is the performance of your system. That means when we run an application on a system at that time we check the performance of the system. For that instance, any CPU or network bottleneck may occur that affect your process means fetching data from the database. The solution for that problem is to check the health of your system regularly and for bottleneck increase or scale up the database size.

2. Internal Cache Performance

Internal cache performance means the hit rate of the database table. Sometimes the hit rate of the database table is low due to insufficient internal buffer space of the database this is the main reason of the table hit rate is low. So in PostgreSQL, we use the shared buffer concept to avoid a low rate of database table. A shared buffer is used to manage the buffer space of a database.

Conclusion

We hope from this article you have understood about the PostgreSQL Caching. From the above article, we have learned the basic syntax PostgreSQL Caching. We have also learned how we can implement them in PostgreSQL with different examples of each method. From this article, we have learned how we can handle PostgreSQL Caching in PostgreSQL.

Recommended Articles

This is a guide to PostgreSQL caching. Here we discuss the introduction, How caching work in PostgreSQL? How to Improve the performance of caching in PostgreSQL? and examples respectively. You may also have a look at the following articles to learn more –

  1. PostgreSQL Compare Strings
  2. PostgreSQL Text Search
  3. PostgreSQL OID
  4. PostgreSQL Clustered Index

PostgreSQL Course (2 Courses, 1 Project)

2 Online Courses

1 Hands-on Project

7+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Merge
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PostgreSQL Course (2 Courses, 1 Project) Learn More