'Check if pg_prepare was already executed

Is there a way to check if pg_prepare already executed and remove it from the session?

Seems like pg_close doesn't remove prepared statement from the session. Kind of seems like a bug in php, but maybe I'm missing something or maybe there is a workaround.

public static function readSubdomains($dcName, $filter = null) {
// ...
        $conn = pg_pconnect($connectionString);
// ...

        $result = pg_prepare($conn, "subdomains", "SELECT subdomain
            from tenants
            where $where
            order by 1 asc
        ");
        $result = pg_execute($conn, "subdomains", $params);

// ...

        pg_close($conn);
}

Second call to readSubdomains shows a warning like this:

Warning: pg_prepare(): Query failed: ERROR: prepared expression "subdomains" already exists in inc/TestHelper.php on line 121


Solution 1:[1]

Always check the official manuals for this sort of stuff.

https://www.postgresql.org/docs/current/view-pg-prepared-statements.html

Oh - if pg_close isn't dropping prepared statements then it isn't closing the connection. You might have some connection pooling involved.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Richard Huxton