DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Formulating a Robust Strategy for Storage in Amazon Relational Database Service PostgreSQL Deployments
  • Just Use PostgreSQL, a Quick-Start Guide: Exploring Essential and Extended Capabilities of the Most Beloved Database
  • Understanding RDS Costs
  • It’s Time to Use a Data Privacy Vault

Trending

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Building a Real-Time Change Data Capture Pipeline With Debezium, Kafka, and PostgreSQL
  • Web Crawling for RAG With Crawl4AI
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  1. DZone
  2. Data Engineering
  3. Databases
  4. Fast Key-Value Store With PostgreSQL

Fast Key-Value Store With PostgreSQL

Explore Key-Value store features which augments the already fantastic NoSQL tool set that PostgreSQL offers.

By 
Everett Berry user avatar
Everett Berry
·
Oct. 09, 21 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
6.6K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction to Key-Value Store

We have seen several features in PostgreSQL that goes beyond relational storage, such as:

  • JSON
  • Geospatial data
  • Fuzzy Search
  • Full-Text

This article will explore the key-value store feature, which is less popular, but still plays an integral part in PostgreSQL's NoSQL capabilities.

Setting Up PostgreSQL With Extension and Data

Unlike JSON and JSONB, the key-value store comes as a separate extension called HStore. We can install it right away as it comes bundled with PostgreSQL installation itself.

Enabling HStore

PLSQL
 
CREATE EXTENSION HSTORE

This will enable the key-value store extension.

Note: Extension can be enabled only with superuser access. The system will throw an error otherwise.

Creating Table and HStore Columns

Let's create a simple demo table where we maintain scores of people in key-value fashion. To do that, we need to create the data type itself as hstore.

PLSQL
 
CREATE TABLE hstore_example (score hstore)

We do not need to specify the key-value structure type (i.e., whether the key is an int or text, as the default type will always be text). Proper datacasting is necessary either at the database level or application level for manipulation. This is similar to the JSON data type, where all of the data is just JSONB.

Populating Test Data

Inserting data into hstore columns is pretty straightforward.

PLSQL
 
INSERT INTO hstore_example values('"Jason" => 100');
INSERT INTO hstore_example values('"Jack" => 200');
INSERT INTO hstore_example values('"Perry" => 150');




Getting Started with Key-Value Queries

We can query for rows by searching for keys.

PLSQL
 
SELECT
    *
FROM
    hstore_example
WHERE
    score ? 'Jason';


Get a value for a particular key,

PLSQL
 
SELECT
    score -> 'Jason' as score
FROM
    hstore_example
WHERE
    score -> 'Jason' is NOT NULL 




HStore
https://www.postgresql.org/docs/current/hstore.html

There are several powerful operators present that can help us do a variety of data manipulations without having to handle them in application logic. We should always do data manipulation at the database level rather than at the application level for a variety of reasons such as performance, security, etc.,

Indexing for Faster Queries

The HStore extension supports indexes of the GIN type. We can create these indexes as follows,

PLSQL
 
CREATE INDEX hstore_example_idx ON hstore_example USING GIN (score)


This is similar in speed and power to the GIN index we can create for the JSON type. It can significantly speed up queries, particularly if the keys are nested and searching is not straightforward with normal comparisons. These indexes are considerably smaller than a JSON GIN and can efficiently operate from the memory/cache for quicker lookups.

On the other hand, an index will slow down the writes, so a proper tradeoff has to be considered before we extensively use an index. Benchmarking should be done before taking an application/solution to production.

Internal Structure of HStore

HStore is internally stored as a varlena. Similar to JSON, the whole field has to be read from the disk to do any read/modifications. As a result, HStore is not really optimized compared to traditional key-value stores such as Redis and Memcache.

The documentation about TOAST table structure is also highly recommended on why the reads/writes can only be done as a whole field rather than partial updates, https://www.postgresql.org/docs/current/storage-toast.html

For this reason, if the K/V structure is complex and nested, we are better off storing it as a proper JSON or JSONB type.

Conclusion

Potential use case scenarios can be similar but not limited to the below examples,

  • Maintaining a cache.
  • Fast read/update scenarios such as maintaining an API rate limiter.
  • Maintaining scores/simple time-series data.

HStore is not a replacement for Key-Value stores; they are much more optimized for different use cases. But as already mentioned, these features exist in PostgreSQL because there are many situations where using a separate data store would be an overkill and a maintenance issue. We can use PostgreSQL's NoSQL capabilities as a bridge to fill that gap. If the business expands, we can migrate to a different database. Still, until then, we can comfortably use PostgreSQL, which provides ACID-compliant NoSQL features without the additional overhead of maintaining a separate database.

Database PostgreSQL Relational database Test data

Published at DZone with permission of Everett Berry. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Formulating a Robust Strategy for Storage in Amazon Relational Database Service PostgreSQL Deployments
  • Just Use PostgreSQL, a Quick-Start Guide: Exploring Essential and Extended Capabilities of the Most Beloved Database
  • Understanding RDS Costs
  • It’s Time to Use a Data Privacy Vault

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!