Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i9aje-0005Xb-9J for pgsql-hackers@arkaria.postgresql.org; Sun, 15 Sep 2019 20:03:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1i9ajc-0002KC-Uc for pgsql-hackers@arkaria.postgresql.org; Sun, 15 Sep 2019 20:03:00 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1i9ajc-0002Jx-GX for pgsql-hackers@lists.postgresql.org; Sun, 15 Sep 2019 20:03:00 +0000 Received: from mail-wr1-x444.google.com ([2a00:1450:4864:20::444]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1i9ajZ-0008Az-4D for pgsql-hackers@postgresql.org; Sun, 15 Sep 2019 20:02:59 +0000 Received: by mail-wr1-x444.google.com with SMTP id l11so36724484wrx.5 for ; Sun, 15 Sep 2019 13:02:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=uxROiCW8SZIPm5BNE3bg9IJwdZ9bgDNIZ+k0OuWg2KM=; b=Wm1uSApg7Ztqxf0AJVvk/5RjBdqrlz27zPVcx8mFPMwFNHJGCc3+X48WB8S0+oBpDo pxeISH5QjJEcs+jpCu9ZXoDD55F/F7ZPe5OfVuPdbXKmHdY4ZqfOAsPsMob4Ay8I2OdA MIedHrmucAAGRmOggYvhThDFXfOa9Lk4Hvm4+ut6J1OPNGMeIjFeBoxK4MHkr/sfhTDG LWpFpeQxeQWQJy8mKpOFuAomjUmSlTzZyuRbAH1tffK5cClaQW40XeEcX9ieGLTSANFo 1/JyTshEPrfN4ylWl8ebaK5EmvJeZrd4tVV+sNKTZQcrZmz+sMRf1Cz3z/Dmd0tSODD5 +drw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=uxROiCW8SZIPm5BNE3bg9IJwdZ9bgDNIZ+k0OuWg2KM=; b=UNsmK1jZeYjbR8wLlXjjOxNabWLEQ3gY/5//K7GMuFIrihoMc36WXPMeA6VN0Qh0UT YkALAUytrsgR4YKEztRX62ISD9AtYyPMG/GxfaIdYaz21H8tOtdfhsiodAaar3MjRW6Y vZ6CnDsAr/cfmZr6QzYG301ekykPSFYk1gvsnGyBt05Hq+nel56dBc9bnRiG1BWvi8DY YjCGURRwtF/iO+J9vSqQsKn5xTfmd3l/kXNVLPpq8iQcTgpoNu2zqW1DgyNwlSrniA/L RYck/eqGS694ctSDk21GN2RNtSye2kIUmSF/MausYeR37sXTzUSkbx5JKMfEKnwI/UWw Xbxw== X-Gm-Message-State: APjAAAWtPllVJTPfsOo04CwaUStsC5RKYXXOSNXeYD5SEuF161+JHFXx 20ayrXMsRJGlWNvQklitskgrUQ== X-Google-Smtp-Source: APXvYqwjqNdZg4DgAzsCpeNOwiGhV8Tf2EIYFxzvX/Y7FcNaMeayeMJLCorZ/8NUhUuqU2JESVfK1g== X-Received: by 2002:a5d:63ca:: with SMTP id c10mr11792013wrw.314.1568577775955; Sun, 15 Sep 2019 13:02:55 -0700 (PDT) Received: from localhost (ip-86-49-253-183.net.upcbroadband.cz. [86.49.253.183]) by smtp.gmail.com with ESMTPSA id 33sm44046539wra.41.2019.09.15.13.02.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Sep 2019 13:02:52 -0700 (PDT) Date: Sun, 15 Sep 2019 22:02:51 +0200 From: Tomas Vondra To: Tom Lane Cc: Arseny Sher , pgsql-hackers Subject: Re: (Re)building index using itself or another index of the same table Message-ID: <20190915200251.tusdmm6hrus5ngj2@development> References: <87d0g5d0tm.fsf@ars-thinkpad> <21174.1568300908@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <21174.1568300908@sss.pgh.pa.us> User-Agent: NeoMutt/20180716 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Thu, Sep 12, 2019 at 11:08:28AM -0400, Tom Lane wrote: >Arseny Sher writes: >> A problem of similar nature can be reproduced with the following >> stripped-down scenario: > >> CREATE TABLE pears(f1 int primary key, f2 int); >> INSERT INTO pears SELECT i, i+1 FROM generate_series(1, 100) i; >> CREATE OR REPLACE FUNCTION pears_f(i int) RETURNS int LANGUAGE SQL IMMUTABLE AS $$ >> SELECT f1 FROM pears WHERE pears.f2 = 42 >> $$; >> CREATE index ON pears ((pears_f(f1))); > >We've seen complaints about this sort of thing before, and rejected >them because, as you say, that function is NOT immutable. When you >lie to the system like that, you should not be surprised if things >break. > >> There is already a mechanism which prevents usage of indexes during >> reindex -- ReindexIsProcessingIndex et al. However, to the contrary of >> what index.c:3664 comment say, these protect only indexes on system >> catalogs, not user tables: the only real caller is genam.c. >> Attached patch extends it: the same check is added to >> get_relation_info. Also SetReindexProcessing is cocked in index_create >> to defend from index self usage during creation as in stripped example >> above. There are some other still unprotected callers of index_build; >> concurrent index creation doesn't need it because index is >> 'not indisvalid' during the build, and in RelationTruncateIndexes >> table is empty, so it looks like it can be omitted. > >I have exactly no faith that this fixes things enough to make such >cases supportable. And I have no interest in opening that can of >worms anyway. I'd rather put in some code to reject database >accesses in immutable functions. > Same here. My hunch is a non-trivaial fraction of applications using this "trick" is silently broken in various subtle ways. >> One might argue that function selecting from table can hardly be called >> immutable, and immutability is required for index expressions. However, >> if user is sure table contents doesn't change, why not? > >If the table contents never change, why are you doing VACUUM FULL on it? > It's possible the columns referenced by the index expression are not changing, but some additional columns are updated. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services