Functions / PostGIS / ST_MemSize
PostGISGeometry Accessors

ST_MemSize

What is ST_MemSize?

ST_MemSize is a PostGIS function that returns the total in-memory byte size of a geometry value, including headers, cached bounding box, and all vertex data.

SQL
ST_MemSize(geometry g)integer

Useful for diagnostics and storage analysis. The value reflects actual in-memory representation, which is close to but not identical to on-disk size (TOAST compression affects the latter).

When would you use ST_MemSize?

Use ST_MemSize to identify oversized geometries that dominate storage, slow down queries, or blow past TOAST thresholds:

SQL
1SELECT id, ST_MemSize(geom) AS bytes, ST_NPoints(geom) AS pts
2FROM features
3ORDER BY bytes DESC
4LIMIT 20;

High byte counts on single rows often point to under-simplified imports from high-precision sources. Pair with ST_Simplify or ST_SimplifyPreserveTopology to reduce vertex counts while keeping visual fidelity.

FAQs

Does ST_MemSize match on-disk size?

Not exactly. PostgreSQL TOAST may compress large geometries on disk; ST_MemSize reports the uncompressed in-memory form. For disk metrics use pg_column_size on the TOASTed column.

How do I find the biggest geometries in my table?

SELECT id FROM t ORDER BY ST_MemSize(geom) DESC LIMIT 50; — but this scans the whole table. For tables too large to full-scan, maintain a computed bytes column via trigger.

Is this the same as pg_column_size?

Similar but not identical. pg_column_size returns the size of the column value as stored (post-TOAST); ST_MemSize returns the unpacked size including the cached bbox header.

How do I reduce geometry size?

ST_Simplify for controlled vertex reduction, ST_SnapToGrid for precision clamping, or switch to a smaller SRID-appropriate column type. For visualization-only data, ST_SimplifyPreserveTopology avoids breaking adjacency.