DEV Community

Cover image for Do You Really Need to Suffer with No-SQL and Big Data? πŸ€”Be happy 😊 and just use PostgreSQL! πŸš€
Rodrigo Estrada
Rodrigo Estrada

Posted on

Do You Really Need to Suffer with No-SQL and Big Data? πŸ€”Be happy 😊 and just use PostgreSQL! πŸš€

Are You Unnecessarily Struggling with NoSQL and Big Data? 🀯
Many teams are struggling with unexpected costs, operational overhead, cognitive load, dependency on highly specific knowledge, excessive optimization efforts, and a lack of documentation β€” forcing themselves to use Redis, MongoDB, Cassandra, DocumentDB, ElasticSearch, and similar technologies. They seek low latency and scalability that they often don’t need and, in many cases, are not even achieving due to the complexity involved. These technologies are trendy, appear in job postings, and are frequently highlighted in tech radar discussions. But are they truly necessary for most problems?

When to Step Away from SQL? πŸšͺ
Before considering other databases and technologies, it’s important to ask: Do you really need them?

SQL is fantastic and fits most cases, but if you hit these limits, you might need something else:

❌ More than 100TB of Data? Consider a Data Lake like Apache Iceberg or Delta Lake.

❌ Sub-2ms Query Response? Hello, Redis.

❌ Vector Databases for ML? Consider Pinecone for high-performance and scalable vector search.

❌ Active-Active Multi-Region? Consider CockroachDB instead of ACID SQL.

But let’s be real β€” how many projects actually exceed these limits? Most don’t! PostgreSQL can handle a vast majority of workloads without unnecessary complexity.

There are many myths about SQL databases, and it’s time to debunk them! πŸ’‘

Myth #1: SQL Databases Are Slow πŸ’βž‘οΈπŸš€
SQL databases are incredibly fast in the context they were designed for. They are optimized for efficiency and can outperform many NoSQL solutions when used correctly.

Myth #2: SQL Is Hard to Learn 🧐
SQL is a 4th generation language (4GL) β€” simpler, easier to read, write, and share compared to 3GL languages like Java, Python, C#, or Go. Plus, SQL is declarative, meaning you describe what you want, and the engine figures out the best way to get it. No imperative programming needed!

Myth #3: SQL Is Just Text and Lacks Compile-Time Verification πŸ“
Every language is β€œjust text” until the right tools come in! Modern SQL tools provide autocomplete, verification, and optimization. Stop treating SQL as just a string β€” use ORM libraries or query builders to make life easier. πŸ˜‰

PostgreSQL: Your Swiss Army Knife πŸ”ͺ

A properly tuned PostgreSQL instance can handle 10TB–100TB, process simple queries in 1–10ms, complex queries in 100ms, and achieve 100k TPS (transactions per second).

πŸš€ Not bad, right?

Here’s how you can use PostgreSQL for almost everything:

βœ… Simple Queries? Use good indexing.

CREATE INDEX idx_user_email ON users(email);

Enter fullscreen mode Exit fullscreen mode

βœ… High Read/Write Workloads? Go master-slave replication.

-- On the Master Node
ALTER SYSTEM SET wal_level = replica;
ALTER SYSTEM SET max_wal_senders = 10;
ALTER SYSTEM SET hot_standby = on;
SELECT pg_reload_conf();
-- Create a replication user
CREATE USER replicator WITH REPLICATION ENCRYPTED PASSWORD 'yourpassword';
-- On the Slave Node
SELECT pg_create_physical_replication_slot('replica_slot');
-- Start replication
pg_basebackup -h master_host -D /var/lib/postgresql/data -U replicator -P -R
Enter fullscreen mode Exit fullscreen mode

βœ… Horizontal Scaling? Implement Citus for sharding.

-- Install Citus extension
CREATE EXTENSION IF NOT EXISTS citus;
-- Create a distributed table
SELECT create_distributed_table('orders', 'customer_id');

-- Insert data
INSERT INTO orders (customer_id, order_total) VALUES (1, 100.00), (2, 200.00);
-- Query data across shards
SELECT customer_id, SUM(order_total) FROM orders GROUP BY customer_id;
Enter fullscreen mode Exit fullscreen mode

βœ… Complex Aggregations? Use window functions & CTEs.

WITH avg_sales AS (
  SELECT region, AVG(sales) OVER(PARTITION BY region) AS avg_sales
  FROM sales_data
)
SELECT * FROM avg_sales;
Enter fullscreen mode Exit fullscreen mode

βœ… Key/Value Store? Use hstore.

CREATE TABLE kv_store (id SERIAL PRIMARY KEY, data hstore);

Enter fullscreen mode Exit fullscreen mode

βœ… Full-Text Search? Leverage GIN indexes.

CREATE INDEX gin_index ON articles USING gin(to_tsvector('english', content));
Enter fullscreen mode Exit fullscreen mode

βœ… JSON/XML Storage? PostgreSQL handles both with JSONB.

SELECT data->>'name' FROM users WHERE data @> '{"role": "admin"}';

Enter fullscreen mode Exit fullscreen mode

βœ… Geospatial Data? Use PostGIS.

SELECT name, ST_AsText(location) 
FROM places 
WHERE ST_DWithin(location, ST_MakePoint(-73.935242, 40.730610)::geography, 5000);
Enter fullscreen mode Exit fullscreen mode

βœ… Vector Search? Use PGVector for vector-based similarity search.

CREATE EXTENSION IF NOT EXISTS vector;


CREATE TABLE items (
    id SERIAL PRIMARY KEY,
    embedding VECTOR(3)
);

INSERT INTO items (embedding) VALUES ('[0.1, 0.2, 0.3]');


SELECT id, embedding <-> '[0.1, 0.2, 0.4]' AS similarity
FROM items
ORDER BY similarity LIMIT 5;
Enter fullscreen mode Exit fullscreen mode

βœ… Graph Databases? PostgreSQL has AGE for graphs.

SELECT * FROM cypher('graph', $$
  MATCH (p:Person)-[:KNOWS]->(f:Person)
  WHERE p.name = 'Alice'
  RETURN f.name
$$) AS (name text);
Enter fullscreen mode Exit fullscreen mode

βœ… Machine Learning? Run ML models directly in SQL with MADlib.

SELECT madlib.linregr_train(
    'ml_training_data',    -- Training table
    'ml_model',            -- Output model
    'y',                   -- Dependent variable
    'ARRAY[x1, x2, x3]'    -- Independent variables
);
Enter fullscreen mode Exit fullscreen mode

βœ… Cross-Database Queries? PostgreSQL can connect to external DBs with postgres_fdw.

CREATE EXTENSION IF NOT EXISTS postgres_fdw;
CREATE SERVER mysql_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'mysql.example.com', dbname 'remote_db', port '3306');
CREATE USER MAPPING FOR current_user SERVER mysql_server OPTIONS (user 'mysql_user', password 'mysql_password');
IMPORT FOREIGN SCHEMA public FROM SERVER mysql_server INTO local_schema;
SELECT * FROM local_schema.remote_table;
Enter fullscreen mode Exit fullscreen mode

βœ… ETL Processing with UDFs? Use SQL + UDFs for transformation in ETL pipelines.

CREATE FUNCTION normalize_text(input_text TEXT) RETURNS TEXT AS $$
BEGIN
  RETURN LOWER(TRIM(input_text));
END;
$$ LANGUAGE plpgsql;

SELECT normalize_text('   Hello World!   ');
Enter fullscreen mode Exit fullscreen mode

βœ… Automated Query Optimization? Use AI-powered tools to analyze and optimize queries automatically:

DBTune β€” AI-driven database performance tuning.
HypoPG β€” Hypothetical indexes for query optimization.
pg_tune β€” PostgreSQL configuration tuning.
Index Advisor β€” Built-in PostgreSQL index recommendations.
AutoExplain β€” Automatic query execution analysis.
And the best part? You can do all this within the same engine! πŸ”₯

Top comments (0)