pg_get_serial_sequence not working for manually set seq

From: Marcelo Fernandes <marcefern7(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: pg_get_serial_sequence not working for manually set seq
Date: 2025-04-22 01:22:50
Message-ID: CAM2F1VPo-mknp3DOy7cr3SsN3vw-W7M_vq4GK8yqw9ux=T7c3A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi folks,

I've been testing the pg_get_serial_sequence function and noticed that I can
only get reliable results when using a SERIAL or IDENTITY column.

However, shouldn't it work for manually set sequences too?

In the docs[0] we have that this function:

> Returns the name of the sequence associated with a column, or NULL if no
> sequence is associated with the column

But according to my test below, that does not hold for manually set sequences
on a column.

Is this expected behaviour?

Test:

-- Identity column ✓
DROP TABLE IF EXISTS foo CASCADE;
CREATE TABLE foo (id INT GENERATED ALWAYS AS IDENTITY);
SELECT pg_get_serial_sequence('foo', 'id');
-- pg_get_serial_sequence
-- ------------------------
-- public.foo_id_seq

-- Test with a serial column ✓
DROP TABLE IF EXISTS bar CASCADE;
CREATE TABLE bar (id SERIAL);
SELECT pg_get_serial_sequence('bar', 'id');
-- pg_get_serial_sequence
-- ------------------------
-- public.bar_id_seq

-- Manually set seq ✗
DROP TABLE IF EXISTS buzz CASCADE;
CREATE SEQUENCE seq;
CREATE TABLE buzz (id INTEGER);
ALTER TABLE buzz ALTER COLUMN id SET DEFAULT nextval('seq');
SELECT pg_get_serial_sequence('buzz', 'id');
-- No results
-- pg_get_serial_sequence
------------------------

[0] https://www.postgresql.org/docs/current/functions-info.html

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2025-04-22 01:33:46 Re: pg_get_serial_sequence not working for manually set seq
Previous Message Thiemo Kellner 2025-04-21 17:58:56 Re: Order of update