After upgrading PG from 11 to 15, I struggeling with the following situation:
old (working) Setup: Postgres 11 PostGIS 2.5
Setup: Postgres 15 PostGIS 3.3
table_1 (~14.000.000 rows)
When I do select count(*) from table_1 it never seems to be finished.
When I do select * into table_2 from table_1 and than select count(*) from table_2, it would finished in ~15 minutes.
The concrete statements are:
-- this never stops
select count(*)
FROM table_1 t1
INNER JOIN table_1 t2 ON (ST_Equals(t1.wkb_geometry, t2.wkb_geometry))
WHERE t1.gid > t2.gid
;
-- this works
select *
into table_2
from table_1
;
select count(*)
FROM table_2 t1
INNER JOIN table_2 t2 ON (ST_Equals(t1.wkb_geometry, t2.wkb_geometry))
WHERE t1.gid > t2.gid
;
In both cases there is an index created on wkb_geometry, like this:
CREATE INDEX table_1_geom ON table_1
USING gist (wkb_geometry)
TABLESPACE pgdata;
I already recreated the index an vacuum the table, but nothing worked for me.
Update: I figured out, that the problem is located at the pkey for column gid. After the select into... table_2 has no pkey on gid. If i remove that pkey on table_1 it would work, too. So what happend by adding a pkey different in PG version 15? Any ideas?
Update 2: DDL/SQL for table_1
SELECT ST_MakeLine(sp,ep) AS wkb_geometry,
ogc_fid,
...
INTO table_1
FROM
(SELECT
ST_PointN(geom, generate_series(1, ST_NPoints(geom)-1)) as sp,
ST_PointN(geom, generate_series(2, ST_NPoints(geom) )) as ep,
ogc_fid,
...
FROM
(SELECT (ST_Dump(ST_Boundary(wkb_geometry))).geom,
ogc_fid,
...
FROM another_table
) AS linestrings
) AS segments;
ALTER TABLE table_1 ADD COLUMN gid serial NOT NULL;
--ALTER TABLE table_1 ADD CONSTRAINT table_1_pk PRIMARY KEY (gid);
CREATE INDEX table_1_geom ON table_1 USING gist (wkb_geometry) TABLESPACE pgdata;
explainfor this query? (usually I would ask for all the details usinganalyze, verbose, buffers, settings, but when it takes too long, lets start with just explain) And the complete DDL for both tables and all their indexes. All in plain text, as an update of your question.