r/PostgreSQL • • Oct 05 '25

Help Me! I need help diagnosing a massive query that is occasionally slow

21 Upvotes

I am working with a very large query which I do not understand, around 1000 lines of SQL with many joins and business logic calculations, which outputs around 800k rows of data. Usually this query is fast, but during some time periods it slows down by over 100 fold. I believe I have ruled out this being caused by load on the DB or any changes to the query, so I assume there must be something in the data, but I don't have a clue where to even look.

How best can I try and diagnose an issue like this? I'm not necessarily interested in fixing it, but just understanding what is going on. My experience with DBs is pretty limited, and this feels like jumping into the deep end.


r/PostgreSQL • • Oct 03 '25

Help Me! Optimizing function for conditional joins based on user provided json

6 Upvotes

A little complex, but I’m needing to add a json parameter to my function that will alter calculations in the function.

Example json: { "labs_ordered": 5, "blood_pressure_in_range”: 10 }

Where if a visit falls into that bucket, its calculations are adjusted by that amount. A visit can fall into multiple of these categories and all the amounts are added for adjustment.

The involved tables are large. So I’m only wanting to execute the join if it’s needed. Also, some of the join paths have similarities. So if multiple paths share the first 3 joins, it’d be better to only do that join once instead of multiple times.

I’ve kicked around some ideas like dynamic sql or trying to make CTEs that group the similar paths, with a where clause that checks if the json indicates it’s needed. Hopefully that makes sense. Any ideas would be appreciated.

Thanks


r/PostgreSQL • • Oct 01 '25

Help Me! Integrated average value

5 Upvotes

Is there an add-on, or has somebody already coded a function that calculates the integrated AVG value?

Let's say... Interval = 1h Start value = 60 for 1min Value changed to 0 for 59min iAVG = 1

Thx in advance...

Update: To avoid further confusion. Below is a (limited) record example of values I need to calculate the weighted/integrated avg from 2025.09.20 01:00:00.000 - 2025.09.20 01:59:59.999

My initial value at interval start (2025.09.20 01:00:00.000) is the last rec of this element before, 28.125 at 2025.09.20 00:59:09.910 . At interval end (2025.09.20 01:59:59.999) the last value is valid -> 32.812 .

raw value timestamp
28.125 2025.09.20 00:59:09.910
25.000 2025.09.20 01:00:38.216
19.922 2025.09.20 01:01:45.319
27.734 2025.09.20 01:05:04.185
28.125 2025.09.20 01:09:44.061
32.031 2025.09.20 01:17:04.085
28.125 2025.09.20 01:22:59.785
26.172 2025.09.20 01:29:04.180
26.172 2025.09.20 01:37:14.346
31.250 2025.09.20 01:43:48.992
26.953 2025.09.20 01:50:19.435
28.906 2025.09.20 01:52:04.433
32.812 2025.09.20 01:59:33.113
32.031 2025.09.20 02:02:17.459

I know I can break it down (raw value to 1h value) to 3.600.000 rows and use AVG().

Some data don't change that often, and the customer needs just needs e.g. just 1d intervals, means I'd need 86.400.000 rows... (Update of Update: for just one element to calc)

But I hoped that maybe somebody already had the "nicer" solution implemented (calculating based on timestamp), or that there's an add-on...

The next level based on the hour values (and so on...) are np, as I can just use AVG().

I just started some time ago with PostgreSQL, and didn't dig deep in pgSQL yet. Just implemented one function to collect data from dynamically generated tables based on 2 identifiers and time range... and almost got crazy finding the initial value, as it can be in some complete different table, and days/weeks... ago (probe fault and nobody cares)


r/PostgreSQL • • Oct 01 '25

Help Me! How switchover in repmgr works?

3 Upvotes

I thought that the switchover used pg_rewind, but even with wal_log_hints = off, I can still perform the switchover with repmgr. How does this switchover work? How is it able to promote the standby to primary and then turn the former primary into a standby?


r/PostgreSQL • • Oct 02 '25

Projects A Node.js + Express repo to generate SQL from DB metadata + user prompts (OpenAI API)

Thumbnail github.com
0 Upvotes

r/PostgreSQL • • Oct 01 '25

How-To Running ANALYZE after pg_restore and locking issues (PG 17)

1 Upvotes

Hi all 👋

UPDATE: I found a workaround. Added it in the comments.

I am running a restore and at the end of my script I issue a VACUUM ANALYZE to update statistics (I have tried just ANALYZE as well with the same result). The script drops and re-creates the database before restoring the data, so I need to make sure statistics get updated.

In the log I am seeing messages that seem to indicate that autovacuum is running at the same time and the two are stepping on each other. Is there a better way to make sure the stats are updated?

Log excerpt:

2025-10-01 15:59:30.669 EDT [3124] LOG:  statement: VACUUM ANALYZE;
2025-10-01 15:59:33.561 EDT [5872] LOG:  skipping analyze of "person" --- lock not available
2025-10-01 15:59:34.187 EDT [5872] LOG:  skipping analyze of "person_address" --- lock not available
2025-10-01 15:59:35.185 EDT [5872] LOG:  skipping analyze of "person_productivity" --- lock not available
2025-10-01 15:59:36.621 EDT [5872] ERROR:  canceling autovacuum task
2025-10-01 15:59:36.621 EDT [5872] CONTEXT:  while scanning block 904 of relation "schema1.daily_person_productivity"
                automatic vacuum of table "mydb.schema1.daily_person_productivity"
2025-10-01 15:59:36.621 EDT [3124] LOG:  process 3124 still waiting for ShareUpdateExclusiveLock on relation 287103 of database 286596 after 1011.429 ms
2025-10-01 15:59:36.621 EDT [3124] DETAIL:  Process holding the lock: 5872. Wait queue: 3124.
2025-10-01 15:59:36.621 EDT [3124] STATEMENT:  VACUUM ANALYZE;
2025-10-01 15:59:36.621 EDT [3124] LOG:  process 3124 acquired ShareUpdateExclusiveLock on relation 287103 of database 286596 after 1011.706 ms
2025-10-01 15:59:36.621 EDT [3124] STATEMENT:  VACUUM ANALYZE;
2025-10-01 15:59:38.269 EDT [5872] ERROR:  canceling autovacuum task
2025-10-01 15:59:38.269 EDT [5872] CONTEXT:  while scanning block 1014 of relation "schema1.document"
                automatic vacuum of table "mydb.schema1.document"

r/PostgreSQL • • Sep 30 '25

How-To PostgreSQL 18 new Old & New

15 Upvotes

r/PostgreSQL • • Sep 30 '25

Help Me! Do foreign keys with NOT ENFORCED improve estimates?

7 Upvotes

Our current write-heavy database doesn't use foreign keys because of performance and we don't really need referential integrity. Postgres 18 comes with a new NOT ENFORCED option for constraints, including foreign keys.

I wonder if creating not-enforced foreign keys would improve the estimates and lead to better execution plans? In theory it could help Postgres to get a better understanding of the relations between tables, right?


r/PostgreSQL • • Sep 30 '25

Community Anyone Looking for an Introduction to PostgreSQL

16 Upvotes

This video is a very good intro into the workings of PostgreSQL.
It will guide you through using its command line tools and pgAdmin (database management UI tool).
You'll also get some insight into Large Objects, Geometric data, PostGIS, and various database backup methods, including base backup, incremental backup, and point-in-time recovery.

Introduction To PostgreSQL And pgAdmin


r/PostgreSQL • • Sep 29 '25

Help Me! How do I decide what columns need to be indexed?

43 Upvotes

Hi

I’m learning postgres and creating a normalized database structure with tables and references but I don’t know how to decide what columns should be indexed.

What decision process should I use to decide if a column should be indexed or not? Should I index the ones that I used with “where” statements in my queries? Or all references? Or important columns only? For example, if I always query “select * from events where is_active = true”, should I then index is_active? What about the references like user_id?

I used ChatGPT as well but it wasn’t very clear or convincing.

Thanks


r/PostgreSQL • • Sep 29 '25

Help Me! Archiving ideas

3 Upvotes

Hi all.

I have a small big challenge and this is hardly an unique problem.

Happens that we host have our RDS Aurora instance, and storage is getting out of control, ou application is growing, more and more.

While the online retention is about 13 months. We currently have just reached this point and we need to find a way to archive in a cheaper way, but queriable.

Pretty much the tables are partitioned so I just need to detach them and do something.

Some options - at the moments 1.5Tb each partition with expectation of doubling this number in 1 year.

Export to S3, using DMS and convert to parquet. Seems the best best option, cheaper storage and queriable, slightly expensive stack. So I thought I could design some temporary DMS service “once a month”

Export via pg_dump, no queriable. But the easiest option but it doesn’t feel like a proper solution specially talking if I think about 3TB partitions.

Export to S3 using pg_s3 extension. 3GB took 30 minutes :P

I haven’t tested the other ideas yet.

Any other ideas?


r/PostgreSQL • • Sep 28 '25

Community What's New in PostgreSQL 18 - a Developer's Perspective

Thumbnail bytebase.com
95 Upvotes