Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyPHb-0003bs-C6 for pgsql-general@arkaria.postgresql.org; Mon, 25 Feb 2019 23:03:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gyPHa-0003cf-57 for pgsql-general@arkaria.postgresql.org; Mon, 25 Feb 2019 23:03:34 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyPHZ-0003cX-PK for pgsql-general@lists.postgresql.org; Mon, 25 Feb 2019 23:03:33 +0000 Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyPHX-0002S1-5f for pgsql-general@lists.postgresql.org; Mon, 25 Feb 2019 23:03:32 +0000 Received: by mail-pg1-x52c.google.com with SMTP id y4so5166500pgc.12 for ; Mon, 25 Feb 2019 15:03:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-transfer-encoding:content-language; bh=3AbLc8iiuWBkASQAbA2XEusefwKeHLNskb9tNA9P2hg=; b=oYmN6KkRoHMnYHFw1uw8i/C02dvbt1rGhlEW4ev46V2mSTQFENUmKXB1wPam1H18S9 2RaAgjIBjys5sVu9OOdRYsVmo/9FjAYL3rsbckGECwXCybdBIEWhBDEiNn2cBdxhKU7k lPT9hh2jZCIghiKlWkrpyJ2DYZGeTxBk8JHP4FFg448hxTwF3xQAsgq5r48SUaLYXqqt RIJ04eMKt0MwprZftgpyxnOP/HrQUzBIJHghrBxIAV1ECpxe/AS9+O3hporyo7m9qTBc W7Y0S5ULOc2pPdhy7k9Knb+rjdfD0ljKN4juMiiElFcM1ZB8JEmkzqi4voUECToHx7UK wCLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-transfer-encoding:content-language; bh=3AbLc8iiuWBkASQAbA2XEusefwKeHLNskb9tNA9P2hg=; b=ap1CV72hfVsyHhlI9vCBxg6f09NM0NYu5UhbsRnbJnEeg68L+PcWinWctwYxHMzDvW 4KB+XxrGwBV82FLZeTjqqLPfVU/thcDlkvlRn/uPxhZ+UF6pzKUNB5oRhG7krkmBNwc4 /8sDJHLvKwHSSzQIDJ2GI8Ra6nkIskaHFsqL/vvF4MAFOYpchcf4w3i1Ic0B8SmO3RzK ZF9fg1Bkb1MEZqeDLdkku1/7ywoYNobbaFEqWpnsNXsNIqcNLAdeVWUd3109TkkcS2Ds IrOWHDQLUtR2u9eX4T+uWuRUNfiyAgUoevl4HBCG1rAlE7fZE3hP88lzCSSWrD3okC3Q bbtw== X-Gm-Message-State: AHQUAuaTuRbtpLIjroQ9nRfhnJCsVETd9iJ6xSnMFFqf+EnEHnPa04Ss PPyT5GIKDxJCmMLKfL2u4HPCN5Vt X-Google-Smtp-Source: AHgI3IYlZLhnDnH7XfwzJbGMLFOUmFRU4g2ATHB0USa+p2G/VUITN7GxQ72ME5Ybh4wtjhaDu4Xm5Q== X-Received: by 2002:a62:6d81:: with SMTP id i123mr22564313pfc.235.1551135809591; Mon, 25 Feb 2019 15:03:29 -0800 (PST) Received: from ?IPv6:2601:646:c201:b55:bdd0:e358:2c10:a2c3? ([2601:646:c201:b55:bdd0:e358:2c10:a2c3]) by smtp.gmail.com with ESMTPSA id z67sm19302908pfi.152.2019.02.25.15.03.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 25 Feb 2019 15:03:28 -0800 (PST) To: pgsql-general@lists.postgresql.org From: Kevin Wilkinson Subject: atomically replace partition of range partitioned table Message-ID: <3a929946-5a7d-fed5-c252-d23dd0925513@gmail.com> Date: Mon, 25 Feb 2019 15:03:26 -0800 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk i have a range partitioned table with a brin index that i am using for "Internet-of-Things" kind of data (essentially timeseries data about some entities). the partition key is a timestamp. data is only loaded to the "current" partition and data is never modified. older partitions are static. the index key is the entity identifier. my problem is that the brin index on the current partition does not perform well (because summarization is not immediate) so i also include a b-tree index on the current partition. when the current partition is "full", i create a new partition. i then want to optimize the previous current partition by (1) clustering the partition on the index key to give me a correlation of 1 and (2) dropping the b-tree index to reclaim its storage space. i want to do this atomically so that querying over the full table is not interrupted. of course, the cluster command is not usable because it takes an exclusive lock. so, i do the following. 1. create a new partition table by copying the old partition table, ordered by index key. both tables will have the same partition key range. 2. create a brin index on the new table. 3. detach the old partition table from the parent and drop it. 4. attach the new partition table to the parent. what i need is for steps 3-4 to be atomic or quick. but, step 4 takes tens of seconds, sometimes almost a minute. i tried adding a check constraint to the new table so that it would not be scanned when attached but that does not help. is there any way to do want i want? thanks, kevin