pg_tiktoken icon indicating copy to clipboard operation
pg_tiktoken copied to clipboard

Mark tiktoken functions as IMMUTABLE by default

Open nick-inkeep opened this issue 1 year ago • 1 comments

Description

When using tiktoken_count in a generated column on managed Postgres instances (like Neon), we encounter:

ERROR: generation expression is not immutable

Since tokenization is deterministic (same input → same output), marking these functions as IMMUTABLE in the extension itself would allow direct use in generated columns without requiring superuser privileges.

Example Use Case

CREATE TABLE documents (
    content text NOT NULL,
    token_count integer GENERATED ALWAYS AS (tiktoken_count('cl100k_base', content)) STORED
);

Benefits

  • Works out of the box on managed Postgres instances
  • Enables use in generated columns
  • Follows Postgres best practices for deterministic functions

nick-inkeep avatar Jan 05 '25 07:01 nick-inkeep

Sorry, missed that issue. Makes a lot of sense.

kelvich avatar Oct 15 '25 19:10 kelvich