DEV Community

Cover image for PostgreSQL temp files usage

PostgreSQL temp files usage

Bolaji Wahab on January 22, 2022

Temporary files Certain query operations such as sort or hash table require some memory facility. This memory is provided by a runtime c...
Collapse
 
dineshparva profile image
dinesh reddy

path ~ ('PG_' || substring(current_setting('server_version') FROM '^(?:\d.\d\d?|\d+)'))

what exactly is this part doing in the query especially FROM '^(?:\d.\d\d?|\d+)')??? query is failing with msg "ERROR: absolute path not allowed"

Collapse
 
bolajiwahab profile image
Bolaji Wahab • Edited

For this part of the query

path ~ ('PG_' || substring(current_setting('server_version') FROM '^(?:\d.\d\d?|\d+)'))
Enter fullscreen mode Exit fullscreen mode

When you create a tablespace, a directory is created in format PG_server_version_some_other_digit.
We check all tablespaces except pg_global for temp files.

For the error "ERROR: absolute path not allowed".

I cannot figure where the error seems to be coming from currently but it seems to be related to tablespace setup.
Are you using tablespaces? And if so, can you confirm the setup of the tablespaces?

Thank you.

Collapse
 
dineshparva profile image
dinesh reddy

Image description

Thread Thread
 
dineshparva profile image
dinesh reddy

Anything wrong with the tablespace setup

Thread Thread
 
bolajiwahab profile image
Bolaji Wahab

Can you try break down the query? That way it would be easier to debug.
The first part

WITH tablespaces AS (
    SELECT
        spcname AS tbl_name,
        coalesce(nullif(pg_tablespace_location(oid), ''), (current_setting('data_directory') || '/base')) AS tbl_location
    FROM pg_tablespace
),
tablespace_suffix AS (
    SELECT
        tbl_name,
        tbl_location || '/pgsql_tmp' AS path
    FROM tablespaces
    WHERE tbl_name = 'pg_default'
    UNION ALL
    SELECT
        tbl_name,
        tbl_location || '/' || path || '/pgsql_tmp'
    FROM tablespaces, LATERAL pg_ls_dir(tbl_location) AS path
    WHERE path ~ ('PG_' || substring(current_setting('server_version') FROM '^(?:\d\.\d\d?|\d+)'))
)
SELECT * FROM tablespace_suffix;
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
bolajiwahab profile image
Bolaji Wahab

Btw, which version of PostgreSQL is it and is it native PostgreSQL?

Collapse
 
jtorral profile image
JT

One simple solution if resource are available is to create a ram based file system and use that for your temp file destination.

Collapse
 
stephen_price profile image
Stephen Price

You mentioned that you avoided using "pg_ls_tmpdir" in the query since it's new as of PG12, but the query is using it. Is there an earlier version of the query that does not use it?
I ask because I'm trying to wrap my head around which queries are eating up my temp space, and due to the fact that I'm running Aurora PostgreSQL in AWS RDS, I cannot use "pg_ls_tmpdir" (the "rds_superuser" permissions explicitly deny it).

Collapse
 
bolajiwahab profile image
Bolaji Wahab • Edited

I started working with RDS recently but I didn't check out this issue until now. So using pg_ls_tmpdir is indeed going to bypass the permission issue. I have now added a query which can be used on RDS, hopefully you find it useful. Cheers.

Collapse
 
bolajiwahab profile image
Bolaji Wahab • Edited

Hi, I believe you mean pg_ls_dir and pg_stat_file? Both functions are restricted to only superusers by default but execute can be granted to any user by a superuser.
I am not totally conversant with rds_superuser so you might not be able to call these functions.

Collapse
 
kmohsoe profile image
KMohsoe

If the temporary file size is coming greater, please could you tell me how could I delete the temporary file in postgreSQL with the command?

Image description

Collapse
 
bolajiwahab profile image
Bolaji Wahab • Edited

Temporary files are cleaned up automatically once the queries using them are done processing or canceled. Deleting an in-use temporary files can lead to backend crashes or even server crashes.
Why do you want to delete the files? Are they stale/orphaned? You should only have orphaned/stale temporary files when there are crashes.