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 1kl5qV-000125-HL for pgsql-hackers@arkaria.postgresql.org; Fri, 04 Dec 2020 07:49: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 1kl5qT-0002RQ-Bd for pgsql-hackers@arkaria.postgresql.org; Fri, 04 Dec 2020 07:49: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 1kl5qT-0002RI-3W for pgsql-hackers@lists.postgresql.org; Fri, 04 Dec 2020 07:49:37 +0000 Received: from esa4.fujitsucc.c3s2.iphmx.com ([68.232.151.214]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kl5qL-0007TV-6U for pgsql-hackers@lists.postgresql.org; Fri, 04 Dec 2020 07:49:36 +0000 IronPort-SDR: PqRQm6+mOjHvNNVUUWmZcTwXhvBRfi3ANn22R1Q0ONcK54Y9mQ0JO7BqkFhIRvgJ/kpgI70aDD Y3Wg63LThHxr/oRjFcRjdWI5Loiv/IBjuQlglpfSL8y7HeTqRkt7BhMrQNgb/GeCpgMLdnPoI7 0JUwXVo1MlhEcLtm2Rq0vYd8RLP4G6pC9rfiH62l8Qexh9hfV6mIKzBaJisuHq68ojghCped0I G4XEqgkUGBQTdjSA4EUirdo6i7AmxzJ6X4A8xXgX+T2sfZ41+zADCq/t7jSv8XeeyFE2vvWwtt XiE= X-IronPort-AV: E=McAfee;i="6000,8403,9824"; a="30262562" X-IronPort-AV: E=Sophos;i="5.78,392,1599490800"; d="scan'208";a="30262562" Received: from mail-os2jpn01lp2052.outbound.protection.outlook.com (HELO JPN01-OS2-obe.outbound.protection.outlook.com) ([104.47.92.52]) by ob1.fujitsucc.c3s2.iphmx.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 04 Dec 2020 16:49:26 +0900 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oAwq28zQRIUGQ5qYobYeU2yulBnwG/DyGAJop8QZf3rCHAGLi0y5afyj3+l+AWt4q/OEJgahqammHdPbMOwrQNVET32xot66ZdJdxF04gTWN/1PH2H4+IvTYo3+zUsi8xUkg+W4Hkjrs+0GlVCLPmmSd6WTIDnbj/swyJGJTk3LNkWHHEFSiK8A9zPChYiOj9EtanDnCsiK1jxfZXBOvUs67Gda2b05DT4f3hFn1f7qhT4bKMG6kkADHREwrrLsCtT0LSTnlIzJE3L3TsP4lVG94fu6gb1rYh0zSvOfg5moMsNkVXzTddvvd1uV5vbFaOqhyIVyekpzGS+nsBBVZJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ss20W+v/E/DVLKUGE4iuQhICxf4mLCdouFfDE6bdSXI=; b=Mlm6jzDkHjf+DZduYRIYfZIw8kkhCloW7AGftNkc5fDUAQlN8knU8DIIJZewqdk/8HOo/xQkE9cl9gbhnTtnn3vCrXaiT9M+zQLFlSliXw1TMIhCVw0QqadecmDM2yWycPNIZl7PJzBKag6tlCktZH+1Y3aWR42yzyHCouhPssbzsB4nPsDVJ2ZTfti2NH5ryGbbe82OQ2kKl6oSivc3uvuJZcqWDAuMTWtd++6rqz622guuQ0B6K9ymE1gwZGZgTyZhWhWBM3Mmovh52ANCLssCkruSs12eNAxGNaqgPh2GGTRg7iT570G09lOIyN9Tz76q7JQ/6ihU8kl7aCHqIg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=fujitsu.com; dmarc=pass action=none header.from=fujitsu.com; dkim=pass header.d=fujitsu.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fujitsu.onmicrosoft.com; s=selector2-fujitsu-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=ss20W+v/E/DVLKUGE4iuQhICxf4mLCdouFfDE6bdSXI=; b=QygJ7itUhDnIykRBXwviCLRw1Q7GJ1G1E1X4iis+fxg+HCPxvqipdWUpPJn1h9aDytkKemnfOMHm7mpbbJ6/xRDZbTC2/z03q2qqqypDDFw1oyK3SwynJeXeESzLXWMJ6np2k8UQNpu52AiLrZV9DgWpj+j9PxVff9iRXGi+6Rs= Received: from TYAPR01MB2990.jpnprd01.prod.outlook.com (2603:1096:404:86::19) by TYAPR01MB5593.jpnprd01.prod.outlook.com (2603:1096:404:8056::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3632.17; Fri, 4 Dec 2020 07:49:22 +0000 Received: from TYAPR01MB2990.jpnprd01.prod.outlook.com ([fe80::ad26:4de1:1519:675c]) by TYAPR01MB2990.jpnprd01.prod.outlook.com ([fe80::ad26:4de1:1519:675c%7]) with mapi id 15.20.3611.025; Fri, 4 Dec 2020 07:49:22 +0000 From: "tsunakawa.takay@fujitsu.com" To: 'Kyotaro Horiguchi' CC: "osumi.takamichi@fujitsu.com" , "sfrost@snowman.net" , "masao.fujii@oss.nttdata.com" , "ashutosh.bapat.oss@gmail.com" , "pgsql-hackers@lists.postgresql.org" Subject: RE: In-placre persistance change of a relation Thread-Topic: In-placre persistance change of a relation Thread-Index: AQHWuAVWpBgdNJT6dUCBfvr3Pw06w6nFeQaAgAAkKrCAAAxbgIAAEt0AgCD1VDA= Date: Fri, 4 Dec 2020 07:49:22 +0000 Message-ID: References: <20201113.132201.148696021363404735.horikyota.ntt@gmail.com> <20201113.172312.1767546251154140847.horikyota.ntt@gmail.com> In-Reply-To: <20201113.172312.1767546251154140847.horikyota.ntt@gmail.com> Accept-Language: ja-JP, en-US Content-Language: ja-JP X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: gmail.com; dkim=none (message not signed) header.d=none;gmail.com; dmarc=none action=none header.from=fujitsu.com; x-originating-ip: [218.44.52.182] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 3eb53862-6836-4257-00ab-08d898291d43 x-ms-traffictypediagnostic: TYAPR01MB5593: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:9508; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: rsdrporcxorj6nWhrtt69fwHNGT78/6/Bc22KdwD77G0MMjWIYxfBQHYPI0dTQx6gaJpGt3HAOr6ubHe9fQk4eAucGImHy7x5XVH7u+nnSmQjqDfS34/KPV58BzdLEIprOZaRq+GSMD6l/w6Wced0lAqTE9usqWV2ELq7+HYlMpWmidAk/LjJirswKs6ipCL2WQs0sI2N71EKx3EQPdOJwcua5HdH4QkNste+aQF6qdpYtlxdfTknwnQh69o35l95Lpw+E5lYGqHJEH7f7+CxBfVWWkRfwUPNe1Aoj4z4iD8tbDocmg9G3/TPHzQGYZIIUC5Lc94mKG4VQ97nWdzXZMaVu0LihwgjtiDM+a5CwuOn0cFwPDqGN4LFtIo+Z3c x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:TYAPR01MB2990.jpnprd01.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(376002)(396003)(346002)(39860400002)(136003)(366004)(66946007)(4326008)(85182001)(66556008)(66476007)(64756008)(478600001)(5660300002)(8936002)(86362001)(8676002)(6916009)(66446008)(83380400001)(76116006)(7696005)(6506007)(52536014)(33656002)(26005)(55016002)(186003)(71200400001)(9686003)(316002)(2906002)(54906003)(777600001);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata: =?iso-2022-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K2pPMFgrQ1BO?= =?iso-2022-jp?B?bG5hbFhvRDg1NjhEeXd1OWY5T1dTbTFCS3JtcC9OOTdXclhwZGZnZ0V3?= =?iso-2022-jp?B?LzdkUmg3VjViY1NoVG1hT1JMVGRmQmFoc2dLeU1jbGt3aUNLK0dIbCtn?= =?iso-2022-jp?B?bkdvaXlaUzJZZ0s4MWZteVF1RDVyWDhEdk5URUZpTWk2NEg1bmN6VEFr?= =?iso-2022-jp?B?R2hCNlBzQUE3NmVDRlRlbE85Z1BYVjgwS1pNaE1xTTZuMjA4N0ptdXRs?= =?iso-2022-jp?B?THVSd2VON3ZGTXlOVG9CSFh3UHVLTmU0M2dlSGdZSm5oeVZCN3kxVjBx?= =?iso-2022-jp?B?R2xYL1RJYUNYUG1XQTFkQmpxZDdnRW92ZnNtSm1QZ29scGZKYXd6bmdQ?= =?iso-2022-jp?B?eitBSjVOZE9zdGkrblNKUmpsaFF3d29sWWpZZ213aU9hbW9iNmRhUmNY?= =?iso-2022-jp?B?MUFMaUcvb05EL3EvL2dsUndWSkhVRW5VNkgzUmV4YjlKM2FJVEo4bnp3?= =?iso-2022-jp?B?ZmFCNnI4VEFQWkswRzd5MDJhcjI5R0toNEFWMnVtaTJIZFN6TT0=?= Content-Type: text/plain; charset="iso-2022-jp" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: fujitsu.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: TYAPR01MB2990.jpnprd01.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 3eb53862-6836-4257-00ab-08d898291d43 X-MS-Exchange-CrossTenant-originalarrivaltime: 04 Dec 2020 07:49:22.3334 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: a19f121d-81e1-4858-a9d8-736e267fd4c7 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: GK0fFPVUEb7+rcvZ9ohRScKaFEd97Y1v/8WQE9AhluOu/9jDD7i1jp6A8Ty0OoP7KGUOfx+7B9xkNkPD8mXnNub5FhLrN6a25lPHFiniTUk= X-MS-Exchange-Transport-CrossTenantHeadersStamped: TYAPR01MB5593 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk From: Kyotaro Horiguchi > > No, not really. The issue is more around what happens if we crash > > part way through. At crash recovery time, the system catalogs are not > > available, because the database isn't consistent yet and, anyway, the > > startup process can't be bound to a database, let alone every database > > that might contain unlogged tables. So the sentinel that's used to > > decide whether to flush the contents of a table or index is the > > presence or absence of an _init fork, which the startup process > > obviously can see just fine. The _init fork also tells us what to > > stick in the relation when we reset it; for a table, we can just reset > > to an empty file, but that's not legal for indexes, so the _init fork > > contains a pre-initialized empty index that we can just copy over. > > > > Now, to make an unlogged table logged, you've got to at some stage > > remove those _init forks. But this is not a transactional operation. > > If you remove the _init forks and then the transaction rolls back, > > you've left the system an inconsistent state. If you postpone the > > removal until commit time, then you have a problem if it fails, >=20 > It's true. That are the cause of headache. ... > The current implement is simple. It's enough to just discard old or > new relfilenode according to the current transaction ends with commit > or abort. Tweaking of relfilenode under use leads-in some skews in > some places. I used pendingDelete mechanism a bit complexified way > and a violated an abstraction (I think, calling AM-routines from > storage.c is not good.) and even introduce a new fork kind only to > mark a init fork as "not committed yet". There might be better way, > but I haven't find it. I have no alternative idea yet, too. I agree that we want to avoid them, e= specially introducing inittmp fork... Anyway, below are the rest of my rev= iew comments for 0001. I want to review 0002 when we have decided to go wi= th 0001. (2) XLOG_SMGR_UNLINK seems to necessitate modification of the following comment= s: [src/include/catalog/storage_xlog.h] /* * Declarations for smgr-related XLOG records * * Note: we log file creation and truncation here, but logging of deletion * actions is handled by xact.c, because it is part of transaction commit. */ [src/backend/access/transam/README] 3. Deleting a table, which requires an unlink() that could fail. Our approach here is to WAL-log the operation first, but to treat failure of the actual unlink() call as a warning rather than error condition. Again, this can leave an orphan file behind, but that's cheap compared to the alternatives. Since we can't actually do the unlink() until after we've committed the DROP TABLE transaction, throwing an error would be out of the question anyway. (It may be worth noting that the WAL entry about the file deletion is actually part of the commit record for the dropping transaction.) (3) +/* This is bit-map, not ordianal numbers */ There seems to be no comments using "bit-map". "Flags for ..." can be seen= here and there. (4) Some wrong spellings: + /* we flush this buffer when swithing to PERMANENT */ swithing -> switching + * alredy flushed out by RelationCreate(Drop)InitFork called just alredy -> already + * relation content to be WAL-logged to recovery the table. recovery -> recover + * The inittmp fork works as the sentinel to identify that situaton. situaton -> situation (5) + table_close(classRel, NoLock); + + + + } These empty lines can be deleted. (6) +/* + * Perform XLogInsert of an XLOG_SMGR_UNLINK record to WAL. + */ +void +log_smgrbufpersistence(const RelFileNode *rnode, bool persistence) ... + * Make an XLOG entry reporting the file unlink. Not unlink but buffer persistence? (7) + /* + * index-init fork needs further initialization. ambuildempty shoud do + * WAL-log and file sync by itself but otherwise we do that by myself. + */ + if (rel->rd_rel->relkind =3D=3D RELKIND_INDEX) + rel->rd_indam->ambuildempty(rel); + else + { + log_smgrcreate(&rnode, INIT_FORKNUM); + smgrimmedsync(srel, INIT_FORKNUM); + } + + /* + * We have created the init fork. If server crashes before the current + * transaction ends the init fork left alone corrupts data while recovery= . + * The inittmp fork works as the sentinel to identify that situaton. + */ + smgrcreate(srel, INITTMP_FORKNUM, false); + log_smgrcreate(&rnode, INITTMP_FORKNUM); + smgrimmedsync(srel, INITTMP_FORKNUM); If the server crashes between these two processings, only the init fork exi= sts. Is it correct to create the inittmp fork first? (8) + if (inxact_created) + { + SMgrRelation srel =3D smgropen(rnode, InvalidBackendId); + smgrclose(srel); + log_smgrunlink(&rnode, INIT_FORKNUM); + smgrunlink(srel, INIT_FORKNUM, false); + log_smgrunlink(&rnode, INITTMP_FORKNUM); + smgrunlink(srel, INITTMP_FORKNUM, false); + return; + } smgrclose() should be called just before return. Isn't it necessary here to revert buffer persistence state change? (9) +void +smgrunlink(SMgrRelation reln, ForkNumber forknum, bool isRedo) +{ + smgrsw[reln->smgr_which].smgr_unlink(reln->smgr_rnode, forknum, isRedo); +} Maybe it's better to restore smgrdounlinkfork() that was removed in the old= er release. That function includes dropping shared buffers, which can clea= n up the shared buffers that may be cached by this transaction. (10) [RelationDropInitFork] + /* revert buffer-persistence changes at abort */ + pending =3D (PendingRelDelete *) + MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete)); + pending->relnode =3D rnode; + pending->op =3D PDOP_SET_PERSISTENCE; + pending->bufpersistence =3D false; + pending->backend =3D InvalidBackendId; + pending->atCommit =3D true; + pending->nestLevel =3D GetCurrentTransactionNestLevel(); + pending->next =3D pendingDeletes; + pendingDeletes =3D pending; +} bufpersistence should be true. (11) + BlockNumber block =3D 0; ... + DropRelFileNodeBuffers(rbnode, &pending->unlink_forknum, 1, + &block); "block" is unnecessary and 0 can be passed directly. (12) - && pending->backend =3D=3D InvalidBackendId) + && pending->backend =3D=3D InvalidBackendId && + pending->op =3D=3D PDOP_DELETE) nrels++; It's better to put && at the beginning of the line to follow the existing c= ode here. (13) + table_close(rel, lockmode); lockmode should be NoLock to retain the lock until transaction completion. (14) + ctl.keysize =3D sizeof(unlogged_relation_entry); + ctl.entrysize =3D sizeof(unlogged_relation_entry); + hash =3D hash_create("unlogged hash", 32, &ctl, HASH_ELEM); ... + memset(key.oid, 0, sizeof(key.oid)); + memcpy(key.oid, de->d_name, oidchars); + ent =3D hash_search(hash, &key, HASH_FIND, NULL); keysize should be the oid member of the struct. Regards Takayuki Tsunakawa