Header And Logo

PostgreSQL
| The world's most advanced open source database.

pg_upgrade_support.c

Go to the documentation of this file.
00001 /*
00002  *  pg_upgrade_support.c
00003  *
00004  *  server-side functions to set backend global variables
00005  *  to control oid and relfilenode assignment, and do other special
00006  *  hacks needed for pg_upgrade.
00007  *
00008  *  Copyright (c) 2010-2013, PostgreSQL Global Development Group
00009  *  contrib/pg_upgrade_support/pg_upgrade_support.c
00010  */
00011 
00012 #include "postgres.h"
00013 
00014 #include "catalog/namespace.h"
00015 #include "catalog/pg_type.h"
00016 #include "commands/extension.h"
00017 #include "miscadmin.h"
00018 #include "utils/array.h"
00019 #include "utils/builtins.h"
00020 
00021 /* THIS IS USED ONLY FOR PG >= 9.0 */
00022 
00023 #ifdef PG_MODULE_MAGIC
00024 PG_MODULE_MAGIC;
00025 #endif
00026 
00027 extern PGDLLIMPORT Oid binary_upgrade_next_pg_type_oid;
00028 extern PGDLLIMPORT Oid binary_upgrade_next_array_pg_type_oid;
00029 extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_type_oid;
00030 
00031 extern PGDLLIMPORT Oid binary_upgrade_next_heap_pg_class_oid;
00032 extern PGDLLIMPORT Oid binary_upgrade_next_index_pg_class_oid;
00033 extern PGDLLIMPORT Oid binary_upgrade_next_toast_pg_class_oid;
00034 
00035 extern PGDLLIMPORT Oid binary_upgrade_next_pg_enum_oid;
00036 extern PGDLLIMPORT Oid binary_upgrade_next_pg_authid_oid;
00037 
00038 Datum       set_next_pg_type_oid(PG_FUNCTION_ARGS);
00039 Datum       set_next_array_pg_type_oid(PG_FUNCTION_ARGS);
00040 Datum       set_next_toast_pg_type_oid(PG_FUNCTION_ARGS);
00041 
00042 Datum       set_next_heap_pg_class_oid(PG_FUNCTION_ARGS);
00043 Datum       set_next_index_pg_class_oid(PG_FUNCTION_ARGS);
00044 Datum       set_next_toast_pg_class_oid(PG_FUNCTION_ARGS);
00045 
00046 Datum       set_next_pg_enum_oid(PG_FUNCTION_ARGS);
00047 Datum       set_next_pg_authid_oid(PG_FUNCTION_ARGS);
00048 
00049 Datum       create_empty_extension(PG_FUNCTION_ARGS);
00050 
00051 PG_FUNCTION_INFO_V1(set_next_pg_type_oid);
00052 PG_FUNCTION_INFO_V1(set_next_array_pg_type_oid);
00053 PG_FUNCTION_INFO_V1(set_next_toast_pg_type_oid);
00054 
00055 PG_FUNCTION_INFO_V1(set_next_heap_pg_class_oid);
00056 PG_FUNCTION_INFO_V1(set_next_index_pg_class_oid);
00057 PG_FUNCTION_INFO_V1(set_next_toast_pg_class_oid);
00058 
00059 PG_FUNCTION_INFO_V1(set_next_pg_enum_oid);
00060 PG_FUNCTION_INFO_V1(set_next_pg_authid_oid);
00061 
00062 PG_FUNCTION_INFO_V1(create_empty_extension);
00063 
00064 
00065 Datum
00066 set_next_pg_type_oid(PG_FUNCTION_ARGS)
00067 {
00068     Oid         typoid = PG_GETARG_OID(0);
00069 
00070     binary_upgrade_next_pg_type_oid = typoid;
00071 
00072     PG_RETURN_VOID();
00073 }
00074 
00075 Datum
00076 set_next_array_pg_type_oid(PG_FUNCTION_ARGS)
00077 {
00078     Oid         typoid = PG_GETARG_OID(0);
00079 
00080     binary_upgrade_next_array_pg_type_oid = typoid;
00081 
00082     PG_RETURN_VOID();
00083 }
00084 
00085 Datum
00086 set_next_toast_pg_type_oid(PG_FUNCTION_ARGS)
00087 {
00088     Oid         typoid = PG_GETARG_OID(0);
00089 
00090     binary_upgrade_next_toast_pg_type_oid = typoid;
00091 
00092     PG_RETURN_VOID();
00093 }
00094 
00095 Datum
00096 set_next_heap_pg_class_oid(PG_FUNCTION_ARGS)
00097 {
00098     Oid         reloid = PG_GETARG_OID(0);
00099 
00100     binary_upgrade_next_heap_pg_class_oid = reloid;
00101 
00102     PG_RETURN_VOID();
00103 }
00104 
00105 Datum
00106 set_next_index_pg_class_oid(PG_FUNCTION_ARGS)
00107 {
00108     Oid         reloid = PG_GETARG_OID(0);
00109 
00110     binary_upgrade_next_index_pg_class_oid = reloid;
00111 
00112     PG_RETURN_VOID();
00113 }
00114 
00115 Datum
00116 set_next_toast_pg_class_oid(PG_FUNCTION_ARGS)
00117 {
00118     Oid         reloid = PG_GETARG_OID(0);
00119 
00120     binary_upgrade_next_toast_pg_class_oid = reloid;
00121 
00122     PG_RETURN_VOID();
00123 }
00124 
00125 Datum
00126 set_next_pg_enum_oid(PG_FUNCTION_ARGS)
00127 {
00128     Oid         enumoid = PG_GETARG_OID(0);
00129 
00130     binary_upgrade_next_pg_enum_oid = enumoid;
00131 
00132     PG_RETURN_VOID();
00133 }
00134 
00135 Datum
00136 set_next_pg_authid_oid(PG_FUNCTION_ARGS)
00137 {
00138     Oid         authoid = PG_GETARG_OID(0);
00139 
00140     binary_upgrade_next_pg_authid_oid = authoid;
00141     PG_RETURN_VOID();
00142 }
00143 
00144 Datum
00145 create_empty_extension(PG_FUNCTION_ARGS)
00146 {
00147     text       *extName = PG_GETARG_TEXT_PP(0);
00148     text       *schemaName = PG_GETARG_TEXT_PP(1);
00149     bool        relocatable = PG_GETARG_BOOL(2);
00150     text       *extVersion = PG_GETARG_TEXT_PP(3);
00151     Datum       extConfig;
00152     Datum       extCondition;
00153     List       *requiredExtensions;
00154 
00155     if (PG_ARGISNULL(4))
00156         extConfig = PointerGetDatum(NULL);
00157     else
00158         extConfig = PG_GETARG_DATUM(4);
00159 
00160     if (PG_ARGISNULL(5))
00161         extCondition = PointerGetDatum(NULL);
00162     else
00163         extCondition = PG_GETARG_DATUM(5);
00164 
00165     requiredExtensions = NIL;
00166     if (!PG_ARGISNULL(6))
00167     {
00168         ArrayType  *textArray = PG_GETARG_ARRAYTYPE_P(6);
00169         Datum      *textDatums;
00170         int         ndatums;
00171         int         i;
00172 
00173         deconstruct_array(textArray,
00174                           TEXTOID, -1, false, 'i',
00175                           &textDatums, NULL, &ndatums);
00176         for (i = 0; i < ndatums; i++)
00177         {
00178             text       *txtname = DatumGetTextPP(textDatums[i]);
00179             char       *extName = text_to_cstring(txtname);
00180             Oid         extOid = get_extension_oid(extName, false);
00181 
00182             requiredExtensions = lappend_oid(requiredExtensions, extOid);
00183         }
00184     }
00185 
00186     InsertExtensionTuple(text_to_cstring(extName),
00187                          GetUserId(),
00188                        get_namespace_oid(text_to_cstring(schemaName), false),
00189                          relocatable,
00190                          text_to_cstring(extVersion),
00191                          extConfig,
00192                          extCondition,
00193                          requiredExtensions);
00194 
00195     PG_RETURN_VOID();
00196 }