diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php
index a0a9de8..b56bf80 100644
--- a/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php
+++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Upsert.php
@@ -14,6 +14,8 @@
  */
 class Upsert extends QueryUpsert {
 
+  protected $tempTableMap = [];
+
   /**
    * {@inheritdoc}
    */
@@ -43,8 +45,16 @@ public function execute() {
       // are trying to upsert in. This results in the following query:
       //
       // CREATE TEMP TABLE temp_table AS SELECT * FROM table_name LIMIT 0;
-      $query = 'SELECT * FROM {' . $table . '} LIMIT 0';
-      $temp_table = $this->connection->queryTemporary($query, [], $this->queryOptions);
+      if (!isset($this->tempTableMap[$table])) {
+        $query = 'SELECT * FROM {' . $table . '} LIMIT 0';
+        $temp_table = $this->connection->queryTemporary($query, [], $this->queryOptions);
+
+        $this->tempTableMap[$table] = $temp_table;
+      }
+      else {
+        $temp_table = $this->tempTableMap[$table];
+        $this->connection->query('TRUNCATE {' . $temp_table . '} RESTART IDENTITY;', [], $this->queryOptions);
+      }
 
       // Second, insert the data in the temporary table.
       $insert = $this->connection->insert($temp_table, $this->queryOptions)
