<row>
<entry><literal><function>pg_get_serial_sequence(<parameter>table_name</parameter>, <parameter>column_name</parameter>)</function></literal></entry>
<entry><type>text</type></entry>
- <entry>get name of the sequence that a <type>serial</type>, <type>smallserial</type> or <type>bigserial</type> column
- uses</entry>
+ <entry>get name of the sequence that a serial or identity column uses</entry>
</row>
<row>
<entry><literal><function>pg_get_statisticsobjdef(<parameter>statobj_oid</parameter>)</function></literal></entry>
<para>
<function>pg_get_serial_sequence</function> returns the name of the
sequence associated with a column, or NULL if no sequence is associated
- with the column. The first input parameter is a table name with
- optional schema, and the second parameter is a column name. Because
- the first parameter is potentially a schema and table, it is not treated
- as a double-quoted identifier, meaning it is lower cased by default,
- while the second parameter, being just a column name, is treated as
- double-quoted and has its case preserved. The function returns a value
- suitably formatted for passing to sequence functions (see <xref
- linkend="functions-sequence">). This association can be modified or
- removed with <command>ALTER SEQUENCE OWNED BY</>. (The function
- probably should have been called
- <function>pg_get_owned_sequence</function>; its current name reflects the fact
- that it's typically used with <type>serial</> or <type>bigserial</>
- columns.)
+ with the column. If the column is an identity column, the associated
+ sequence is the sequence internally created for the identity column. For
+ columns created using one of the serial types
+ (<type>serial</type>, <type>smallserial</type>, <type>bigserial</type>), it
+ is the sequence created for that serial column definition. In the latter
+ case, this association can be modified or removed with <command>ALTER
+ SEQUENCE OWNED BY</>. (The function probably should have been called
+ <function>pg_get_owned_sequence</function>; its current name reflects the
+ fact that it has typically been used with <type>serial</>
+ or <type>bigserial</> columns.) The first input parameter is a table name
+ with optional schema, and the second parameter is a column name. Because
+ the first parameter is potentially a schema and table, it is not treated as
+ a double-quoted identifier, meaning it is lower cased by default, while the
+ second parameter, being just a column name, is treated as double-quoted and
+ has its case preserved. The function returns a value suitably formatted
+ for passing to sequence functions
+ (see <xref linkend="functions-sequence">). A typical use is in reading the
+ current value of a sequence for an identity or serial column, for example:
+<programlisting>
+SELECT currval(pg_get_serial_sequence('sometable', 'id'));
+</programlisting>
</para>
<para>
/*
* pg_get_serial_sequence
- * Get the name of the sequence used by a serial column,
+ * Get the name of the sequence used by an identity or serial column,
* formatted suitably for passing to setval, nextval or currval.
* First parameter is not treated as double-quoted, second parameter
* is --- see documentation for reason.
Form_pg_depend deprec = (Form_pg_depend) GETSTRUCT(tup);
/*
- * We assume any auto dependency of a sequence on a column must be
- * what we are looking for. (We need the relkind test because indexes
- * can also have auto dependencies on columns.)
+ * Look for an auto dependency (serial column) or internal dependency
+ * (identity column) of a sequence on a column. (We need the relkind
+ * test because indexes can also have auto dependencies on columns.)
*/
if (deprec->classid == RelationRelationId &&
deprec->objsubid == 0 &&
- deprec->deptype == DEPENDENCY_AUTO &&
+ (deprec->deptype == DEPENDENCY_AUTO ||
+ deprec->deptype == DEPENDENCY_INTERNAL) &&
get_rel_relkind(deprec->objid) == RELKIND_SEQUENCE)
{
sequenceId = deprec->objid;