diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index f7e83d8ef8..8634c14afc 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -448,6 +448,10 @@ jobs:
                 include:
                     - php: '7.4'
                       db: "postgres"
+                    - php: '8.0'
+                      db: "postgres"
+                    - php: '8.1'
+                      db: "postgres"
 
         name: Windows - PHP ${{ matrix.php }} - ${{ matrix.db }}
 
@@ -523,6 +527,8 @@ jobs:
                   Set-ACL -Path "${env:TEMP_DIR}" -ACLObject $acl
                   cd ${env:GITHUB_WORKSPACE}\phpBB
                   php ..\composer.phar install
+                  php ..\composer.phar remove phpunit/dbunit --dev --update-with-dependencies
+                  php ..\composer.phar require symfony/yaml:~4.4 misantron/dbunit:~5.0 phpunit/phpunit:^9.3 --dev --update-with-all-dependencies --ignore-platform-reqs
                   cd ..
             - name: Setup database
               run: |
diff --git a/phpBB/phpbb/db/driver/postgres.php b/phpBB/phpbb/db/driver/postgres.php
index 7541d1e6b6..3ee4b2b00e 100644
--- a/phpBB/phpbb/db/driver/postgres.php
+++ b/phpBB/phpbb/db/driver/postgres.php
@@ -207,14 +207,16 @@ class postgres extends \phpbb\db\driver\driver
 					return false;
 				}
 
+				$safe_query_id = $this->clean_query_id($this->query_result);
+
 				if ($cache && $cache_ttl)
 				{
-					$this->open_queries[(int) $this->query_result] = $this->query_result;
+					$this->open_queries[$safe_query_id] = $this->query_result;
 					$this->query_result = $cache->sql_save($this, $query, $this->query_result, $cache_ttl);
 				}
 				else if (strpos($query, 'SELECT') === 0)
 				{
-					$this->open_queries[(int) $this->query_result] = $this->query_result;
+					$this->open_queries[$safe_query_id] = $this->query_result;
 				}
 			}
 			else if ($this->debug_sql_explain)
@@ -555,6 +557,15 @@ class postgres extends \phpbb\db\driver\driver
 	 */
 	private function clean_query_id($query_id)
 	{
-		return is_resource($query_id) ? (int) $query_id : $query_id;
+		// As of PHP 8.1 PgSQL functions accept/return \PgSQL\* objects instead of "pgsql *" resources
+		// Attempting to cast object to int will throw error, hence correctly handle all cases
+		if (is_resource($query_id))
+		{
+			return function_exists('get_resource_id') ? get_resource_id($query_id) : (int) $query_id;
+		}
+		else
+		{
+			return is_object($query_id) ? spl_object_id($query_id) : $query_id;
+		}
 	}
 }