pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: hubert depesz lubaczewski <depesz@depesz.com>
To: PostgreSQL mailing lists <pgsql-bugs@lists.postgresql.org>
Subject: Functions used by index don't need to be immutable?
Date: Tue, 11 Nov 2025 13:35:22 +0100
Message-ID: <aRMtismOfO86U9gS@depesz.com> (raw)

Hi,
while researching some blogpost I found that we can make index on
volatile functions.

Tested this code:

CREATE TABLE wiki_docs (
    id INT8 generated always as identity PRIMARY KEY,
    title TEXT,
    body TEXT
);
create function get_tsvector( IN the_row wiki_docs ) returns
tsvector as $$
    select
    setweight( to_tsvector('english', the_row.title), 'A')
    ||
    setweight( to_tsvector('english', the_row.body),
    'B');
$$ language sql;
create index the_magic_gist on wiki_docs using gist( get_tsvector(wiki_docs) );
create index the_magic_gin on wiki_docs using gin( get_tsvector(wiki_docs) );

Ran it in in every Pg from 10 to 19, and it worked.

postgres=# select provolatile from pg_proc where proname = 'get_tsvector';
 provolatile
-------------
 v
(1 row)

Which seems to contradict part of
https://www.postgresql.org/docs/18/sql-createindex.html
which says:

> All functions and operators used in an index definition must be
> “immutable”,

What am I misunderstanding and/or doing wrong?

Best regards,

depesz






view thread (3+ messages)  latest in thread

Message-ID: <aRMtismOfO86U9gS@depesz.com>
Permalink:  ../aRMtismOfO86U9gS@depesz.com/
Also on:    postgresql.org/message-id/aRMtismOfO86U9gS@depesz.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-bugs@postgresql.org
  Cc: depesz@depesz.com, pgsql-bugs@lists.postgresql.org
  Subject: Re: Functions used by index don't need to be immutable?
  In-Reply-To: <aRMtismOfO86U9gS@depesz.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox