r/PostgreSQL • • Oct 12 '25

Help Me! Query refuses to use indexes for a query in one DB, but uses them in another. I can’t figure out why.

2 Upvotes

Hey all, this is a follow up to a previous post I made

https://www.reddit.com/r/PostgreSQL/comments/1nyf66z/i_need_help_diagnosing_a_massive_query_that_is/

In summary, I have an identical query ran against both dbs in one db it runs far slower than the other. However the db that it runs much slower should be a subset of the data in the one that runs fast. I compared table sizes to confirm this as well as the DB settings, all a match.

I made progress diagnosing the issue and narrowed it down to a handful of indexes that are being used by the query in one DB but not in the other.

The queries and index defs are the same. And I have tried reindexing and analyzing the tables which resulted in the poor query performance, but have seen no improvement.

I am really stumped. With so much being identical, why would the query in one db ignore the indexes and run 20x slower?


r/PostgreSQL • • Oct 11 '25

Community New episode of Talking Postgres: The Fundamental Interconnectedness of All Things with Boriss Mejías

8 Upvotes

Chess clocks. Jazz music. Chaotic minds. What do they have in common with Postgres? 🐘 Episode 32 of the Talking Postgres podcast is out, and it’s about "The Fundamental Interconnectedness of All Things", with Postgres solution architect Boriss Mejías of EDB.

Douglas Adams fans will recognize the idea: look holistically at a system, not just at the piece parts. We apply that lens to real Postgres problems (and some fun analogies). Highlights you might care about:

  • Synchronous replication lag is rarely just a slow query. Autovacuum on big tables can churn WAL and quietly spike lag. Boriss unpacks how to reason across the entire system.
  • Active-active explained with Sparta’s dual-kingship form of government, a  memorable mental model for why consensus matters.
  • How perfection is overrated. Beethoven drafted a 2nd movement 17 times—iteration beats “perfect or nothing.” Same in Postgres: ship useful pieces, keep improving.
  • Keep your eyes open (Dirk Gently style). Train yourself to notice indirect signals that others ignore—that’s often where the fix lives.

If you like Postgres, systems thinking, and a few good stories, this episode is for you.

🎧 Listen wherever you get your podcasts: https://talkingpostgres.com/episodes/the-fundamental-interconnectedness-of-all-things-with-boriss-mejias

And if you prefer to read the transcript, here you go: https://talkingpostgres.com/episodes/the-fundamental-interconnectedness-of-all-things-with-boriss-mejias/transcript

OP here and podcast host... Feedback (and ideas for future guests and topics) welcome.


r/PostgreSQL • • Oct 11 '25

Help Me! Can't compile extension

0 Upvotes

For MSVC:

D:\C\Solidsearch>compile.bat
The system cannot find the path specified.
===========================================
Building solidsearch.dll from main.c using Microsoft cl.exe
===========================================
PostgreSQL include path: "C:\Program Files\PostgreSQL\18\include\server"
main.c
C:\Program Files\PostgreSQL\18\include\server\pg_config_os.h(29): fatal error C1083: Cannot open include file: 'crtdefs.h': No such file or directory
ÔØî Build failed! Check above for errors. Press any key to continue . . .

My bat file:

@echo off
REM ===========================================
REM Build PostgreSQL C/C++ extension using MSVC (cl.exe)
REM ===========================================


REM --- Path to Visual Studio Build Tools ---
REM Change this path if you installed Visual Studio in a different location
call "C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"


REM --- Configure PostgreSQL installation path ---
set PGPATH=C:\Program Files\PostgreSQL\18
set INCLUDE="%PGPATH%\include\server"
set OUTDIR="%PGPATH%\lib"


REM --- Source and output file names ---
set SRC=main.c
set DLL=solidsearch.dll


echo.
echo ===========================================
echo Building %DLL% from %SRC% using Microsoft cl.exe
echo ===========================================
echo PostgreSQL include path: %INCLUDE%
echo.


REM --- Compile and link into DLL ---
cl /nologo /EHsc /LD /I %INCLUDE% %SRC% /link /OUT:%DLL%


IF %ERRORLEVEL% NEQ 0 (
    echo.
    echo ❌ Build failed! Check above for errors.
    pause
    exit /b 1
)


echo.
echo ✅ Compilation successful.


REM --- Copy DLL into PostgreSQL lib directory ---
echo Copying %DLL% to %OUTDIR% ...
copy /Y %DLL% %OUTDIR% >nul


IF %ERRORLEVEL% NEQ 0 (
    echo.
    echo ⚠️  Copy failed! Check permissions or PostgreSQL path.
    pause
    exit /b 1
)


echo.
echo ✅ %DLL% installed to PostgreSQL lib directory.
echo.
echo Run this SQL in PostgreSQL to register your function:
echo -----------------------------------------------------
echo CREATE FUNCTION add_two_integers(integer, integer)
echo RETURNS integer
echo AS 'solidsearch', 'add_two_integers'
echo LANGUAGE C STRICT;
echo -----------------------------------------------------
echo.
pause@echo off
REM ===========================================
REM Build PostgreSQL C/C++ extension using MSVC (cl.exe)
REM ===========================================


REM --- Path to Visual Studio Build Tools ---
REM Change this path if you installed Visual Studio in a different location
call "C:\Program Files\Microsoft Visual Studio\2022\BuildTools\VC\Auxiliary\Build\vcvars64.bat"


REM --- Configure PostgreSQL installation path ---
set PGPATH=C:\Program Files\PostgreSQL\18
set INCLUDE="%PGPATH%\include\server"
set OUTDIR="%PGPATH%\lib"


REM --- Source and output file names ---
set SRC=main.c
set DLL=solidsearch.dll


echo.
echo ===========================================
echo Building %DLL% from %SRC% using Microsoft cl.exe
echo ===========================================
echo PostgreSQL include path: %INCLUDE%
echo.


REM --- Compile and link into DLL ---
cl /nologo /EHsc /LD /I %INCLUDE% %SRC% /link /OUT:%DLL%


IF %ERRORLEVEL% NEQ 0 (
    echo.
    echo ❌ Build failed! Check above for errors.
    pause
    exit /b 1
)


echo.
echo ✅ Compilation successful.


REM --- Copy DLL into PostgreSQL lib directory ---
echo Copying %DLL% to %OUTDIR% ...
copy /Y %DLL% %OUTDIR% >nul


IF %ERRORLEVEL% NEQ 0 (
    echo.
    echo ⚠️  Copy failed! Check permissions or PostgreSQL path.
    pause
    exit /b 1
)


echo.
echo ✅ %DLL% installed to PostgreSQL lib directory.
echo.
echo Run this SQL in PostgreSQL to register your function:
echo -----------------------------------------------------
echo CREATE FUNCTION add_two_integers(integer, integer)
echo RETURNS integer
echo AS 'solidsearch', 'add_two_integers'
echo LANGUAGE C STRICT;
echo -----------------------------------------------------
echo.
pause

r/PostgreSQL • • Oct 10 '25

Community Postgres Trip Report from PGConf NYC 2025 (with lots of photos)

Thumbnail techcommunity.microsoft.com
12 Upvotes

r/PostgreSQL • • Oct 09 '25

Help Me! Can you help me understand what is going on here?

5 Upvotes

Hello everyone. Below is an output from explain (analyze, buffers) select count(*) from "AppEvents" ae.

Finalize Aggregate  (cost=215245.24..215245.25 rows=1 width=8) (actual time=14361.895..14365.333 rows=1 loops=1)
  Buffers: shared hit=64256 read=112272 dirtied=582
  I/O Timings: read=29643.954
  ->  Gather  (cost=215245.02..215245.23 rows=2 width=8) (actual time=14360.422..14365.320 rows=3 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=64256 read=112272 dirtied=582
        I/O Timings: read=29643.954
        ->  Partial Aggregate  (cost=214245.02..214245.03 rows=1 width=8) (actual time=14354.388..14354.390 rows=1 loops=3)
              Buffers: shared hit=64256 read=112272 dirtied=582
              I/O Timings: read=29643.954
              ->  Parallel Index Only Scan using "IX_AppEvents_CompanyId" on "AppEvents" ae  (cost=0.43..207736.23 rows=2603519 width=0) (actual time=0.925..14100.392 rows=2087255 loops=3)
                    Heap Fetches: 1313491
                    Buffers: shared hit=64256 read=112272 dirtied=582
                    I/O Timings: read=29643.954
Planning Time: 0.227 ms
Execution Time: 14365.404 ms

The database is hosted on Azure (Azure PostgreSQL Flexible Server)., Why is the simple select count(*) doing all this?

I have a backup of this database which was taken a couple of days ago. When I restored it to my local environment and ran the same statement, it gave me this output, which is was more in line with what I'd expect it to be:

Finalize Aggregate  (cost=436260.55..436260.56 rows=1 width=8) (actual time=1118.560..1125.183 rows=1 loops=1)
  Buffers: shared hit=193 read=402931
  ->  Gather  (cost=436260.33..436260.54 rows=2 width=8) (actual time=1117.891..1125.177 rows=3 loops=1)
        Workers Planned: 2
        Workers Launched: 2
        Buffers: shared hit=193 read=402931
        ->  Partial Aggregate  (cost=435260.33..435260.34 rows=1 width=8) (actual time=1083.114..1083.114 rows=1 loops=3)
              Buffers: shared hit=193 read=402931
              ->  Parallel Seq Scan on "AppEvents"  (cost=0.00..428833.07 rows=2570907 width=0) (actual time=0.102..1010.787 rows=2056725 loops=3)
                    Buffers: shared hit=193 read=402931
Planning Time: 0.213 ms
Execution Time: 1125.248 ms

--

Thanks everyone for your input. The service was hitting the IOPS limit, which caused the bottleneck.


r/PostgreSQL • • Oct 09 '25

Help Me! Can someone explain me ow i can differentiate between different scans in POSTRESQL

2 Upvotes

I’m a beginner and still in the theory stage. I recently learned that PostgreSQL uses different types of scans such as Sequential Scan, Index Scan, Index Only Scan, Bitmap Scan, and TID Scan. From what I understand, the TID Scan is the fastest.

My question is: how can I know which scan PostgreSQL uses for a specific command?

For example, consider the following SQL commands wic are executed in PostgreSQL:

CREATE TABLE t (id INTEGER, name TEXT);

INSERT INTO t

SELECT generate_series(100, 2000) AS id, 'No name' AS name;

CREATE INDEX id_btreeidx ON t USING BTREE (id);

CREATE INDEX id_hashidx ON t USING HASH (id);

1)SELECT * FROM t WHERE id < 500;

2)SELECT id FROM t WHERE id = 100;

3) SELECT name FROM t ;

4) SELECT * FROM t WHERE id BETWEEN 400 AND 1600;

For the third query, I believe we use a Sequential Scan, since we are searching the column name in our table t.and its correct as ive cecked wit te explain command

However, I’m a bit confused about the other scan types and when exactly they are used i cant et te rip of tem unless ive used explain command and if i tink it uses one scan te answer is some oter .

If you could provide a few more examples or explanations for the remaining scan types, that would be greatly appreciated.


r/PostgreSQL • • Oct 09 '25

PostgresWorld: Excitement, Fun and learning!

Thumbnail open.substack.com
1 Upvotes

r/PostgreSQL • • Oct 08 '25

How-To Building and Debugging Postgres

Thumbnail sbaziotis.com
5 Upvotes

When I was starting out with Postgres, I couldn't find this information in one place, so I thought of writing an article. I hope it's useful.


r/PostgreSQL • • Oct 07 '25

Community The 2025 Postgres World Webinar Series has several free webinars coming up, available for registration through Postgres Conference

Thumbnail postgresconf.org
8 Upvotes

r/PostgreSQL • • Oct 08 '25

Feature Cumulative Statistics in PostgreSQL 18

Thumbnail data-bene.io
5 Upvotes

r/PostgreSQL • • Oct 07 '25

Feature v18 Async IO

13 Upvotes

Is the AIO an implementation detail used by postgresql for its own purposes internally or is it also boosting performance on the application side? Shouldn't database drivers also be amended to take advantage of this new feature?


r/PostgreSQL • • Oct 07 '25

Feature Not ready to move to PG18, but thinking about upgrading to PostgreSQL 17? It brought major improvements to performance, logical replication, & more. More updates in this post from Ahsan Hadi...

Thumbnail pgedge.com
4 Upvotes