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 1q9ncf-0006uN-T4 for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jun 2023 14:06:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q9nce-0007Pi-9H for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jun 2023 14:06:48 +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 1q9nce-0007PY-00 for pgsql-hackers@lists.postgresql.org; Thu, 15 Jun 2023 14:06:48 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by magus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1q9nca-002Xo4-Tr for pgsql-hackers@lists.postgresql.org; Thu, 15 Jun 2023 14:06:47 +0000 Received: from [10.4.14.209] (unknown [93.174.131.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: y.sokolov@postgrespro.ru) by mail.postgrespro.ru (Postfix/465) with ESMTPSA id C9EA0E20C38; Thu, 15 Jun 2023 17:06:44 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mx2023; t=1686838004; bh=NGt1zWUb1OKMzomueRRxtSk8Je3YXyZOBwo1BxNuIkY=; h=Message-ID:Date:User-Agent:Subject:To:Cc:References:From: In-Reply-To:From; b=sI/nl2aDKIKs8Uz1larEsOQ1Ju7bqrMs73uWMV4Pqp3s+hC5/NyPJnaws/FqLCFcE ueIAi5atCj0QeKyWEZS7Q89Yadx9FBPJ7N2sZ+CB6bx80Ou0L2ceKmFGqTvADKYIh0 XSr1b2nXggVzK7MSpDG0XxBetbf7uM2AYAj7eqdaLjjyFpiKKzwoDTyHThJMacOphd jOR49xt4s5PeOJeu2y8DINJWAXMagql9AGtV5g7bbAs6KG+Ojc5HISz5YVuRjxA58l vQJ/FzyliM4IW4/NO7fV0huNWMpUxb5r5hVMv3uHulv4Ogr5ex8RZF1VVsKLLum+Az XTvrqYKm39Fww== Message-ID: <46a455f6-16f8-1c98-955f-140449004d72@postgrespro.ru> Date: Thu, 15 Jun 2023 17:06:44 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: When IMMUTABLE is not. Content-Language: en-US To: chap@anastigmatix.net, Tom Lane Cc: pgsql-hackers@lists.postgresql.org References: <389c986d-fbb4-c644-9280-db7836af7ca9@postgrespro.ru> <2111925.1686835298@sss.pgh.pa.us> <69234e25e34e68425f49630f79ef9991@anastigmatix.net> From: Yura Sokolov In-Reply-To: <69234e25e34e68425f49630f79ef9991@anastigmatix.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 15.06.2023 16:58, chap@anastigmatix.net пишет: > On 2023-06-15 09:21, Tom Lane wrote: >> Yura Sokolov writes: >>> not enough to be sure function doesn't manipulate data. >> >> Of course not.  It is the user's responsibility to mark functions >> properly. > > And also, isn't it the case that IMMUTABLE should mark a function, > not merely that "doesn't manipulate data", but whose return value > doesn't depend in any way on data (outside its own arguments)? > > The practice among PLs of choosing an SPI readonly flag based on > the IMMUTABLE/STABLE/VOLATILE declaration seems to be a sort of > peculiar heuristic, not something inherent in what that declaration > means to the optimizer. (And also influences what snapshot the > function is looking at, and therefore what it can see, which has > also struck me more as a tacked-on effect than something inherent > in the declaration's meaning.) Documentation disagrees: https://www.postgresql.org/docs/current/sql-createfunction.html#:~:text=IMMUTABLE%0ASTABLE%0AVOLATILE > |IMMUTABLE|indicates that the function cannot modify the database and always returns the same result when given the same argument values > |STABLE|indicates that the function cannot modify the database, and that within a single table scan it will consistently return the same result for the same argument values, but that its result could change across SQL statements. > |VOLATILE|indicates that the function value can change even within a single table scan, so no optimizations can be made... But note that any function that has side-effects must be classified volatile, even if its result is quite predictable, to prevent calls from being optimized away