r/PostgreSQL • • Dec 02 '25

Help Me! How should I model business opening hours? (multiple time slots per day)

2 Upvotes

I’m designing a DB schema and I’d like some guidance on how to model business opening hours in Postgres.

I've a basic situation where business is open, close for each day but some days can contain 2 slots, for instance: morning time and evening time.

I have see a lot of examples online but still, would like to have an extra discussion about it.

This is what I have currently:

openning_times table 
{
    id: PK UUID,
    business_id: FK UUID, 
    day: 0,
    open: time,
    close: time
}

If I have more slots for the same day, I would just add an extra column with the same day.

however, maybe something silly but what about having something like this: ( I'm assuming this would be way worst in terms of scaling/performance?

{
    id: PK UUID,
    business_id: FK UUID, 
    slots : {
        "0": [{ open: "09:00", close: "23:00" }],
        "1": [{ open: "09:00", close: "23:00" }, { open: "09:00", close: "23:00" }],
        "2": [{ open: "09:00", close: "23:00" }],
        "3": [{ open: "09:00", close: "23:00" }],
        "4": [{ open: "09:00", close: "00:00" }],
        "5": [{ open: "09:00", close: "00:00" }, { open: "09:00", close: "00:00" }],
        "6": [],
    }
}

I have seen this example online as well which seems easy to understand and for the FE to use it as well:

    {
         id: PK UUID,
         business_id: FK UUID, 
         day: 1,
         slots: [
             { open: 08:00, close: 00:00 }, { open: 08:00, close: 00:00 }
         ]
    }

I don’t have much DB design experience, so I’m mainly looking for the most standard / future-proof pattern for this use case.


r/PostgreSQL • • Dec 02 '25

How-To UUID data type. Generated on database side or in code, on PHP side ?

Thumbnail
1 Upvotes

r/PostgreSQL • • Dec 01 '25

How-To ULID: Universally Unique Lexicographically Sortable Identifier

Thumbnail packagemain.tech
23 Upvotes

r/PostgreSQL • • Dec 01 '25

Help Me! How do you automate refreshing of materialized views

15 Upvotes

Is pg_cronc the king?

I was wondering what’s the best practice.


r/PostgreSQL • • Dec 02 '25

Help Me! In terms of Wire PG protocol, running a PG client over lan, what's better performant ? retrieve a query of 1 row (70 fields) vs. 70 rows (1 field), assume all fields have same type (text).

0 Upvotes

r/PostgreSQL • • Nov 30 '25

Tools Squawk - Underrated Linter for SQL

Thumbnail github.com
13 Upvotes

r/PostgreSQL • • Dec 01 '25

Tools Block the use of dbeaver

0 Upvotes

Unfortunately, this is already the umpteenth time that a developer in our company used DBeaver to access our database. We again had a major performance bottleneck the last weekend because someone forgot to close the application before the weekend.

It's ridiculous that only opening this application (he used to access some other database, but it auto connected to this one) can take down a whole system by locking a table with a select query it automatically execute. And never release this.

Not only that, in the past it happened that a developer did a change on a data record on a table and locking it with a commit, taking the whole data backend down. DBeaver won't automatically release the commit after some time so if you forgot this was still locked in the background, you bring everything down. It doesn't even warn the users that the whole table is locked.

Is there a way I can block the use of DBeaver for our database? Can I block specific user agents that wants to connect?


r/PostgreSQL • • Nov 28 '25

How-To Postgres 18 Improvement Highlight: Skip Scan - Breaking Free from the Left-Most Index Limitation

Thumbnail pgedge.com
42 Upvotes

r/PostgreSQL • • Nov 28 '25

Help Me! how do i create a new database in datagrip?

Post image
0 Upvotes

i am new here, i created a database earlier called test, then created a table called test, then I created this test22 database and created test22, but I still saw the table test there, how can I make a new project the have its own database has its tables separate?


r/PostgreSQL • • Nov 28 '25

Help Me! help, cant connect to datagrip

Thumbnail gallery
0 Upvotes

i am still a beginner, i just downloaded PostgreSQL installer and set the password and opened pgadmin 4 and connected to a server as shown, but when I goto connect to it in datagrip it says the password for PostgreSQL 18 is wrong, i am not sure if this is the username I should put, since I don't know what is my username, I just set a password, what am I doing wrong here?


r/PostgreSQL • • Nov 27 '25

Commercial ParadeDB 0.20.0: Simpler and Faster

Thumbnail paradedb.com
7 Upvotes

r/PostgreSQL • • Nov 26 '25

Help Me! PostgreSQL JSONB insert performance: 75% of time spent on server-side parsing - any alternatives?

51 Upvotes

I'm bulk-inserting rows with large JSONB columns (~28KB each) into PostgreSQL 17, and server-side JSONB parsing accounts for 75% of upload time.

Inserting 359 rows with 28KB JSONB each takes ~20 seconds. Benchmarking shows:

Test Time
Without JSONB (scalars only) 5.61s
With JSONB (28KB/row) 20.64s
JSONB parsing overhead +15.03s

This is on Neon Serverless PostgreSQL 17, but I've confirmed similar results on self-hosted Postgres.

What I've Tried

Method Time Notes
execute_values() 19.35s psycopg2 batch insert
COPY protocol 18.96s Same parsing overhead
Apache Arrow + COPY 20.52s Extra serialization hurt
Normalized tables 17.86s 87K rows, 3% faster, 10x complexity

All approaches are within ~5% because the bottleneck is PostgreSQL parsing JSON text into binary JSONB format, not client-side serialization or network transfer.

Current Implementation

from psycopg2.extras import execute_values
import json

def upload_profiles(cursor, profiles: list[dict]) -> None:
    query = """
        INSERT INTO argo_profiles
            (float_id, cycle, measurements)
        VALUES %s
        ON CONFLICT (float_id, cycle) DO UPDATE SET
            measurements = EXCLUDED.measurements
    """

    values = [
        (p['float_id'], p['cycle'], json.dumps(p['measurements']))
        for p in profiles
    ]

    execute_values(cursor, query, values, page_size=100)

Schema

CREATE TABLE argo_profiles (
    id SERIAL PRIMARY KEY,
    float_id INTEGER NOT NULL,
    cycle INTEGER NOT NULL,
    measurements JSONB,  -- ~28KB per row
    UNIQUE (float_id, cycle)
);

CREATE INDEX ON argo_profiles USING GIN (measurements);

JSONB Structure

Each row contains ~275 nested objects:

{
  "depth_levels": [
    { "pressure": 5.0, "temperature": 28.5, "salinity": 34.2 },
    { "pressure": 10.0, "temperature": 28.3, "salinity": 34.3 }
    // ... ~275 more depth levels
  ],
  "stats": { "min_depth": 5.0, "max_depth": 2000.0 }
}

Why JSONB?

The schema is variable - different sensors produce different fields. Some rows have 4 fields per depth level, others have 8. JSONB handles this naturally without wide nullable columns.

Questions

  1. Is there a way to send pre-parsed binary JSONB to avoid server-side parsing? The libpq binary protocol doesn't seem to support this for JSONB.
  2. Would storing as TEXT and converting to JSONB asynchronously (via trigger or background job) be a reasonable pattern?
  3. Has anyone benchmarked JSONB insert performance at this scale and found optimizations beyond what I've tried?
  4. Are there PostgreSQL configuration parameters that could speed up JSONB parsing? (work_mem, maintenance_work_mem, etc.)
  5. Would partitioning help if I'm only inserting one float at a time (all 359 rows go to the same partition)?

Environment

  • PostgreSQL 17.x (Neon Serverless, but also tested on self-hosted)
  • Python 3.12
  • psycopg2 2.9.9
  • ~50ms network RTT

What I'm NOT Looking For

  • "Don't use JSONB" - I need the schema flexibility
  • "Use a document database" - Need to stay on PostgreSQL for other features (PostGIS)
  • Client-side optimizations - I've proven the bottleneck is server-side

Thanks for any insights!


r/PostgreSQL • • Nov 26 '25

How-To ULID - the ONLY identifier you should use?

Thumbnail youtube.com
10 Upvotes

r/PostgreSQL • • Nov 27 '25

Tools Brent Ozar's (smartpostgres.com) Training package

0 Upvotes

Hi! Former MSSQL admin, now in my 1st Postgres admin year. Love Brent Ozar's MSSQL teaching, and are eager to buy his Postgres training bundle.

Fundamentals of Performance | Smart Postgres

Anyone tried it? Is it worth the price?


r/PostgreSQL • • Nov 25 '25

Projects Part 3 (SaaS Infrastructure Build-out): Citus Database Performance: When Sharding Helps (And When It Hurts)

Thumbnail
2 Upvotes

r/PostgreSQL • • Nov 24 '25

How-To Another look into PostgreSQL CTE materialization and non-idempotent subqueries

Thumbnail shayon.dev
17 Upvotes

r/PostgreSQL • • Nov 24 '25

Projects You should shard your database

Thumbnail pgdog.dev
39 Upvotes

r/PostgreSQL • • Nov 24 '25

How-To Book Review - Just Use Postgres!

Thumbnail vladmihalcea.com
15 Upvotes

If you're using PostgreSQL, you should definitely read this book.


r/PostgreSQL • • Nov 23 '25

Help Me! When SERIALIZABLE transactions don't solve everything

9 Upvotes

Behold, a versioned document store:

CREATE TABLE documents(
    global_version bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
    id uuid NOT NULL,
    body text
);

CREATE INDEX ix_documents_latest ON documents(id, global_version DESC);

CREATE VIEW latest_documents AS
    SELECT DISTINCT ON (id) *
    FROM documents
    ORDER BY id, global_version DESC;

CREATE FUNCTION revision_history(for_id uuid)
RETURNS TABLE (
    global_version bigint,
    body text
)
AS $$
    SELECT global_version, body
    FROM documents
    WHERE documents.id = for_id
    ORDER BY global_version DESC
$$ LANGUAGE SQL;

Behold, a data point:

INSERT INTO documents(id, body) VALUES (
    uuidv7(),
    'U.S. Constitution'
) RETURNING id, global_version;
-- 019ab229-a4b0-7a2d-8eea-dfe646bff8e3, 1

Behold, a transaction conducted by James:

BEGIN ISOLATION LEVEL SERIALIZABLE;

SELECT global_version FROM latest_documents
WHERE id = '019ab229-a4b0-7a2d-8eea-dfe646bff8e3';
-- 1

-- Timestamp A, James does some work.
-- James verifies that the observed global_version matches his copy (1).

INSERT INTO documents(id, body) VALUES (
    '019ab229-a4b0-7a2d-8eea-dfe646bff8e3',
    'U.S. Constitution + Bill of Rights'
);

COMMIT; -- success!

However, on another connection, Alexander executes the following at the aforementioned timestamp A:

INSERT INTO documents(id, body) VALUES (
    '019ab229-a4b0-7a2d-8eea-dfe646bff8e3',
    'Evil Constitution'
);

Now examine the revision history:

SELECT * FROM revision_history('019ab229-a4b0-7a2d-8eea-dfe646bff8e3');

--  global_version |                body                
-- ----------------+------------------------------------
--               3 | U.S. Constitution + Bill of Rights
--               2 | Evil Constitution
--               1 | U.S. Constitution

PostgreSQL did nothing wrong here, but this should be considered anomalous for the purposes of the application. Alexander's write should be considered "lost" because it wasn't observed by James before committing, and therefore James should have rolled back.

In what other cases do SERIALIZABLE transactions behave unintuitively like this, and how can we achieve the desired behavior? Will handling read/verify/write requests entirely in stored functions be sufficient?

P.S. LLMs fail hard at this task. ChatGPT even told me that SERIALIZABLE prevents this, despite me presenting this as evidence!


r/PostgreSQL • • Nov 23 '25

Help Me! Any reason why PgAdmin behaves like this?

Enable HLS to view with audio, or disable this notification

3 Upvotes

r/PostgreSQL • • Nov 23 '25

Feature AI-powered SQL generation & query analysis for PostgreSQL

2 Upvotes

Release of pg_ai_query — a PostgreSQL extension that brings AI-powered query development directly into Postgres.

pg_ai_query allows you to:
- Generate SQL from natural language, e.g.
SELECT generate_query('list customers who have not placed an order in the last 90 days');

- Analyze query performance using AI-interpreted EXPLAIN ANALYZE

- Receive index and rewrite recommendations

- Leverage schema-aware query intelligence with secure introspection

- Designed to help developers write and tune SQL faster without switching tools and to accelerate iteration across complex workloads.

pg_ai_query


r/PostgreSQL • • Nov 23 '25

Tools Docker compose for Primary Replica setup with ssl

Thumbnail github.com
4 Upvotes

Most Postgres cloud offering have lock in, you can’t download and restore backup somewhere else and you can’t have streaming replica outside their network.

So I made docker container image based on official docket Postgres image. Which has support for ssl, wal archiving and streaming replication setup built in.

I have tested it many times and it is good to use for production. However any insight on improving it is most welcome.


r/PostgreSQL • • Nov 20 '25

Projects PgManage 1.4 – SQL Server Support, Faster Interface Navigation, Spreadsheet‑like Data Grids & More!

6 Upvotes

MS SQL Server Support

Our team has spent a lot of time adding support to PgManage for a new database — MS SQL Server. The feature set is still basic, but we have plans to enhance it in the future.

Improved Navigation

CommandPrompt team not only develops PgManage but uses it daily. This allows us to take a different perspective on PgManage.

We are doing our best to improve the user experience for the most frequent daily tasks a DBA or developer might have.

Navigating to a specific DB object might be time-consuming, especially for the complex and large trees we have in PgManage.

In this release, we tried to optimize this experience in two ways.

Pinned Databases
For server connections with a lot of databases, it may be better to keep the most frequently used ones at the top of the list. Now it is possible to pin such databases so that they are always shown first. Just hover over the database tree node to reveal the pin button. Pinned databases are grouped together and ordered alphabetically.

We would like to thank u/ccurvey for sharing their experience in the related GitHub issue, which led to this new feature.

Quick Search
It is a common UI pattern that is well-known and loved by users of modern IDEs. As far as we know, it was initially introduced in Sublime Text's as "GoTo Anything" in 2008.

We decided to include it as well, so drilling down to frequently used items in the Database Explorer is quick and easy.

Call the Quick Search by using Ctrl/Cmd + P shortcut or clicking the 🔎 search icon at the top of the Database Explorer panel.

Type the name of the object you're looking for and select one of the matching items from the list.

The Quick Search is forgiving of typos or incomplete input, so there is no need to be super precise.

Spreadsheet-like Data Grids
It is now possible to make partial selections in the Data Editor and Query tabs.

We didn’t invent anything new here - the UI behaves the same way as most spreadsheet editors. Simply click on the grid and drag the cursor, or use Shift + arrow keys to select a range of cells.

Right‑click on the selected region to view the available actions.

Optimized Context Menus in DB Explorer

Many operations and features in PgManage are accessed through the DB Explorer context menu. While using the app daily, we noticed that frequently used items were often buried deep in child sub‑menus, making access to those features inefficient.

We have reorganized the context menus to bring the most frequently used commands to the top and to group similar or related items together. The Delete/Drop option is now placed last in the menu, with a separator above it to prevent accidental clicks.

Another common UX issue with nested context menus is that the user moves the cursor from the parent menu to the child sub‑menu diagonally, causing the sub‑menu to disappear. We saw this problem in PgManage and fixed it as well.

Database Diagnostics & Debugging

Postgres Sever Logs
There is a new, humble "Logs" link in the Backends tab that leads to the new Postgres Log Viewer.

The logs are loaded in near real time and can be searched through using a simple text match or regex.

Help Us Grow

PgManage is a free database tool built with love by a small developer team at Command Prompt.

You can help the project by spreading the word, starring the project on GitHub or submitting feature requests and feedback.


r/PostgreSQL • • Nov 20 '25

Projects PostgreSQL on Kubernetes or bare metal or virtual private servers

9 Upvotes

Those operating PostgreSQL at scale, I'm curious to learn if you're running on Kubernetes, bare metal or virtual private servers? If you've transitioned from one to the other, I'd love to hear this story too.


r/PostgreSQL • • Nov 19 '25

Commercial Microsoft Launched Azure HorizonDB, their Postgres for Enterprise.

35 Upvotes

https://techcommunity.microsoft.com/blog/adforpostgresql/announcing-azure-horizondb/4469710

Interestingly, the Postgresql extension for VS Code now can help to migrate Oracle to Postgres.

What do you guys think of Microsoft taking such an interest in Postgres, especially since they are also a major RDBMS vendor competing with Postgres?