Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eaBWO-0007TB-Bu for pgsql-hackers@arkaria.postgresql.org; Sat, 13 Jan 2018 02:26:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eaBWN-0008IO-7q for pgsql-hackers@arkaria.postgresql.org; Sat, 13 Jan 2018 02:26:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eaBWM-0008IE-Rb for pgsql-hackers@lists.postgresql.org; Sat, 13 Jan 2018 02:26:11 +0000 Received: from mail-wr0-x236.google.com ([2a00:1450:400c:c0c::236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eaBWE-0000rt-Gl for pgsql-hackers@postgresql.org; Sat, 13 Jan 2018 02:26:09 +0000 Received: by mail-wr0-x236.google.com with SMTP id 100so6835360wrb.7 for ; Fri, 12 Jan 2018 18:26:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=4eb+w4uk7aJLzQQxH5rhpDw6sncgHKJoS/QEyZCZzxY=; b=t4hqPqSdbV5GXt3YsG5okaJfEQJ7XMcCU9D1jixsBLf0YAPbwrpFqv5b7Q/PnKKBVd xggnMo75jhXbbdAtNZH1XwggV2dhscJYPCPTbYcsJwAUiwVMUzT2q85hoTrhtk3ewGve 2Du1rDG6TAh0s54WRS1gG+VKX7y/UL9MfufWvh3fbR6v0cJ/5DOUPNHcsrGqgt1dCfMW B0F0og8OpnsHkzFetyut4Q9lbFnH17zIchp0JC4Z71ClWx8/16p4pqMcQOOtx71yu0Kg ZdkeeVZUSDbe4LDQlpXxOqfLPpEB+rgkfyZp4J9yf9gun7NxuQaRfZkyjIrzDWuMR09L OWcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=4eb+w4uk7aJLzQQxH5rhpDw6sncgHKJoS/QEyZCZzxY=; b=SwpR2uBOX+Va+U9JYCDf0gWTiQFnRFJNWgiGK1hnuxJsqgM5P/ja6UVYpn0jUJjhra Kef+agensyec0gtxDtaz4BEVGk5Yjor88yCVWCm+4Sb9EZFoo+XatzGDJ+XL3fO02aBu jGBta9FJCFTC845n4VnsntoVn/Es5ZpKVehblrGwegwYNX7YDoD1VHwZXC9ry23lu3/9 w302bfdpzdTfPdHWp/egtEZ4Ktk/nEmVJyZEFxyb1k9AeMTTfLcqLBncwJc/rnb7vxM2 ncfCPF5HiYkHrGhItgWTeLyZdnYDe8zHBQvC3oJzA1PFHOecmA+Diwc+6ULJjM2pvJjC j9ZA== X-Gm-Message-State: AKwxytc8SLlimIaWNKpz4HisUfNMbUluUP4oUotiPq7+c8e6Mgrg+6a1 1o2ZmTN70kPDbxqeH37tIxXVKZ2t68cji/41CI3w14g0u1x8H51ooa/R54oU7CjdKAcPg9hPkuh wM56rM7kONWv+ulxJc7B8GJOT7rdaKPQocPUUFpl80qukvmuVAHNkfG78eKo+yZiKcD4dchlxis BkB2zKbkQg X-Google-Smtp-Source: ACJfBostptcHmp8uJc0aNZftZNVoBQ+ltt0TXxmsi7NOpU97i5hDAQO/wk+0GRVRMTx1DHsYYv6PAQ== X-Received: by 10.223.184.14 with SMTP id h14mr10544427wrf.31.1515810358769; Fri, 12 Jan 2018 18:25:58 -0800 (PST) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id 187sm7113219wmu.19.2018.01.12.18.25.57 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 18:25:57 -0800 (PST) Subject: Re: [PROPOSAL] Shared Ispell dictionaries To: Arthur Zakirov , pgsql-hackers References: <20171226164825.GA29922@zakirov.localdomain> <20171231152811.GA4233@arthur.localdomain> <20180107190526.GA27803@arthur.localdomain> From: Tomas Vondra Message-ID: Date: Sat, 13 Jan 2018 03:25:55 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: <20180107190526.GA27803@arthur.localdomain> Content-Type: text/plain; charset=windows-1252 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi Arthur, I've done some initial review of the patch today, and here are some thoughts: 0001-Fix-ispell-memory-handling-v2.patch This makes sense. The patch simply replaces two cpstrdup() calls with MemoryContextStrdup, but I see spell.c already has two macros to allocate memory in the buildCxt. What about adding tmpstrdup to copy a string into the context? I admit this is mostly nitpicking though. 0002-Retreive-shmem-location-for-ispell-v2.patch I think the GUC name should make it clear it's a maximum number of something, just like "max_parallel_workers" and other such GUCs. When I first saw "shared_dictionaries" in the patch I thought it's a list of dictionary names, or something like that. I have a bunch of additional design questions and proposals (not necessarily required for v1, but perhaps useful for shaping it). 1) Why do we actually need the limit? Is it really necessary / useful? When I wrote shared_ispell back in 2012, all we had were fixed segments allocated at start, and so similar limits were a built-in restriction. But after the DSM stuff was introduced I imagined it would not be necessary. I realize the current implementation requires that, because the hash table is still created in an old-style memory context (and only the dictionaries are in DSM segments). But that seems fairly straightforward to fix by maintaining the hash table in a separate DSM segment too. So lookup of the dictionary DSM would have to fist check what the current hash table segment is, and then continue as now. I'm not sure if dynahash can live in a DSM segment, but we already have a hash table that supports that in dshash.c (which is also concurrent, although I'm not sure if that's a major advantage for this use case). 2) Do we actually want/need some limits? Which ones? That is not to say we don't need/want some limits, but the current limit may not be the droid we're looking for, for a couple of reasons. Firstly, currently it only matters during startup, when the dynahash is created. So to change the limit (e.g. to increase it) you actually have to restart the database, which is obviously a major hassle. Secondly, dynahash tweaks the values to get proper behavior. For example it's not using the values directly but some higher value of 2^N form. Which means the limit may not enforced immediately when hitting the GUC, but unexpectedly somewhat later. And finally, I believe this is log-worthy - right now the dictionary load silently switches to backend memory (thus incurring all the parsing overhead). This certainly deserves at least a log message. Actually, I'm not sure "number of dictionaries" is a particularly useful limit in the first place - that's not a number I really care about. But I do care about amount of memory consumed by the loaded dictionaries. So I do suggest adding such "max memory for shared dictionaries" limit. I'm not sure we can enforce it strictly, because when deciding where to load the dict we haven't parsed it yet and so don't know how much memory will be required. But I believe a lazy check should be fine (load it, and if we exceeded the total memory disable loading additional ones). 3) How do I unload a dictionary from the shared memory? Assume we've reached the limit (it does not matter if it's the number of dictionaries or memory used by them). How do I resolve that without restarting the database? How do I unload a dictionary (which may be unused) from shared memory? ALTER TEXT SEARCH DICTIONARY x UNLOAD 4) How do I reload a dictionary? Assume I've updated the dictionary files (added new words into the files, or something like that). How do I reload the dictionary? Do I have to restart the server, DROP/CREATE everything again, or what? What about instead having something like this: ALTER TEXT SEARCH DICTIONARY x RELOAD 5) Actually, how do I list currently loaded dictionaries (and how much memory they use in the shared memory)? 6) What other restrictions would be useful? I think it should be possible to specify which ispell dictionaries may be loaded into shared memory, and which should be always loaded into local backend memory. That is, something like CREATE TEXT SEARCH DICTIONARY x ( TEMPLATE = ispell, DictFile = czech, AffFile = czech, StopWords = czech, SharedMemory = true/false (default: false) ); because otherwise the dictionaries will compete for shared memory, and it's unclear which of them will get loaded. For a server with a single application that may not be a huge issue, but think about servers shared by multiple applications, etc. In the extension this was achieved kinda explicitly by definition of a separate 'shared_ispell' template, but if you modify the current one that won't work, of course. 7) You mentioned you had to get rid of the compact_palloc0 - can you elaborate a bit why that was necessary? Also, when benchmarking the impact of this make sure to measure not only the time but also memory consumption. In fact, that was the main reason why Pavel implemented it in 2010, because the czech dictionary takes quite a bit of memory, and without the shared memory a copy was kept in every backend. Of course, maybe that would be mostly irrelevant thanks to this patch (due to changes to the representation and keeping just a single copy). 8) One more thing - I've noticed that the hash table uses this key: typedef struct { char dictfile[MAXPGPATH]; char afffile[MAXPGPATH]; } TsearchDictKey; That is, full paths to the two files, and I'm not sure that's a very good idea. Firstly, it's a bit wasteful (1kB per path). But more importantly it means all dictionaries referencing the same files will share the same chunk of shared memory - not only within a single database, but across the whole cluster. That may lead to surprising behavior, because e.g. unloading a dictionary in one database will affect dictionaries in all other databases referencing the same files. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services