* Initialize a space for temporary files that can be opened by other backends. * Other backends must attach to it before accessing it. Associate this * SharedFileSet with 'seg'. Any contained files will be deleted when the * last backend detaches. * * We can also use this interface if the temporary files are used only by * single backend but the files need to be opened and closed multiple
| 61 | * be deleted. |
| 62 | */ |
| 63 | void |
| 64 | SharedFileSetInit(SharedFileSet *fileset, dsm_segment *seg) |
| 65 | { |
| 66 | static uint32 counter = 0; |
| 67 | |
| 68 | SpinLockInit(&fileset->mutex); |
| 69 | fileset->refcnt = 1; |
| 70 | fileset->creator_pid = MyProcPid; |
| 71 | fileset->number = counter; |
| 72 | counter = (counter + 1) % INT_MAX; |
| 73 | |
| 74 | /* Capture the tablespace OIDs so that all backends agree on them. */ |
| 75 | PrepareTempTablespaces(); |
| 76 | fileset->ntablespaces = |
| 77 | GetTempTablespaces(&fileset->tablespaces[0], |
| 78 | lengthof(fileset->tablespaces)); |
| 79 | if (fileset->ntablespaces == 0) |
| 80 | { |
| 81 | /* If the GUC is empty, use current database's default tablespace */ |
| 82 | fileset->tablespaces[0] = MyDatabaseTableSpace; |
| 83 | fileset->ntablespaces = 1; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | int i; |
| 88 | |
| 89 | /* |
| 90 | * An entry of InvalidOid means use the default tablespace for the |
| 91 | * current database. Replace that now, to be sure that all users of |
| 92 | * the SharedFileSet agree on what to do. |
| 93 | */ |
| 94 | for (i = 0; i < fileset->ntablespaces; i++) |
| 95 | { |
| 96 | if (fileset->tablespaces[i] == InvalidOid) |
| 97 | fileset->tablespaces[i] = MyDatabaseTableSpace; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /* Register our cleanup callback. */ |
| 102 | if (seg) |
| 103 | on_dsm_detach(seg, SharedFileSetOnDetach, PointerGetDatum(fileset)); |
| 104 | else |
| 105 | { |
| 106 | static bool registered_cleanup = false; |
| 107 | |
| 108 | if (!registered_cleanup) |
| 109 | { |
| 110 | /* |
| 111 | * We must not have registered any fileset before registering the |
| 112 | * fileset clean up. |
| 113 | */ |
| 114 | Assert(filesetlist == NIL); |
| 115 | on_proc_exit(SharedFileSetDeleteOnProcExit, 0); |
| 116 | registered_cleanup = true; |
| 117 | } |
| 118 | |
| 119 | filesetlist = lcons((void *) fileset, filesetlist); |
| 120 | } |
no test coverage detected