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 1pMiGR-0003eS-9I for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Jan 2023 04:28:59 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pMiGP-000182-Q2 for pgsql-hackers@arkaria.postgresql.org; Tue, 31 Jan 2023 04:28:57 +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 1pMiGP-00011g-Ey for pgsql-hackers@lists.postgresql.org; Tue, 31 Jan 2023 04:28:57 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pMhxn-0001x1-QG for pgsql-hackers@postgresql.org; Tue, 31 Jan 2023 04:09:49 +0000 Received: by mail-ej1-x633.google.com with SMTP id me3so38023246ejb.7 for ; Mon, 30 Jan 2023 20:09:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TvUxgeIYD0CFpNd33+pbN37IEYcrz24O6m+r0OtCCqg=; b=Nnzh7SmbJVsOvN8DPF0qXWHQompVfiXVTsFvSAn26pSfa9DoA0Cm7tzm4lmhG3nP0O 3k6AdaR/OWYa4N9sAlOdgtB/bqvhAsQnwq4lvhFibImYRZVPKjLml5p5kX0gy5JFJhr2 ceqBvcJn/wymwQWvW60FImr8SAfrmqwNR/GOUdHj96Yp5E2OeP+PHL8WMOtJ+oSADyr/ 1uFg0rVHyt/eU7ZhyR5cAFBerxZ0T4G9iRXlZbeayGOmKXNB3dsZUvGcCI6b9HdUjsKY 4Dg5JEKZ5JPqUGPRQAi4gLfi83ahX2L98YOzzLALyyRSuazmQopc61CRn2kaQh1MiDXN wbfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=TvUxgeIYD0CFpNd33+pbN37IEYcrz24O6m+r0OtCCqg=; b=wDtQyhTkRHBVFrbeVPs0OcPI9IS7Q+YjblV4kaNSgO3haJeB54I7qngSj02GUHL644 h7uNsSDBdTXd1yo228lzz9NoCQIbitYOPIhv+sUJFrLONRtxLMWFigN2SQWhq+JjNfqW QGMek1ravPg0b26I+P6Q1DL52ZtruvIJLEDtvvUODfG+1oomoxkErhKPAMzidCRAGymS +kzjLMDsC+YqnGrU83KROq4lBnRjDG/a/pg/lYm2Eh0BJ84V/Yhku0I4SEYadxVvtbcK XYhGw55d53i4kuh4jWMkhvUnaOlNuKVXKuj016jH6mV4kMOTie9h4/ZjAJqDU6UxSDLR 23Gg== X-Gm-Message-State: AFqh2kpMXZDcVZjK9TVd6Aeb3rnj5L5K3EpbPnC8WijrZ0BLNvM+b7sg q/OQLuusf6xlZ4G+5oJUC7G3ntNmAKTG+CPLXgk= X-Google-Smtp-Source: AMrXdXuchcEHcr59D9eZKDV8vQeKHT5RwU0pUhfBoUgD/PAEijQv86GkySHkXyJd319sQKChX7L3lrKaiAC0s0ukcuQ= X-Received: by 2002:a17:906:a00b:b0:7c0:beef:79e2 with SMTP id p11-20020a170906a00b00b007c0beef79e2mr9744341ejy.148.1675138182701; Mon, 30 Jan 2023 20:09:42 -0800 (PST) MIME-Version: 1.0 References: <20221123014224.xisi44byq3cf5psi@awork3.anarazel.de> <3748783.1669241103@sss.pgh.pa.us> <20d45197-80be-4984-33a9-a8d13708992a@inbox.ru> In-Reply-To: <20d45197-80be-4984-33a9-a8d13708992a@inbox.ru> From: Thomas Munro Date: Tue, 31 Jan 2023 17:09:05 +1300 Message-ID: Subject: Re: odd buildfarm failure - "pg_ctl: control file appears to be corrupt" To: "Anton A. Melnikov" Cc: Tom Lane , Andres Freund , pgsql-hackers@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Jan 31, 2023 at 2:10 PM Anton A. Melnikov wrote: > Also checked for a few hours that the patch 0002 fixes this error, > but there are some questions to its logical structure. Hi Anton, Thanks for looking! > The equality between the previous and newly calculated crc is checked only > if the last crc calculation was wrong, i.e not equal to the value stored in the file. > It is very unlikely that in this case the previous and new crc can match, so, in fact, > the loop will spin until crc is calculated correctly. In the other words, > this algorithm is practically equivalent to an infinite loop of reading from a file > and calculating crc while(EQ_CRC32C(crc, ControlFile->crc) != true). Maybe it's unlikely that two samples will match while running that torture test, because it's overwriting the file as fast as it can. But the idea is that a real system isn't writing the control file most of the time. > But then it can be simplified significantly by removing checksums equality checks, > bool fist_try and by limiting the maximum number of iterations > with some constant in the e.g. for loop to avoid theoretically possible freeze. Yeah, I was thinking that we should also put a limit on the loop, just to be cautious. Primary servers write the control file very infrequently. Standybys more frequently, while writing data out, maybe every few seconds on a busy system writing out a lot of data. UpdateMinRecoveryPoint() makes some effort to avoid updating the file too often. You definitely see bursts of repeated flushes that might send this thing in a loop for a while if the timings were exactly wrong, but usually with periodic gaps; I haven't really studied the expected behaviour too closely. > Or maybe use the variant suggested by Tom Lane, i.e, as far as i understand, > repeat the file_open-read-close-calculate_crc sequence twice without a pause between > them and check the both calculated crcs for the equality. If they match, exit and return > the bool result of comparing between the last calculation with the value from the file, > if not, take a pause and repeat everything from the beginning. Hmm. Would it be good enough to do two read() calls with no sleep in between? How sure are we that a concurrent write will manage to change at least one bit that our second read can see? I guess it's likely, but maybe hypervisors, preemptible kernels, I/O interrupts or a glitch in the matrix could decrease our luck? I really don't know. So I figured I should add a small sleep between the reads to change the odds in our favour. But we don't want to slow down all reads of the control file with a sleep, do we? So I thought we should only bother doing this slow stuff if the actual CRC check fails, a low probability event. Clearly there is an element of speculation or superstition here. I don't know what else to do if both PostgreSQL and ext4 decided not to add interlocking. Maybe we should rethink that. How bad would it really be if control file access used POSIX file locking? I mean, the writer is going to *fsync* the file, so it's not like one more wafer thin system call is going to hurt too much.