mirror of
https://github.com/postgres/postgres.git
synced 2025-07-30 11:03:19 +03:00
Updated the pg_get_constraintdef() to use conbin. Update pg_dump to use
pg_get_constraintdef() for >= 70400. Rod Taylor <rbt@rbt.ca>
This commit is contained in:
@ -3,7 +3,7 @@
|
|||||||
* back to source text
|
* back to source text
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.141 2003/05/28 16:03:59 tgl Exp $
|
* $Header: /cvsroot/pgsql/src/backend/utils/adt/ruleutils.c,v 1.142 2003/06/25 03:56:30 momjian Exp $
|
||||||
*
|
*
|
||||||
* This software is copyrighted by Jan Wieck - Hamburg.
|
* This software is copyrighted by Jan Wieck - Hamburg.
|
||||||
*
|
*
|
||||||
@ -894,6 +894,10 @@ pg_get_constraintdef(PG_FUNCTION_ARGS)
|
|||||||
{
|
{
|
||||||
Datum val;
|
Datum val;
|
||||||
bool isnull;
|
bool isnull;
|
||||||
|
char *conbin;
|
||||||
|
char *consrc;
|
||||||
|
Node *expr;
|
||||||
|
List *context;
|
||||||
|
|
||||||
/* Start off the constraint definition */
|
/* Start off the constraint definition */
|
||||||
/* The consrc for CHECK constraints always seems to be
|
/* The consrc for CHECK constraints always seems to be
|
||||||
@ -901,14 +905,39 @@ pg_get_constraintdef(PG_FUNCTION_ARGS)
|
|||||||
appendStringInfo(&buf, "CHECK ");
|
appendStringInfo(&buf, "CHECK ");
|
||||||
|
|
||||||
/* Fetch constraint source */
|
/* Fetch constraint source */
|
||||||
val = heap_getattr(tup, Anum_pg_constraint_consrc,
|
val = heap_getattr(tup, Anum_pg_constraint_conbin,
|
||||||
RelationGetDescr(conDesc), &isnull);
|
RelationGetDescr(conDesc), &isnull);
|
||||||
if (isnull)
|
if (isnull)
|
||||||
elog(ERROR, "pg_get_constraintdef: Null consrc for constraint %u",
|
elog(ERROR, "pg_get_constraintdef: Null consrc for constraint %u",
|
||||||
constraintId);
|
constraintId);
|
||||||
|
|
||||||
|
conbin = DatumGetCString(DirectFunctionCall1(textout, val));
|
||||||
|
expr = stringToNode(conbin);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* If top level is a List, assume it is an implicit-AND structure, and
|
||||||
|
* convert to explicit AND. This is needed for partial index
|
||||||
|
* predicates.
|
||||||
|
*/
|
||||||
|
if (expr && IsA(expr, List))
|
||||||
|
expr = (Node *) make_ands_explicit((List *) expr);
|
||||||
|
|
||||||
|
if (conForm->conrelid != InvalidOid)
|
||||||
|
/* It's a Relation */
|
||||||
|
context = deparse_context_for(get_rel_name(conForm->conrelid),
|
||||||
|
conForm->conrelid);
|
||||||
|
else
|
||||||
|
/*
|
||||||
|
* Since VARNOs aren't allowed in domain constraints, relation context
|
||||||
|
* isn't required as anything other than a shell.
|
||||||
|
*/
|
||||||
|
context = deparse_context_for(get_typname(conForm->contypid),
|
||||||
|
InvalidOid);
|
||||||
|
|
||||||
|
consrc = deparse_expression(expr, context, false, false);
|
||||||
|
|
||||||
/* Append the constraint source */
|
/* Append the constraint source */
|
||||||
appendStringInfoString(&buf, DatumGetCString(DirectFunctionCall1(textout, val)));
|
appendStringInfoString(&buf, consrc);
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
33
src/backend/utils/cache/lsyscache.c
vendored
33
src/backend/utils/cache/lsyscache.c
vendored
@ -7,7 +7,7 @@
|
|||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* Portions Copyright (c) 1994, Regents of the University of California
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.97 2003/06/24 23:14:46 momjian Exp $
|
* $Header: /cvsroot/pgsql/src/backend/utils/cache/lsyscache.c,v 1.98 2003/06/25 03:56:31 momjian Exp $
|
||||||
*
|
*
|
||||||
* NOTES
|
* NOTES
|
||||||
* Eventually, the index information should go through here, too.
|
* Eventually, the index information should go through here, too.
|
||||||
@ -1443,6 +1443,37 @@ get_typtype(Oid typid)
|
|||||||
return '\0';
|
return '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* get_typname
|
||||||
|
* Returns the name of a given type.
|
||||||
|
*
|
||||||
|
* Returns a palloc'd copy of the string, or NULL if no such relation.
|
||||||
|
*
|
||||||
|
* NOTE: since type name is not unique, be wary of code that uses this
|
||||||
|
* for anything except preparing error messages.
|
||||||
|
*/
|
||||||
|
char *
|
||||||
|
get_typname(Oid typid)
|
||||||
|
{
|
||||||
|
HeapTuple tp;
|
||||||
|
|
||||||
|
tp = SearchSysCache(TYPEOID,
|
||||||
|
ObjectIdGetDatum(typid),
|
||||||
|
0, 0, 0);
|
||||||
|
if (HeapTupleIsValid(tp))
|
||||||
|
{
|
||||||
|
Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp);
|
||||||
|
char *result;
|
||||||
|
|
||||||
|
result = pstrdup(NameStr(typtup->typname));
|
||||||
|
ReleaseSysCache(tp);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* get_typ_typrelid
|
* get_typ_typrelid
|
||||||
*
|
*
|
||||||
|
@ -12,7 +12,7 @@
|
|||||||
* by PostgreSQL
|
* by PostgreSQL
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* IDENTIFICATION
|
||||||
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.333 2003/06/11 16:29:42 tgl Exp $
|
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.334 2003/06/25 03:56:31 momjian Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -3303,10 +3303,17 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
|
|||||||
/*
|
/*
|
||||||
* Fetch and process CHECK constraints for the domain
|
* Fetch and process CHECK constraints for the domain
|
||||||
*/
|
*/
|
||||||
appendPQExpBuffer(chkquery, "SELECT conname, consrc "
|
if (g_fout->remoteVersion >= 70400)
|
||||||
"FROM pg_catalog.pg_constraint "
|
appendPQExpBuffer(chkquery, "SELECT conname,"
|
||||||
"WHERE contypid = '%s'::pg_catalog.oid",
|
"pg_catalog.pg_get_constraintdef(oid) AS consrc "
|
||||||
tinfo->oid);
|
"FROM pg_catalog.pg_constraint "
|
||||||
|
"WHERE contypid = '%s'::pg_catalog.oid",
|
||||||
|
tinfo->oid);
|
||||||
|
else
|
||||||
|
appendPQExpBuffer(chkquery, "SELECT conname, 'CHECK (' || consrc || ')'"
|
||||||
|
"FROM pg_catalog.pg_constraint "
|
||||||
|
"WHERE contypid = '%s'::pg_catalog.oid",
|
||||||
|
tinfo->oid);
|
||||||
|
|
||||||
res = PQexec(g_conn, chkquery->data);
|
res = PQexec(g_conn, chkquery->data);
|
||||||
if (!res ||
|
if (!res ||
|
||||||
@ -3326,7 +3333,7 @@ dumpOneDomain(Archive *fout, TypeInfo *tinfo)
|
|||||||
conname = PQgetvalue(res, i, PQfnumber(res, "conname"));
|
conname = PQgetvalue(res, i, PQfnumber(res, "conname"));
|
||||||
consrc = PQgetvalue(res, i, PQfnumber(res, "consrc"));
|
consrc = PQgetvalue(res, i, PQfnumber(res, "consrc"));
|
||||||
|
|
||||||
appendPQExpBuffer(q, "\n\tCONSTRAINT %s CHECK %s",
|
appendPQExpBuffer(q, "\n\tCONSTRAINT %s %s",
|
||||||
fmtId(conname), consrc);
|
fmtId(conname), consrc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -5257,8 +5264,29 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo)
|
|||||||
tbinfo->relname);
|
tbinfo->relname);
|
||||||
|
|
||||||
resetPQExpBuffer(query);
|
resetPQExpBuffer(query);
|
||||||
if (g_fout->remoteVersion >= 70300)
|
if (g_fout->remoteVersion >= 70400)
|
||||||
appendPQExpBuffer(query, "SELECT conname, consrc"
|
appendPQExpBuffer(query, "SELECT conname, "
|
||||||
|
" pg_catalog.pg_get_constraintdef(c1.oid) AS consrc "
|
||||||
|
" from pg_catalog.pg_constraint c1"
|
||||||
|
" where conrelid = '%s'::pg_catalog.oid "
|
||||||
|
" and contype = 'c' "
|
||||||
|
" and not exists "
|
||||||
|
" (select 1 from "
|
||||||
|
" pg_catalog.pg_constraint c2, "
|
||||||
|
" pg_catalog.pg_inherits i "
|
||||||
|
" where i.inhrelid = c1.conrelid "
|
||||||
|
" and (c2.conname = c1.conname "
|
||||||
|
" or (c2.conname[0] = '$' "
|
||||||
|
" and c1.conname[0] = '$')"
|
||||||
|
" )"
|
||||||
|
" and pg_catalog.pg_get_constraintdef(c2.oid) "
|
||||||
|
" = pg_catalog.pg_get_constraintdef(c1.oid) "
|
||||||
|
" and c2.conrelid = i.inhparent) "
|
||||||
|
" order by conname ",
|
||||||
|
tbinfo->oid);
|
||||||
|
else if (g_fout->remoteVersion >= 70300)
|
||||||
|
appendPQExpBuffer(query, "SELECT conname, "
|
||||||
|
" 'CHECK (' || consrc || ')'"
|
||||||
" from pg_catalog.pg_constraint c1"
|
" from pg_catalog.pg_constraint c1"
|
||||||
" where conrelid = '%s'::pg_catalog.oid "
|
" where conrelid = '%s'::pg_catalog.oid "
|
||||||
" and contype = 'c' "
|
" and contype = 'c' "
|
||||||
@ -5276,7 +5304,8 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo)
|
|||||||
" order by conname ",
|
" order by conname ",
|
||||||
tbinfo->oid);
|
tbinfo->oid);
|
||||||
else
|
else
|
||||||
appendPQExpBuffer(query, "SELECT rcname as conname, rcsrc as consrc"
|
appendPQExpBuffer(query, "SELECT rcname as conname,"
|
||||||
|
" 'CHECK (' || rcsrc || ')' as consrc"
|
||||||
" from pg_relcheck c1"
|
" from pg_relcheck c1"
|
||||||
" where rcrelid = '%s'::oid "
|
" where rcrelid = '%s'::oid "
|
||||||
" and not exists "
|
" and not exists "
|
||||||
@ -5321,7 +5350,7 @@ dumpOneTable(Archive *fout, TableInfo *tbinfo, TableInfo *g_tblinfo)
|
|||||||
if (name[0] != '$')
|
if (name[0] != '$')
|
||||||
appendPQExpBuffer(q, "CONSTRAINT %s ",
|
appendPQExpBuffer(q, "CONSTRAINT %s ",
|
||||||
fmtId(name));
|
fmtId(name));
|
||||||
appendPQExpBuffer(q, "CHECK (%s)", expr);
|
appendPQExpBuffer(q, "%s", expr);
|
||||||
}
|
}
|
||||||
PQclear(res2);
|
PQclear(res2);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* Portions Copyright (c) 1994, Regents of the University of California
|
||||||
*
|
*
|
||||||
* $Id: lsyscache.h,v 1.72 2003/06/24 23:14:49 momjian Exp $
|
* $Id: lsyscache.h,v 1.73 2003/06/25 03:56:31 momjian Exp $
|
||||||
*
|
*
|
||||||
*-------------------------------------------------------------------------
|
*-------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
@ -81,6 +81,7 @@ extern char get_typtype(Oid typid);
|
|||||||
extern Oid get_typ_typrelid(Oid typid);
|
extern Oid get_typ_typrelid(Oid typid);
|
||||||
extern Oid get_element_type(Oid typid);
|
extern Oid get_element_type(Oid typid);
|
||||||
extern Oid get_array_type(Oid typid);
|
extern Oid get_array_type(Oid typid);
|
||||||
|
extern char *get_typname(Oid relid);
|
||||||
extern void getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem);
|
extern void getTypeInputInfo(Oid type, Oid *typInput, Oid *typElem);
|
||||||
extern void getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
|
extern void getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
|
||||||
bool *typIsVarlena);
|
bool *typIsVarlena);
|
||||||
|
Reference in New Issue
Block a user