![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I am trying to track down a method of determining what a sequence name is for a SERIAL is in postgresql. For example, CREATE TABLE foo (id SERIAL PRIMARY KEY NOT NULL, bar TEXT); \d foo Table "public.foo" Column | Type | Modifiers --------+---------+----------------------------------------------------- id | integer | not null default nextval('public.foo_id_seq'::text) bar | text | Indexes: "foo_pkey" primary key, btree (id) Now, I have figured out how to get a list of all the sequences with: foo=> SELECT relname FROM pg_class WHERE relkind='S' AND relname !~ '^pg_'; relname ------------ foo_id_seq (1 row) I can find public.foo in pg_tables, but I am not sure how to relate pg_tables and pg_class in order to find the sequence for a specific field in public.foo. Can anyone point me in the right direction? I am trying to get out of thehabit of hard-coding the sequence names in my code. Now that I think of it, I am lacking 'public.' as well from my query. Ok, so how would I go about getting the sequence name for a SERIAL field on any given schema.table? I would like to build a function that would return this value if I pass it the schema and table (and fieldname is necessary) Thanks, |
#3
| |||
| |||
|
|
I figured out how to get this: foo=> SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'foo'); adsrc ------------------------------------ nextval('public.foo_id_seq'::text) (1 row) However, this will break as soon as I do this: foo=> CREATE SCHEMA x; CREATE SCHEMA foo=> CREATE TABLE x.foo (id SERIAL PRIMARY KEY NOT NULL, x TEXT); NOTICE: CREATE TABLE will create implicit sequence "foo_id_seq" for "serial" column "foo.id" NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "foo_pkey" for table "foo" CREATE TABLE foo=> SELECT adsrc FROM pg_attrdef WHERE adrelid = (SELECT oid FROM pg_class WHERE relname = 'foo'); ERROR: more than one row returned by a subquery used as an expression So, it was a nice attempt, but I am back to the need to of determining the sequence name using a schema and a table. |
#4
| |||
| |||
|
|
So, it was a nice attempt, but I am back to the need to of determining the sequence name using a schema and a table. |
#5
| |||
| |||
|
#6
| |||
| |||
|
|
Ok, so how would I go about getting the sequence name for a SERIAL field on any given schema.table? |
#7
| |||
| |||
|
|
Ok, so how would I go about getting the sequence name for a SERIAL field on any given schema.table? I would like to build a function that would return this value if I pass it the schema and table (and fieldname is necessary) |
#8
| |||
| |||
|
#9
| |||
| |||
|
|
# CREATE OR REPLACE FUNCTION get_default_value (text, text, text) RETURNStext AS ' # SELECT adsrc # FROM pg_attrdef, pg_class, pg_namespace, pg_attribute # WHERE # adrelid = pg_class.oid AND # pg_class.relnamespace = pg_namespace.oid AND # pg_attribute.attnum = pg_attrdef.adnum AND # pg_attribute.attrelid = pg_class.oid AND # pg_namespace.nspname = $1 AND # pg_class.relname = $2 AND # pg_attribute.attname = $3; # ' language sql; As per Tom's mention of pg_depend, here's something that seems to do the trick for the time being, assuming the column is a serial: -- get_sequence(schema_name, table_name, column_name) CREATE OR REPLACE FUNCTION get_sequence (text, text, text) RETURNS text AS ' SELECT seq.relname::text FROM pg_class src, pg_class seq, pg_namespace, pg_attribute, pg_depend WHERE pg_depend.refobjsubid = pg_attribute.attnum AND pg_depend.refobjid = src.oid AND seq.oid = pg_depend.objid AND src.relnamespace = pg_namespace.oid AND pg_attribute.attrelid = src.oid AND pg_namespace.nspname = $1 AND src.relname = $2 AND pg_attribute.attname = $3; ' language sql; |
#10
| |||
| |||
|
|
Thanks, this seems to work well. My goal is to actually create a php function that takes a result and returns the insert_id like mysql_insert_id() does, but without needing to know the sequence names and such. I would make a psql function, but I don't always have that option with some clients existing systems. |
![]() |
| Thread Tools | |
| Display Modes | |
| |