Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kQcYi-0000ef-IA for pgsql-sql@arkaria.postgresql.org; Thu, 08 Oct 2020 20:30:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kQcYh-0003A8-Fk for pgsql-sql@arkaria.postgresql.org; Thu, 08 Oct 2020 20:30:39 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kQcYh-000381-2c for pgsql-sql@lists.postgresql.org; Thu, 08 Oct 2020 20:30:39 +0000 Received: from mail-pf1-x442.google.com ([2607:f8b0:4864:20::442]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kQcYc-0008JM-3N for pgsql-sql@lists.postgresql.org; Thu, 08 Oct 2020 20:30:38 +0000 Received: by mail-pf1-x442.google.com with SMTP id a200so4921555pfa.10 for ; Thu, 08 Oct 2020 13:30:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=KWc6qVFvDvp7k0ptPbndhFWyBlUy3N3hIDcbA1/Iktk=; b=o6iZ/YngVQkRI49zbWcQ70ZKPiCmDFNMV/7i9RnvvboRCwYdPYTBif86v19ANxcZQ5 YdhL/wS7p5udg9yb6UK+4nIHEeFEjA9jwTwCgEUJcTSP3+9s3rEoWidZ9iNtfpR43r1U agundlV4qcq7/L82wVXUqieQ9vizMjAS2XToPOn8BCyZo/krobj+yraBOqobsRorWMpN UuIGCHo3dQfr/d/rAVVqCa/BmJaJ1wmkA1DQZflclsOZBBT5BOlbRUURaSfj8BnY1/j7 DmDivDt+URqAAGSYGZGu0Ahu7jXF6hr0Hg148YYjr6+4xyf1HQ6OhL0cqW5JsiuiODch ZI+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=KWc6qVFvDvp7k0ptPbndhFWyBlUy3N3hIDcbA1/Iktk=; b=fgozMNRNzCk3EowIobyFjVH0a9rWyrjJDlW3wq9S1JMsPpntdoNkpz3AT+iR7Dp5GD 4e4K/Su7Wda/9XLQ5KBgQBY0kX9hiMKSRZ/2Rz83tUGiyOxs9GktzhaTaiVfQs/v5Csi SiPjCZSwkmqgPHX3xsiBv4hQ3SpCSXMjuDMvKFcJqfa+dlNodgfDT3YEfQmAlzlqu9PP FtygG7D0lkEBKyeVZdPIQriMRKnUCWKtCn5zaR5PtQ1Z9MAd7ZPPXIaplYG2NvSfCgF9 t30TxHgfxenoQVj06DmZG1hAliK/sZHm9SlGNEd75yWnEIboWCB8SgnSJqBH45INucMD Gbyw== X-Gm-Message-State: AOAM5305rtiCwbbGQlF/OzR+nL+AZuERQLb1L2AwUkaMx2TQw3mdJBHr 6eLN8sajN14k1gYk30peZC0/V+WJEbM= X-Google-Smtp-Source: ABdhPJxVKDNEhBKHkfvdVvRqETL2CrPyN89H0vUUxDuEXoxg5zdI4X04OxFrHgexxraAWEtgpW59jA== X-Received: by 2002:a62:8c85:0:b029:152:546f:c991 with SMTP id m127-20020a628c850000b0290152546fc991mr9456165pfd.1.1602189032072; Thu, 08 Oct 2020 13:30:32 -0700 (PDT) Received: from pavlo-debian ([204.147.87.6]) by smtp.gmail.com with ESMTPSA id t13sm8184089pfc.1.2020.10.08.13.30.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Oct 2020 13:30:31 -0700 (PDT) Message-ID: <124ce8dff8402159f20dec1bc1ac27e26ee39c23.camel@gmail.com> Subject: Re: libpq CREATE DATABASE operation from multiple treads From: p.sun.fun@gmail.com To: pgsql-sql@lists.postgresql.org Date: Thu, 08 Oct 2020 15:30:27 -0500 In-Reply-To: References: <390804abe88e4e9da9049f6e05c841783acaca47.camel@gmail.com> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.36.4-2 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Indeed, this is a FATAL_ERROR. From two threads I got: // Thread #1 FATAL_ERROR: ERROR: source database "template1" is being accessed by other users DETAIL: There is 1 other session using the database. // Thread #2 FATAL_ERROR: ERROR: source database "template1" is being accessed by other users DETAIL: There is 1 other session using the database. On Thu, 2020-10-08 at 23:15 +0300, Dmitry Igrishin wrote: > > > On Thu, 8 Oct 2020, 22:46 , wrote: > > Hi, PostgreSQL community. > > > > I hope everyone is doing great and keep themselves safe and > > healthy. > > > > I am not sure whether my question should belong here. Please advise > > if > > this is the wrong place. > > > > I am contributing to the libgda project ( > > https://gitlab.gnome.org/GNOME/libgda) which a convenient wrapper > > around multiple SQL providers. We have good support for PostgreSQL > > and > > would like to keep so in the future. To test our code we use > > CI+Docker. > > Docker provides SQL server functionality. Everything is fine but... > > > > We have an API to run DDL operations. Everything works ok but once > > in a > > while, we have a problem to create a new database if we run > > multiple > > build processes communicating to the same SQL server. In our code, > > the > > process to create a new database consist of the following steps: > > > > 1) Connect to "template1" database > > 2) Execute "CREATE DATABASE " > > 3) Close connection > > 4) Open a connection to the new database. > > > > I was trying to explore this problem using libpq to better > > understand > > the origin of the problem. This is a test code: > > > > void function_to_run_in_a_thread(void *data) { > > > > /* The passed void *data is a pointer to an SQL string with a > > randomly > > generated database name, e.g. CREATE DATABASE kajdygsj */ > > const char *sql_create_db = (const char *)data; > > const char *cnc_string = "host='localhost' user='test' > > password='test1' dbname='template1'"; > > > > PGconn *cnc = NULL; > > PGresult *res = NULL; > > > > cnc = PQconnectdb(cnc_string); > > > > if (PQstatus(cnc) != CONNECTION_OK) > > abort(); > > > > res = PQexec(cnc, sql_create_db); > > > > if (PQresultStatus(res) != PGRES_COMMAND_OK) > > abort(); > > > > > > PQclear(res); > > PQfinish(cnc); > > } > > > > I run this function using only one thread and everything works > > great. > > If I use two threads, the PQresultStatus(res) != PGRES_COMMAND_OK. > > I > > was trying to add a delay time for PQresultStatus(res) != > > PGRES_COMMAND_OK and repeat the same command but didn't help much. > > If I > > drop connection for PQresultStatus(res) != PGRES_COMMAND_OK and > > open it > > again after some random delay, it works. > > Okay. You may want to check PQresultStatus(). If it's > PGRES_FATAL_ERROR please check the SQLSTATE and message. > > Can the server handle CREATE DATABASE requests in parallel? > > > > Thanks, > > > > -Pavlo > > > > > >