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 1pZQpD-0005vm-SY for pgsql-admin@arkaria.postgresql.org; Tue, 07 Mar 2023 06:29:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pZQpB-0003nv-FU for pgsql-admin@arkaria.postgresql.org; Tue, 07 Mar 2023 06:29:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pZQpA-0003nm-VP for pgsql-admin@lists.postgresql.org; Tue, 07 Mar 2023 06:29:25 +0000 Received: from mail-ed1-x52f.google.com ([2a00:1450:4864:20::52f]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pZQp8-0001RK-14 for pgsql-admin@lists.postgresql.org; Tue, 07 Mar 2023 06:29:24 +0000 Received: by mail-ed1-x52f.google.com with SMTP id i34so48146428eda.7 for ; Mon, 06 Mar 2023 22:29:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20210112.gappssmtp.com; s=20210112; t=1678170560; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=fI/0p0R8NuE8jgZhGgbKUpcp4zJ+Bl4yaPJLck91ypI=; b=5egijnNAbiNFPhgyRuDZ9lb/HxksRspiP97Dz5a1vycXgr325G0bdfPM1zr+uG9Gif hOG6gYe1aug2Et73usATkmpszNQ/o3OQ64qQZ0irBvZNXb/NwiY4mqj92abeQqwKIK6l iCEme6zCgEdliE52Kw+E+8wbhC/sO9Ag0GyteAAgf0OAojFIwSRTvZAv/beZyXXT60P5 d4UqvTYXbKLeLErVurs50iUQHbwDXRTIwnPuL0i9HGjJ9JH/w5ZNpLKRixKKxL1tCn4m Y16pzZpXMIgY+dRI4Ikj4uvZOZye/iXkQpnoyLZ3Z/rg7JsKYBvtE3nMBeClpTOE4ow3 LWyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678170560; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=fI/0p0R8NuE8jgZhGgbKUpcp4zJ+Bl4yaPJLck91ypI=; b=T7XqfCHUo2+8/rrJeOBeRge8QvDE/t2sVuaM4+fTe6dA153ESe13R/qHRcOn6thnfe noVjksyBeHOK34qFQlMmsn7v0uTAkXidT9B3fBrkLcueKKQPkCW9K/OhgJVyMctkSKHp OFUbepdjRzIfDHIMSTLE/Fx9GcjTu7FD9rYJ38TwL2Ug1hVUNyv97uI4zyeHxcoxDyp6 pd5zpuJ9QKkIQcmuG1urI05VytgLS7sLFe8TL9HRNvW8+QBdFIosmBT+kGGn0Iz5ut0w eduil8KVoKJA+P/byuCngAICBuLmrGvADgpSwbSwVXSMSnGvwGJsRGVG2rW4iNnXtZe9 PFHA== X-Gm-Message-State: AO0yUKUd07/kZ+me8MaRKKsoVZEgtAfscc4K8JgbC60J6MYtCVNJiLrs mpfLA58HvG1dW6/9Z7iQurTd7dklx/IMTd/GEmc= X-Google-Smtp-Source: AK7set+ECYaruFH8e24Asq2ffJEoRNAkDra7NUPHExcFp/fdgqkeA341vWockKAxfQ0FuwGCSfMlWQ== X-Received: by 2002:a17:906:b189:b0:8b1:7b5c:bde7 with SMTP id w9-20020a170906b18900b008b17b5cbde7mr13863778ejy.77.1678170560545; Mon, 06 Mar 2023 22:29:20 -0800 (PST) Received: from localhost.localdomain (178-190-180-9.adsl.highway.telekom.at. [178.190.180.9]) by smtp.gmail.com with ESMTPSA id n26-20020a50c21a000000b004a27046b7a7sm6158367edf.73.2023.03.06.22.29.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Mar 2023 22:29:20 -0800 (PST) Message-ID: Subject: Re: Migrating tables to new schema with minimal downtime From: Laurenz Albe To: Joseph Hammerman , pgsql-admin@lists.postgresql.org Date: Tue, 07 Mar 2023 07:29:19 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.46.4 (3.46.4-1.fc37) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2023-03-06 at 13:00 -0800, Joseph Hammerman wrote: > I am working on splitting up tables that are currently in a single schema= into multiple, > context specific schemas. I would like to do this with minimal downtime f= rom=C2=A0the client > perspective. I will have=C2=A0prepopulated=C2=A0the=C2=A0existing applica= tion client search_path with > the new schema name. >=20 > When inquiring about this in postgresql IRC a concern was raised about da= ta safety, > due to locks concurrently executing transactions might have on the relati= on=C2=A0in question > when the ALTER=C2=A0TABLE ... SET SCHEMA TO ... command is executed (even= if done in a transaction). >=20 > Would it be data safe if I took an ACCESS EXCLUSIVE lock on the table? >=20 > My testing shows that a session with both the old schema and the new sche= ma in its > path will cleanly determine the new location of the relation - does anyon= e have any > caveats or know of any sharp edges I should be aware of here? I don't think there is any concern with the integrity or consistency of you= r data; the transaction system will take care of that. The potential problem is th= at the ALTER TABLE will be blocked by concurrent transactions that use the table, = and it will block such concurrent transactions in turn. So make sure that there are no long running transactions that use the table= , then you should be fine, as long as you COMMIT your transactions right after the= ALTER TABLE. Yours, Laurenz Albe