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 1kLc1T-0000SN-1f for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2020 00:55:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kLc1S-00046r-0H for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Sep 2020 00:55:38 +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 1kLc1R-00046j-JS for pgsql-hackers@lists.postgresql.org; Fri, 25 Sep 2020 00:55:37 +0000 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kLc1P-0003as-3t for pgsql-hackers@postgresql.org; Fri, 25 Sep 2020 00:55:36 +0000 Received: by mail-il1-x133.google.com with SMTP id a19so763367ilq.10 for ; Thu, 24 Sep 2020 17:55:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=VSu1/mbLML5yHnH1BATDOwKL/6/pp8snOnc9YCpJ6/U=; b=UxZuwPy+WMwKMkcnXjYTC4oYlqQmTfz3fe9G33h5xg5wrIjYbXsAljSFcMmGrdt+lz skPhl4S+xgDICrBZGmY4dngug/OMaPacXmloibvjxp35rAVFabOzNYxDSf9LY0cq3Eto BnXiHcMfYPXS5Ui4GpUtBVsUyeLC2icqvA7j+PHDtWAsZUiHB3TmZu5PIFBtbtzGZuuZ v+fQ7ByXI9J3ucxWEajg2V/6IExPLISt/VQbfE99ueJXYB6EOM3/DMph/3qv5Xib4ti7 VHfrKrY9AB4pBac4Bu4ZwsjasqEn9VhhXZlEVuzsfYjbp+6vYnSoflLEAm/Jv5mqrB8N qGCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=VSu1/mbLML5yHnH1BATDOwKL/6/pp8snOnc9YCpJ6/U=; b=CPwqdJZzEqMeCFECayXs7P3Z4xHYuB9+xvIKfzwKsaU0znprvCzeKUKjqQrPd9S+kd rUBmr45YPqvTITJebaEdoi/wJL7np147DQNZGhPLv64yuWtaTSn4/iE7c9QbyQZ7ETVr GGC4rZeOyE6vdvZB/A1/pA46F9KTQO5im72uGzTb7mhXbPdZBrBXPj0cVsvsYlEvyr4t JYF2iWa1cMy9w+ZGoE5DPmU0tUA5CjSpIrfGj+BCKytzBYrD9vDXQoQ2HlFn/VEfAPJZ Hhx04weLD4vedM8D4kWJ6Q0Vkb5VnoAhRDeWEp0vVCFCV9tj0txCjwJQgeg7D8i8enzg z0tQ== X-Gm-Message-State: AOAM531L6yKPNqUledqTNtgenFwPRwYxmD+a+8S2EXmT6/WfOMnplvpg i2T7tne0jlTjg4IJL6Jboe8pNJ2j8xQe6w== X-Google-Smtp-Source: ABdhPJzfZKIIcXfHyXruUhq8Uo5ZYSDENOkFte+Fe5WS0Ahxe51Y4a+oO9VZlKPKOYiYt0uV8DDrzw== X-Received: by 2002:a92:910:: with SMTP id y16mr1200419ilg.22.1600995333731; Thu, 24 Sep 2020 17:55:33 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id k2sm462499ioj.2.2020.09.24.17.55.32 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 24 Sep 2020 17:55:32 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id ADECF8005CB; Thu, 24 Sep 2020 19:55:31 -0500 (CDT) Date: Thu, 24 Sep 2020 19:55:31 -0500 From: Justin Pryzby To: pgsql-hackers@postgresql.org Subject: pg_upgrade: fail early if a tablespace dir already exists for new cluster version Message-ID: <20200925005531.GJ23631@telsasoft.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="0QFb0wBpEddLcDHQ" Content-Disposition: inline User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --0QFb0wBpEddLcDHQ Content-Type: text/plain; charset=us-ascii Content-Disposition: inline This should be caught during --check, or earlier in the upgrade, rather than only after dumping the schema. Typically, the tablespace dir would be left behind by a previous failed upgrade attempt, causing a cascade of failured upgrades. I guess I may not be the only one with a 3 year old wrapper which has a hack to check for this. |rm -fr pgsql12.dat pgsql13.dat |/usr/pgsql-12/bin/initdb -D pgsql12.dat --no-sync |/usr/pgsql-13/bin/initdb -D pgsql13.dat --no-sync |/usr/pgsql-12/bin/postgres -D pgsql12.dat -c port=5678 -k /tmp |mkdir tblspc tblspc/PG_13_202007203 |psql -h /tmp -p 5678 postgres -c "CREATE TABLESPACE one LOCATION '/home/pryzbyj/tblspc'" |/usr/pgsql-13/bin/pg_upgrade -D pgsql13.dat -d pgsql12.dat -b /usr/pgsql-12/bin |pg_upgrade_utility.log: |CREATE TABLESPACE "one" OWNER "pryzbyj" LOCATION '/home/pryzbyj/tblspc'; |psql:pg_upgrade_dump_globals.sql:27: ERROR: directory "/home/pryzbyj/tblspc/PG_13_202007201" already in use as a tablespace --0QFb0wBpEddLcDHQ Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-pg_upgrade-check-to-avoid-tablespace-failure-mode.patch" From 3df104610d73833da60bffcc54756a6ecb2f390f Mon Sep 17 00:00:00 2001 From: Justin Pryzby Date: Thu, 24 Sep 2020 19:49:40 -0500 Subject: [PATCH v1] pg_upgrade --check to avoid tablespace failure mode --- src/bin/pg_upgrade/check.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c index 2f7aa632c5..b07e63dab2 100644 --- a/src/bin/pg_upgrade/check.c +++ b/src/bin/pg_upgrade/check.c @@ -13,6 +13,7 @@ #include "fe_utils/string_utils.h" #include "mb/pg_wchar.h" #include "pg_upgrade.h" +#include "common/relpath.h" static void check_new_cluster_is_empty(void); static void check_databases_are_compatible(void); @@ -27,6 +28,7 @@ static void check_for_tables_with_oids(ClusterInfo *cluster); static void check_for_reg_data_type_usage(ClusterInfo *cluster); static void check_for_jsonb_9_4_usage(ClusterInfo *cluster); static void check_for_pg_role_prefix(ClusterInfo *cluster); +static void check_for_existing_tablespace_dirs(ClusterInfo *new_cluster); static char *get_canonical_locale_name(int category, const char *locale); @@ -187,6 +189,8 @@ check_new_cluster(void) check_is_install_user(&new_cluster); check_for_prepared_transactions(&new_cluster); + + check_for_existing_tablespace_dirs(&new_cluster); } @@ -541,6 +545,34 @@ create_script_for_cluster_analyze(char **analyze_script_file_name) check_ok(); } +/* + * Check that tablespace dirs for new cluster do not already exist. + * If they did, they'd cause an error while restoring global objects. + * This allows failing earlier rather than only after dumping schema. + */ +static void +check_for_existing_tablespace_dirs(ClusterInfo *new_cluster) +{ + char old_tablespace_dir[MAXPGPATH]; + + prep_status("Checking for pre-existing tablespace directories for new cluster version"); + +// if (GET_MAJOR_VERSION(old_cluster.major_version) <= 804) + for (int tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++) + { + struct stat statbuf; + snprintf(old_tablespace_dir, MAXPGPATH, "%s%c%s", + os_info.old_tablespaces[tblnum], + PATH_SEPARATOR, TABLESPACE_VERSION_DIRECTORY); + + canonicalize_path(old_tablespace_dir); + + // XXX: lstat ? + if (stat(old_tablespace_dir, &statbuf) == 0) + pg_fatal("tablespace directory already exists for new cluster version: \"%s\"\n", + old_tablespace_dir); + } +} /* * create_script_for_old_cluster_deletion() -- 2.17.0 --0QFb0wBpEddLcDHQ--