Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL bigserial & nextval

Tags:

postgresql

I've got a PgSQL 9.4.3 server setup and previously I was only using the public schema and for example I created a table like this:

CREATE TABLE ma_accessed_by_members_tracking (
    reference bigserial NOT NULL,
    ma_reference bigint NOT NULL,
    membership_reference bigint NOT NULL,
    date_accessed timestamp without time zone,
    points_awarded bigint NOT NULL
);

Using the Windows Program PgAdmin III I can see it created the proper information and sequence.

However I've recently added another schema called "test" to the same database and created the exact same table, just like before.

However this time I see:

CREATE TABLE test.ma_accessed_by_members_tracking
(
  reference bigint NOT NULL DEFAULT nextval('ma_accessed_by_members_tracking_reference_seq'::regclass),
  ma_reference bigint NOT NULL,
  membership_reference bigint NOT NULL,
  date_accessed timestamp without time zone,
  points_awarded bigint NOT NULL
);

My question / curiosity is why in a public schema the reference shows bigserial but in the test schema reference shows bigint with a nextval?

Both work as expected. I just do not understand why the difference in schema's would show different table creations. I realize that bigint and bigserial allow the same volume of ints to be used.

like image 453
Diemuzi Avatar asked Jun 24 '15 19:06

Diemuzi


People also ask

What is Bigserial in PostgreSQL?

SERIAL or BIGSERIAL SERIAL is an auto-incremented integer column that takes 4 bytes while BIGSERIAL is an auto-incremented bigint column taking 8 bytes. Behind the scenes, PostgreSQL will use a sequence generator to generate the SERIAL column values upon inserting a new ROW.

How do I round to 2 decimal places in PostgreSQL?

You must cast the value to be rounded to numeric to use the two-argument form of round . Just append ::numeric for the shorthand cast, like round(val::numeric,2) . to_char will round numbers for you as part of formatting.

What type is Bigserial?

BIGSERIAL data typeThe BIGSERIAL data type stores a sequential integer, of the BIGINT data type, that is assigned automatically by the database server when a new row is inserted.

Does PostgreSQL have auto increment?

By simply setting our id column as SERIAL with PRIMARY KEY attached, Postgres will handle all the complicated behind-the-scenes work and automatically increment our id column with a unique, primary key value for every INSERT .


1 Answers

Merely A Notational Convenience

According to the documentation on Serial Types, smallserial, serial, and bigserial are not true data types. Rather, they are a notation to create at once both sequence and column with default value pointing to that sequence.

I created test table on schema public. The command psql \d shows bigint column type. Maybe it's PgAdmin behavior ?

Update

I checked PgAdmin source code. In function pgColumn::GetDefinition() it scans table pg_depend for auto dependency and when found it - replaces bigint with bigserial to simulate original table create code.

like image 194
Tomasz Myrta Avatar answered Oct 12 '22 14:10

Tomasz Myrta