Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tD746-00G0fr-VA for pgsql-general@arkaria.postgresql.org; Mon, 18 Nov 2024 19:05:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tD744-006iuH-5S for pgsql-general@arkaria.postgresql.org; Mon, 18 Nov 2024 19:05:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tD743-006iu8-Ow for pgsql-general@lists.postgresql.org; Mon, 18 Nov 2024 19:05:36 +0000 Received: from mail-wr1-x441.google.com ([2a00:1450:4864:20::441]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1tD741-002ZCw-H2 for pgsql-general@lists.postgresql.org; Mon, 18 Nov 2024 19:05:34 +0000 Received: by mail-wr1-x441.google.com with SMTP id ffacd0b85a97d-3824709ee03so943008f8f.2 for ; Mon, 18 Nov 2024 11:05:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=cybertec.at; t=1731956732; x=1732561532; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=DQLDePVNqqA3rg35nqXiU3czYLhr241ejgkdXPtyT+Q=; b=FkoazBkSm9jRFnwJoDrQUC9HbvaGhmfibCxdWNA5Rc6rDXJsVGBcGSmI5g8T+6Lni+ KMVaHxMlgYqz6a3PmRjUQ3qt3TsLfo9LCt4kJu76yeQHRgvj1oGzg5UnNfpHANXmJ8Rz B/UuS/PbK8fpBZAjo0haYC1WOjiItlq/eAZA4= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731956732; x=1732561532; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=DQLDePVNqqA3rg35nqXiU3czYLhr241ejgkdXPtyT+Q=; b=vVSw4e65UIpyGcsXvlNex742jApYZ97S3IJujDYJNC5qaok91vDTlLHns36fo68OZO navxibYxvBXqg0bOIjlCtiYXu2RK5/nuQcFqvU9DsFP0kgDo1wQtUIqVz2FMM/M25/sg KBpba3m2HxdYUG+e2pZ4SKbXlV9UZOrPIKJNza+vfTuI6hfjc7L2ELs4XJC4z8sW9jZP l/91RVcZG4EfeoclYtNKR6iRkoHGAGJKslFIKxchs7zUY9a6HISRh6nA9txI2ZpZPved UbQ5yKn73+d6/Uh3TgE9Y19Jrp0ZcwXDIljRDlSxHQR95+JV2OEtD5jmD2eQ+itOjIeU ECTQ== X-Forwarded-Encrypted: i=1; AJvYcCXu5iy9DoosLxYMQ0sv4nd0lNB2C4M5iTJ7zhlEBaW3+xss1x+PcTEHWD+ACooGFZKQ3Jsb44G+IumgqelL@lists.postgresql.org X-Gm-Message-State: AOJu0YzPJCGpC7MR8/ueDxWO3jB6cyeGpJXqz8EwHKEI0nYhePv6kH31 AGsIXHJXQy6ZDMYLH4BYKLSUwaeWs5bFAeekhNUS+3bPZbTQvAnf7473Gjme1kcSPnfrSuqLVsC G4Ydp5w== X-Google-Smtp-Source: AGHT+IGlsJE1iDMSRite6Y3SB7YsqF7RuP4mr0Dmj2Je//2uqCZxrD0LbWzQJaIYS9a0or94AvnYwg== X-Received: by 2002:a5d:6d8a:0:b0:382:4a84:67c with SMTP id ffacd0b85a97d-3824a8409b9mr1566837f8f.32.1731956731930; Mon, 18 Nov 2024 11:05:31 -0800 (PST) Received: from localhost.localdomain ([91.26.40.154]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-432dac0aca0sm164559915e9.29.2024.11.18.11.05.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Nov 2024 11:05:31 -0800 (PST) Message-ID: <61cb94a962667788c9c09107fa9937300e54d3cd.camel@cybertec.at> Subject: Re: Functions and Indexes From: Laurenz Albe To: Moreno Andreo , pgsql-general@lists.postgresql.org Date: Mon, 18 Nov 2024 20:05:31 +0100 In-Reply-To: <8dfd0bd5-87e5-4de7-9f60-8a680a32bc11@evolu-s.it> References: <2e36a2fe-b30f-46a5-937f-3b7d94e75cb6@evolu-s.it> <8dfd0bd5-87e5-4de7-9f60-8a680a32bc11@evolu-s.it> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.52.4 (3.52.4-2.fc40) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 2024-11-18 at 15:37 +0100, Moreno Andreo wrote: > I'm creating indexes for some tables, and I came across a doubt. >=20 > If a column appears in the WHERE clause (and so it should be placed in= =20 > index), in case it is "processed" in a function (see below), is it=20 > possible to insert this function to further narrow down things? >=20 > Common index: > SELECT foo1, foo2 FROM bar WHERE foo1 =3D 2 > CREATE index bar1_idx ON bar USING btree(foo1); >=20 > What about if query becomes > SELECT foo1, foo2 FROM bar WHERE (POSITION(foo1 IN 'blah blah') >0) You could create an index like CREATE INDEX ON bar (position(foo1 IN 'blah blah')); Alternatively, you could have a partial index: CREATE INDEX ON bar (foo1) INCLUDE (foo2) WHERE position(foo1 IN 'blah blah') > 0; > Second question: I 've seen contrasting opinions about putting JOIN=20 > parameters (ON a.field1 =3D b.field2) in an index and I'd like to know= =20 > your thoughts. That depends on the join strategy PostgreSQL chooses. You can use EXPLAIN to figure out the join strategy. This article should explain details: https://www.cybertec-postgresql.com/en/join-strategies-and-performance-in-p= ostgresql/ Yours, Laurenz Albe