On 11/30/26 6:07 PM, Bruno Wolff III wrote:
I'm not sure if this is a bug or just something that surprised me.

I have jsonb data in a table and created a function index on an order number in the data. I expected all of the references to use jsonb data, but it is converting to text and using json.

https://www.postgresql.org/docs/17/functions-json.html

jsonb ->> text → text

Extracts JSON object field with the given key, as text.

'{"a":1,"b":2}'::json ->> 'b' → 2

select pg_typeof('{"a":1,"b":2}'::json ->> 'b');
 pg_typeof
-----------
 text


Seems you are getting what you asked for.



I'm using 18.3 on Fedora. (They are a few updates behind.)

Here is the table defintion and the index defintions:
CREATE TABLE cert_orders (
  order_data JSONB NOT NULL
    CONSTRAINT "Must have an orderNumber"
      CHECK (order_data->'GetOrderReport'->'orderNumber' IS NOT NULL)
);

CREATE UNIQUE INDEX ON cert_orders (
  (order_data->'GetOrderReport'->>'orderNumber'));
CREATE UNIQUE INDEX ON cert_orders (
  (order_data->'Cert'->0->'tbs_certificate'->'issuer'->>'common_name'),
  (order_data->'Cert'->0->'tbs_certificate'->>'serial_number'));
CREATE INDEX ON cert_orders USING GIN (order_data jsonb_path_ops);

Here is \d output for the table and indexes.
bruno=> \d cert_orders
             Table "public.cert_orders"
  Column   | Type  | Collation | Nullable | Default ------------ +-------+-----------+----------+---------
order_data | jsonb |           | not null | Indexes:
   "cert_orders_expr_expr1_idx" UNIQUE, btree ((((((order_data -> 'Cert'::text) -> 0) -> 'tbs_certificate'::text) -> 'issuer'::text) ->> 'common_name'::text), ((((order_data -> 'Cert'::text) -> 0) -> 'tbs_certificate'::text) ->> 'serial_number'::text))    "cert_orders_expr_idx" UNIQUE, btree (((order_data -> 'GetOrderReport'::text) ->> 'orderNumber'::text))
    "cert_orders_order_data_idx" gin (order_data jsonb_path_ops)
Check constraints:
   "Must have an orderNumber" CHECK (((order_data -> 'GetOrderReport'::text) -> 'orderNumber'::text) IS NOT NULL)




--
Adrian Klaver
[email protected]


Reply via email to