# CREATE OR REPLACE FUNCTION get_default_value (text, text, text) RETURNS text 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;

-- 
  Jonathan Daugherty
  http://www.cprogrammer.org

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to [EMAIL PROTECTED]

Reply via email to