Subject: Trying to understand pg_get_expr()

From: Adrian Klaver <[email-masked]>
Date: Tue, 17 Mar 2026 12:31:43 -0700

Given:

select version();
                   version 

-----------------------------------------------
  PostgreSQL 17.9 (Ubuntu 17.9-1.pgdg24.04+1)

and:

CREATE TABLE default_test (
     id integer,
     fld_1 varchar DEFAULT 'test',
     fld_2 integer DEFAULT 0
);

Then:

SELECT
     adrelid::regclass,
     pg_typeof(pg_get_expr(adbin, adrelid)),
     pg_get_expr(adbin, adrelid)
FROM
     pg_attrdef
WHERE
     adrelid = 'default_test'::regclass;

  adrelid    | pg_typeof |        pg_get_expr
--------------+-----------+---------------------------
  default_test | text      | 'test'::character varying
  default_test | text      | 0

and:

SELECT
     adrelid::regclass,
     pg_typeof(pg_get_expr(adbin, adrelid)),
     pg_get_expr(adbin, adrelid)
FROM
     pg_attrdef
WHERE
     adrelid = 'default_test'::regclass
     AND pg_get_expr(adbin, adrelid) = '0';

adrelid    | pg_typeof | pg_get_expr
--------------+-----------+-------------
  default_test | text      | 0

SELECT
     adrelid::regclass,
     pg_typeof(pg_get_expr(adbin, adrelid)),
     pg_get_expr(adbin, adrelid)
FROM
     pg_attrdef
WHERE
     adrelid = 'default_test'::regclass
     AND pg_get_expr(adbin, adrelid) = 'test';

adrelid | pg_typeof | pg_get_expr
---------+-----------+-------------
(0 rows)

Why does the = 'test' not return anything?

-- 
Adrian Klaver
[email-masked]

---

From: Marcos Pegoraro <[email-masked]>
Date: Tue, 17 Mar 2026 17:08:54 -0300

> Why does the = 'test' not return anything?

for me pg_get_expr(adbin, adrelid) returns 'test'::character varying
so it differs from 'test'

regards
Marcos