1212 *by PostgreSQL
1313 *
1414 * IDENTIFICATION
15- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.350 2003/09/23 23:31:52 tgl Exp $
15+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_dump.c,v 1.351 2003/09/27 15:34:06 wieck Exp $
1616 *
1717 *-------------------------------------------------------------------------
1818 */
@@ -3903,6 +3903,7 @@ dumpCasts(Archive *fout,
39033903PQExpBuffer query = createPQExpBuffer ();
39043904PQExpBuffer defqry = createPQExpBuffer ();
39053905PQExpBuffer delqry = createPQExpBuffer ();
3906+ PQExpBuffer castsig = createPQExpBuffer ();
39063907int ntups ;
39073908int i ;
39083909
@@ -3932,16 +3933,51 @@ dumpCasts(Archive *fout,
39323933char * castcontext = PQgetvalue (res ,i ,4 );
39333934int fidx = -1 ;
39343935const char * ((* deps )[]);
3936+ int source_idx ;
3937+ int target_idx ;
39353938
39363939if (strcmp (castfunc ,"0" )!= 0 )
39373940fidx = findFuncByOid (finfo ,numFuncs ,castfunc );
39383941
39393942/*
3940- * We treat the cast as being in the namespace of the underlying
3941- * function. This doesn't handle binary compatible casts. Where
3942- * should those go?
3943+ * As per discussion we dump casts if one or more of the underlying
3944+ * objects (the conversion function and the two data types) are not
3945+ * builtin AND if all of the non-builtin objects namespaces are
3946+ * included in the dump. Builtin meaning, the namespace name does
3947+ * not start with "pg_".
39433948 */
3944- if (fidx < 0 || !finfo [fidx ].pronamespace -> dump )
3949+ source_idx = findTypeByOid (tinfo ,numTypes ,castsource );
3950+ target_idx = findTypeByOid (tinfo ,numTypes ,casttarget );
3951+
3952+ /*
3953+ * Skip this cast if all objects are from pg_
3954+ */
3955+ if ((fidx < 0 || strncmp (finfo [fidx ].pronamespace -> nspname ,"pg_" ,3 )== 0 )&&
3956+ strncmp (tinfo [source_idx ].typnamespace -> nspname ,"pg_" ,3 )== 0 &&
3957+ strncmp (tinfo [target_idx ].typnamespace -> nspname ,"pg_" ,3 )== 0 )
3958+ continue ;
3959+
3960+ /*
3961+ * Skip cast if function isn't from pg_ and that namespace is
3962+ * not dumped.
3963+ */
3964+ if (fidx >=0 &&
3965+ strncmp (finfo [fidx ].pronamespace -> nspname ,"pg_" ,3 )!= 0 &&
3966+ !finfo [fidx ].pronamespace -> dump )
3967+ continue ;
3968+
3969+ /*
3970+ * Same for the Source type
3971+ */
3972+ if (strncmp (tinfo [source_idx ].typnamespace -> nspname ,"pg_" ,3 )!= 0 &&
3973+ !tinfo [source_idx ].typnamespace -> dump )
3974+ continue ;
3975+
3976+ /*
3977+ * and the target type.
3978+ */
3979+ if (strncmp (tinfo [target_idx ].typnamespace -> nspname ,"pg_" ,3 )!= 0 &&
3980+ !tinfo [target_idx ].typnamespace -> dump )
39453981continue ;
39463982
39473983/* Make a dependency to ensure function is dumped first */
@@ -3957,6 +3993,7 @@ dumpCasts(Archive *fout,
39573993
39583994resetPQExpBuffer (defqry );
39593995resetPQExpBuffer (delqry );
3996+ resetPQExpBuffer (castsig );
39603997
39613998appendPQExpBuffer (delqry ,"DROP CAST (%s AS %s);\n" ,
39623999getFormattedTypeName (castsource ,zeroAsNone ),
@@ -3978,9 +4015,13 @@ dumpCasts(Archive *fout,
39784015appendPQExpBuffer (defqry ," AS IMPLICIT" );
39794016appendPQExpBuffer (defqry ,";\n" );
39804017
4018+ appendPQExpBuffer (castsig ,"CAST (%s AS %s)" ,
4019+ getFormattedTypeName (castsource ,zeroAsNone ),
4020+ getFormattedTypeName (casttarget ,zeroAsNone ));
4021+
39814022ArchiveEntry (fout ,castoid ,
3982- format_function_signature ( & finfo [ fidx ], false) ,
3983- finfo [ fidx ]. pronamespace -> nspname ,"" ,
4023+ castsig -> data ,
4024+ tinfo [ source_idx ]. typnamespace -> nspname ,"" ,
39844025"CAST" ,deps ,
39854026defqry -> data ,delqry -> data ,
39864027NULL ,NULL ,NULL );
@@ -3991,6 +4032,7 @@ dumpCasts(Archive *fout,
39914032destroyPQExpBuffer (query );
39924033destroyPQExpBuffer (defqry );
39934034destroyPQExpBuffer (delqry );
4035+ destroyPQExpBuffer (castsig );
39944036}
39954037
39964038