/* * Get owned sequence, error if not exactly one. */ Oid getOwnedSequence(Oid relid, AttrNumber attnum) { List *seqlist = getOwnedSequences(relid, attnum); if (list_length(seqlist) > 1) elog(ERROR, "more than one owned sequence found"); else if (list_length(seqlist) < 1) elog(ERROR, "no owned sequence found"); return linitial_oid(seqlist); }
/* * GetTableDDLEvents takes in a relationId, and returns the list of DDL commands * needed to reconstruct the relation. These DDL commands are all palloced; and * include the table's schema definition, optional column storage and statistics * definitions, and index and constraint defitions. */ List * GetTableDDLEvents(Oid relationId) { List *tableDDLEventList = NIL; char tableType = 0; List *sequenceIdlist = getOwnedSequences(relationId); ListCell *sequenceIdCell; char *tableSchemaDef = NULL; char *tableColumnOptionsDef = NULL; char *schemaName = NULL; Oid schemaId = InvalidOid; Relation pgIndex = NULL; SysScanDesc scanDescriptor = NULL; ScanKeyData scanKey[1]; int scanKeyCount = 1; HeapTuple heapTuple = NULL; /* * Set search_path to NIL so that all objects outside of pg_catalog will be * schema-prefixed. pg_catalog will be added automatically when we call * PushOverrideSearchPath(), since we set addCatalog to true; */ OverrideSearchPath *overridePath = GetOverrideSearchPath(CurrentMemoryContext); overridePath->schemas = NIL; overridePath->addCatalog = true; PushOverrideSearchPath(overridePath); /* if foreign table, fetch extension and server definitions */ tableType = get_rel_relkind(relationId); if (tableType == RELKIND_FOREIGN_TABLE) { char *extensionDef = pg_get_extensiondef_string(relationId); char *serverDef = pg_get_serverdef_string(relationId); if (extensionDef != NULL) { tableDDLEventList = lappend(tableDDLEventList, extensionDef); } tableDDLEventList = lappend(tableDDLEventList, serverDef); } /* create schema if the table is not in the default namespace (public) */ schemaId = get_rel_namespace(relationId); schemaName = get_namespace_name(schemaId); if (strncmp(schemaName, "public", NAMEDATALEN) != 0) { StringInfo schemaNameDef = makeStringInfo(); appendStringInfo(schemaNameDef, CREATE_SCHEMA_COMMAND, schemaName); tableDDLEventList = lappend(tableDDLEventList, schemaNameDef->data); } /* create sequences if needed */ foreach(sequenceIdCell, sequenceIdlist) { Oid sequenceRelid = lfirst_oid(sequenceIdCell); char *sequenceDef = pg_get_sequencedef_string(sequenceRelid); tableDDLEventList = lappend(tableDDLEventList, sequenceDef); }