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 1oPqIN-0007M0-4w for pgsql-bugs@arkaria.postgresql.org; Sun, 21 Aug 2022 19:07: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 1oPqIL-0003aK-T6 for pgsql-bugs@arkaria.postgresql.org; Sun, 21 Aug 2022 19:07:37 +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 1oPqIL-0003ZA-DQ for pgsql-bugs@lists.postgresql.org; Sun, 21 Aug 2022 19:07:37 +0000 Received: from mail-qt1-x82d.google.com ([2607:f8b0:4864:20::82d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oPqII-0006Wl-Iq for pgsql-bugs@lists.postgresql.org; Sun, 21 Aug 2022 19:07:36 +0000 Received: by mail-qt1-x82d.google.com with SMTP id x5so6569953qtv.9 for ; Sun, 21 Aug 2022 12:07:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc; bh=T33uNO0w4Kob0PMOpgrFZowMIJyuVu24IBQ+KnwDVG8=; b=eYLS8VHLcPM/uEVXGno1LQs8hsrSrCnLVm285cdP5j48awmPknDZD+ZktORJIpS68l NmScJnu5fy03CPFP683aSMIpeYlqOwfqjKzJdxdhdElWVI1+H4w4Vs1/AKEv1GNEVQnC Uc3qs7VmcGTLVfLKqg4lpv1zOjt36FEfCWnaM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:x-gm-message-state:from:to:cc; bh=T33uNO0w4Kob0PMOpgrFZowMIJyuVu24IBQ+KnwDVG8=; b=lEMNt1aNDvecVyB4FMUW43oA2KYiXIITS+jm1HpGFob7uKhuhOGF9LdTHxoGh1/ZAR 35IPDV8+Cc0mxTF43FdcV6doKNiULak97BnPRclDhTQg0CrI0KN1DgCZGO3lOTMH8Toe +kMHppoS61rvWXOF19OYqqaLxJiqwN68klUBPF7aL4WwUpPG2AGWjubiDO0/cIemvx3H iuiOkC+CPNoDNbBmB3v3eqz06GauTAVuNvQjC0fJWsDz9IQlm8MMhgnfEPSiQm7GoHPn o8mxaYhHh+yoe2XmkzcmnE1FW6PCmyRx4NsUkdsmdMsKn3XwzkVBbvEXFQldIfBVKikw Z6sg== X-Gm-Message-State: ACgBeo2KrMs748hOjbPPUWEGJB/U7EQLOxV4X1+CHmx0+0JvVkiaSkOT Iq93UXEuZglie31b145/qkDxcg== X-Google-Smtp-Source: AA6agR4ITcgXFt+b6jYfgog1Fgqsd7Kx03k9J3Nk+JBFkYTSTSFc+DV52K89Jigt0SOWhL3jqaneYA== X-Received: by 2002:a05:622a:1ba4:b0:344:57a4:7f4c with SMTP id bp36-20020a05622a1ba400b0034457a47f4cmr13301045qtb.146.1661108852523; Sun, 21 Aug 2022 12:07:32 -0700 (PDT) Received: from rfd.leadboat.com ([2600:1702:a20:5750::2e]) by smtp.gmail.com with ESMTPSA id f22-20020a05620a409600b006bad20a6cfesm10071084qko.102.2022.08.21.12.07.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 21 Aug 2022 12:07:31 -0700 (PDT) Date: Sun, 21 Aug 2022 12:07:28 -0700 From: Noah Misch To: Andres Freund Cc: "David G. Johnston" , "nathanfballance@gmail.com" , "pgsql-bugs@lists.postgresql.org" Subject: Re: BUG #17543: CSVLOG malformed from disk space error Message-ID: <20220821190728.GA631665@rfd.leadboat.com> References: <17543-ea64400353b46221@postgresql.org> <20220713004853.ayu4rfatejemvoyi@awork3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20220713004853.ayu4rfatejemvoyi@awork3.anarazel.de> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, Jul 12, 2022 at 05:48:53PM -0700, Andres Freund wrote: > On 2022-07-11 05:31:35 -0700, David G. Johnston wrote: > > On Saturday, July 9, 2022, PG Bug reporting form wrote: > > > Postgresql server with csvlog log_destination enabled will have malformed > > > CSV upon a disk space error. This causes any loading of the malformed *.csv > > > log file to error > > > > > > ASK: Can the CSV file be written to in a safer way which ensures proper > > > format even upon disk error? > > I’d have to say that there is little interest in sacrificing performance > > for safety here, which seems like an unavoidable proposition. > > I agree in general, but this specific issue seems easy enough to address. We > could just track whether the last write failed, and if so, emit an additional > newline. > > But that just fixes the simple case - if the last successful write contained > the start of an escaped string, the newline won't necessarily be recognized as > the end of a "row". Here's one approach avoiding that problem. After ENOSPC causes the logfile to end with a prefix of a message, issue ftruncate(logfile, logfile_length - written_bytes_of_message_prefix). An alternative would be to periodically posix_fallocate() substantial space in the logfile, and write messages only to already-allocated space. At rotation, clean shutdown, or startup, ftruncate() away trailing NUL bytes. I figure this is inferior to the other approach, because the trailing NUL bytes will be user-visible after OS crashes and when tailing active logs. (Neither approach prevents CSV corruption if the OS crashes in the middle of syslogger's processing of one record. I don't know a low-cost, general fix for that. One tough case is a field that should have been "foo""bar" getting truncated to "foo".)