PostgreSQL: out of memory on Linux

43 views Asked by At

I’m working on a college project. The project is an image editor that allows you to create multiple work spaces and, within them, upload images and perform operations on them (GitHub repo for more information). I find the problem when creating a workspace. The issue is with the function that create a project. Here the function (sorry for some strings in Italian):

int addProject(char *name, char *path, char *comp, char *TPP, char *TUP, char *modEx, uint TTS, bool Backup)
{
    PGconn *conn = PQconnectdb("host=localhost dbname=edix user=edix password=");
    if (PQstatus(conn) != CONNECTION_OK)
    {
        PQfinish(conn);
        handle_error("Errore di connessione a postgres: %s\n", PQerrorMessage(conn));
    }

    char query[1024];
    sprintf(query, "BEGIN;\n"
                   "DO $$\n"
                   "DECLARE\n"
                   "settingsId INT;\n"
                   "BEGIN\n"
                   "INSERT INTO Project (Name, CDate, MDate, Path, Settings) VALUES ('%s', NOW(), NOW(), '%s', -1);\n"
                   "INSERT INTO Settings (TUP, Mode, Comp, TTS, TPP, Backup, Project) VALUES ('%s', '%s', '%s', %d, '%s', %s, '%s') RETURNING Id INTO settingsId;\n"
                   "UPDATE Project SET Settings = settingsId WHERE Name = '%s';\n"
                   "END $$;\n"
                   "COMMIT;\n", name, path, TUP, modEx, comp, TTS, TPP, Backup ? "TRUE" : "FALSE", name, name);
    D_PRINT("Adding project to Postgres...\n");
    PGresult *res = PQexec(conn, query);

    if (PQresultStatus(res) != PGRES_COMMAND_OK)
    {
        const char *sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);

        if (strcmp(sqlstate, "23505") == 0)
            fprintf(stderr, RED "POSTGRES Error:" RESET " Questo progetto già esiste!\n");
        else
            fprintf(stderr, "Errore nell'esecuzione della query: %s\n", PQerrorMessage(conn));


        PQclear(res);
        PQfinish(conn);
        return 1;
    }


    PQclear(res);
    PQfinish(conn);
    return 0;
}

The crash happens, because conn == CONNECTION_BAD

I don’t understand why does not happen the connection, the user and the db edix are created at the first start of the program and I have no problems with connecting to PostgreSQL. I searched a bit online and they say it could be some problem related to how memory is handled by the VM (I'm using a Parallel VM on Mac M1 with Kali Linux 2023.2 ARM64) or Postgres configuration. As for the configuration, I checked its files. And it looks like the values are right. I don't know if you need the configuration files. It's a lot of rows, so if you need I will share them.

0

There are 0 answers