pg_query 2.0.3 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (557) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +165 -0
  3. data/README.md +67 -29
  4. data/Rakefile +8 -23
  5. data/ext/pg_query/extconf.rb +21 -3
  6. data/ext/pg_query/include/pg_query.h +29 -4
  7. data/ext/pg_query/include/pg_query_enum_defs.c +551 -272
  8. data/ext/pg_query/include/pg_query_fingerprint_conds.c +563 -470
  9. data/ext/pg_query/include/pg_query_fingerprint_defs.c +5403 -3945
  10. data/ext/pg_query/include/pg_query_outfuncs_conds.c +402 -330
  11. data/ext/pg_query/include/pg_query_outfuncs_defs.c +1319 -1059
  12. data/ext/pg_query/include/pg_query_readfuncs_conds.c +141 -118
  13. data/ext/pg_query/include/pg_query_readfuncs_defs.c +1685 -1379
  14. data/ext/pg_query/include/{access → postgres/access}/amapi.h +47 -1
  15. data/ext/pg_query/include/{access → postgres/access}/attmap.h +5 -3
  16. data/ext/pg_query/include/{access → postgres/access}/attnum.h +2 -2
  17. data/ext/pg_query/include/{access → postgres/access}/clog.h +4 -2
  18. data/ext/pg_query/include/{access → postgres/access}/commit_ts.h +6 -9
  19. data/ext/pg_query/include/{access → postgres/access}/detoast.h +1 -11
  20. data/ext/pg_query/include/{access → postgres/access}/genam.h +21 -16
  21. data/ext/pg_query/include/{access → postgres/access}/gin.h +17 -4
  22. data/ext/pg_query/include/{access → postgres/access}/htup.h +1 -1
  23. data/ext/pg_query/include/{access → postgres/access}/htup_details.h +80 -88
  24. data/ext/pg_query/include/{access → postgres/access}/itup.h +61 -52
  25. data/ext/pg_query/include/{access → postgres/access}/parallel.h +2 -2
  26. data/ext/pg_query/include/{access → postgres/access}/printtup.h +1 -1
  27. data/ext/pg_query/include/{access → postgres/access}/relation.h +1 -1
  28. data/ext/pg_query/include/{access → postgres/access}/relscan.h +17 -2
  29. data/ext/pg_query/include/postgres/access/rmgr.h +62 -0
  30. data/ext/pg_query/include/{access → postgres/access}/rmgrlist.h +24 -24
  31. data/ext/pg_query/include/{access → postgres/access}/sdir.h +12 -3
  32. data/ext/pg_query/include/{access → postgres/access}/skey.h +1 -1
  33. data/ext/pg_query/include/{access → postgres/access}/stratnum.h +4 -2
  34. data/ext/pg_query/include/{access → postgres/access}/sysattr.h +1 -1
  35. data/ext/pg_query/include/{access → postgres/access}/table.h +2 -1
  36. data/ext/pg_query/include/{access → postgres/access}/tableam.h +337 -62
  37. data/ext/pg_query/include/postgres/access/toast_compression.h +73 -0
  38. data/ext/pg_query/include/{access → postgres/access}/transam.h +123 -13
  39. data/ext/pg_query/include/postgres/access/tsmapi.h +82 -0
  40. data/ext/pg_query/include/{access → postgres/access}/tupconvert.h +5 -2
  41. data/ext/pg_query/include/{access → postgres/access}/tupdesc.h +2 -2
  42. data/ext/pg_query/include/{access → postgres/access}/tupmacs.h +60 -100
  43. data/ext/pg_query/include/{access → postgres/access}/twophase.h +5 -1
  44. data/ext/pg_query/include/{access → postgres/access}/xact.h +99 -32
  45. data/ext/pg_query/include/{access → postgres/access}/xlog.h +69 -165
  46. data/ext/pg_query/include/{access → postgres/access}/xlog_internal.h +147 -73
  47. data/ext/pg_query/include/postgres/access/xlogbackup.h +41 -0
  48. data/ext/pg_query/include/{access → postgres/access}/xlogdefs.h +13 -40
  49. data/ext/pg_query/include/postgres/access/xlogprefetcher.h +55 -0
  50. data/ext/pg_query/include/{access → postgres/access}/xlogreader.h +154 -37
  51. data/ext/pg_query/include/{access → postgres/access}/xlogrecord.h +34 -13
  52. data/ext/pg_query/include/postgres/access/xlogrecovery.h +158 -0
  53. data/ext/pg_query/include/postgres/archive/archive_module.h +59 -0
  54. data/ext/pg_query/include/{c.h → postgres/c.h} +245 -188
  55. data/ext/pg_query/include/{catalog → postgres/catalog}/catalog.h +6 -3
  56. data/ext/pg_query/include/{catalog → postgres/catalog}/catversion.h +6 -2
  57. data/ext/pg_query/include/{catalog → postgres/catalog}/dependency.h +14 -19
  58. data/ext/pg_query/include/postgres/catalog/genbki.h +143 -0
  59. data/ext/pg_query/include/{catalog → postgres/catalog}/index.h +20 -5
  60. data/ext/pg_query/include/postgres/catalog/indexing.h +54 -0
  61. data/ext/pg_query/include/{catalog → postgres/catalog}/namespace.h +5 -3
  62. data/ext/pg_query/include/{catalog → postgres/catalog}/objectaccess.h +73 -3
  63. data/ext/pg_query/include/{catalog → postgres/catalog}/objectaddress.h +12 -7
  64. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_aggregate.h +14 -10
  65. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_aggregate_d.h +2 -1
  66. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_am.h +4 -1
  67. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_am_d.h +3 -1
  68. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_attribute.h +45 -26
  69. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_attribute_d.h +19 -16
  70. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_authid.h +7 -2
  71. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_authid_d.h +19 -9
  72. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_class.h +45 -15
  73. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_class_d.h +31 -2
  74. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_collation.h +35 -8
  75. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_collation_d.h +21 -3
  76. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_constraint.h +39 -13
  77. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_constraint_d.h +10 -4
  78. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_control.h +13 -5
  79. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_conversion.h +8 -5
  80. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_conversion_d.h +4 -1
  81. data/ext/pg_query/include/postgres/catalog/pg_database.h +124 -0
  82. data/ext/pg_query/include/postgres/catalog/pg_database_d.h +52 -0
  83. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_depend.h +11 -7
  84. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_depend_d.h +3 -1
  85. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_event_trigger.h +9 -3
  86. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_event_trigger_d.h +3 -1
  87. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_index.h +17 -7
  88. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_index_d.h +20 -17
  89. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_language.h +10 -5
  90. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_language_d.h +3 -1
  91. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_namespace.h +7 -2
  92. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_namespace_d.h +3 -1
  93. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_opclass.h +8 -5
  94. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_opclass_d.h +3 -1
  95. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_operator.h +21 -16
  96. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_operator_d.h +37 -1
  97. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_opfamily.h +8 -4
  98. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_opfamily_d.h +6 -2
  99. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_partitioned_table.h +20 -9
  100. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_partitioned_table_d.h +2 -1
  101. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_proc.h +20 -11
  102. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_proc_d.h +10 -8
  103. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_publication.h +49 -6
  104. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_publication_d.h +3 -1
  105. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_replication_origin.h +6 -1
  106. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_replication_origin_d.h +5 -1
  107. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_statistic.h +19 -12
  108. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_statistic_d.h +2 -1
  109. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_statistic_ext.h +19 -5
  110. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_statistic_ext_d.h +7 -2
  111. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_transform.h +8 -5
  112. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_transform_d.h +3 -1
  113. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_trigger.h +24 -8
  114. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_trigger_d.h +4 -1
  115. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_config.h +6 -3
  116. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_config_d.h +3 -1
  117. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_dict.h +8 -3
  118. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_dict_d.h +3 -1
  119. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_parser.h +6 -3
  120. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_parser_d.h +3 -1
  121. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_template.h +6 -3
  122. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_ts_template_d.h +3 -1
  123. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_type.h +56 -24
  124. data/ext/pg_query/include/{catalog → postgres/catalog}/pg_type_d.h +70 -31
  125. data/ext/pg_query/include/{catalog → postgres/catalog}/storage.h +9 -7
  126. data/ext/pg_query/include/{commands → postgres/commands}/async.h +4 -5
  127. data/ext/pg_query/include/{commands → postgres/commands}/dbcommands.h +3 -1
  128. data/ext/pg_query/include/{commands → postgres/commands}/defrem.h +12 -24
  129. data/ext/pg_query/include/{commands → postgres/commands}/event_trigger.h +2 -2
  130. data/ext/pg_query/include/{commands → postgres/commands}/explain.h +3 -1
  131. data/ext/pg_query/include/{commands → postgres/commands}/prepare.h +1 -1
  132. data/ext/pg_query/include/{commands → postgres/commands}/tablespace.h +6 -4
  133. data/ext/pg_query/include/{commands → postgres/commands}/trigger.h +36 -25
  134. data/ext/pg_query/include/{commands → postgres/commands}/user.h +10 -4
  135. data/ext/pg_query/include/{commands → postgres/commands}/vacuum.h +140 -47
  136. data/ext/pg_query/include/postgres/common/cryptohash.h +39 -0
  137. data/ext/pg_query/include/{common → postgres/common}/file_perm.h +4 -4
  138. data/ext/pg_query/include/{common → postgres/common}/hashfn.h +1 -1
  139. data/ext/pg_query/include/postgres/common/int.h +437 -0
  140. data/ext/pg_query/include/{common → postgres/common}/keywords.h +2 -6
  141. data/ext/pg_query/include/{common → postgres/common}/kwlookup.h +2 -2
  142. data/ext/pg_query/include/postgres/common/pg_prng.h +61 -0
  143. data/ext/pg_query/include/{common → postgres/common}/relpath.h +21 -14
  144. data/ext/pg_query/include/postgres/common/scram-common.h +70 -0
  145. data/ext/pg_query/include/postgres/common/sha2.h +32 -0
  146. data/ext/pg_query/include/postgres/common/string.h +44 -0
  147. data/ext/pg_query/include/postgres/common/unicode_east_asian_fw_table.h +125 -0
  148. data/ext/pg_query/include/{common/unicode_combining_table.h → postgres/common/unicode_nonspacing_table.h} +138 -8
  149. data/ext/pg_query/include/postgres/copyfuncs.funcs.c +5013 -0
  150. data/ext/pg_query/include/postgres/copyfuncs.switch.c +938 -0
  151. data/ext/pg_query/include/{datatype → postgres/datatype}/timestamp.h +50 -4
  152. data/ext/pg_query/include/postgres/equalfuncs.funcs.c +3097 -0
  153. data/ext/pg_query/include/postgres/equalfuncs.switch.c +785 -0
  154. data/ext/pg_query/include/{executor → postgres/executor}/execdesc.h +1 -1
  155. data/ext/pg_query/include/{executor → postgres/executor}/executor.h +98 -32
  156. data/ext/pg_query/include/{executor → postgres/executor}/functions.h +17 -3
  157. data/ext/pg_query/include/{executor → postgres/executor}/instrument.h +33 -16
  158. data/ext/pg_query/include/{executor → postgres/executor}/spi.h +42 -4
  159. data/ext/pg_query/include/{executor → postgres/executor}/tablefunc.h +1 -1
  160. data/ext/pg_query/include/{executor → postgres/executor}/tuptable.h +18 -11
  161. data/ext/pg_query/include/{fmgr.h → postgres/fmgr.h} +33 -8
  162. data/ext/pg_query/include/postgres/foreign/fdwapi.h +294 -0
  163. data/ext/pg_query/include/{funcapi.h → postgres/funcapi.h} +22 -10
  164. data/ext/pg_query/include/postgres/gram.h +1127 -0
  165. data/ext/pg_query/include/{parser → postgres}/gramparse.h +4 -4
  166. data/ext/pg_query/include/{jit → postgres/jit}/jit.h +12 -12
  167. data/ext/pg_query/include/postgres/kwlist_d.h +1119 -0
  168. data/ext/pg_query/include/postgres/lib/dshash.h +115 -0
  169. data/ext/pg_query/include/{lib → postgres/lib}/ilist.h +454 -22
  170. data/ext/pg_query/include/{lib → postgres/lib}/pairingheap.h +1 -1
  171. data/ext/pg_query/include/{lib → postgres/lib}/simplehash.h +158 -33
  172. data/ext/pg_query/include/postgres/lib/sort_template.h +432 -0
  173. data/ext/pg_query/include/{lib → postgres/lib}/stringinfo.h +3 -3
  174. data/ext/pg_query/include/{libpq → postgres/libpq}/auth.h +12 -4
  175. data/ext/pg_query/include/{libpq → postgres/libpq}/crypt.h +5 -4
  176. data/ext/pg_query/include/{libpq → postgres/libpq}/hba.h +54 -8
  177. data/ext/pg_query/include/{libpq → postgres/libpq}/libpq-be.h +45 -17
  178. data/ext/pg_query/include/{libpq → postgres/libpq}/libpq.h +31 -20
  179. data/ext/pg_query/include/{libpq → postgres/libpq}/pqcomm.h +26 -71
  180. data/ext/pg_query/include/{libpq → postgres/libpq}/pqformat.h +2 -2
  181. data/ext/pg_query/include/{libpq → postgres/libpq}/pqsignal.h +25 -13
  182. data/ext/pg_query/include/postgres/libpq/sasl.h +136 -0
  183. data/ext/pg_query/include/postgres/libpq/scram.h +37 -0
  184. data/ext/pg_query/include/{mb → postgres/mb}/pg_wchar.h +125 -25
  185. data/ext/pg_query/include/{mb → postgres/mb}/stringinfo_mb.h +1 -1
  186. data/ext/pg_query/include/{miscadmin.h → postgres/miscadmin.h} +96 -65
  187. data/ext/pg_query/include/{nodes → postgres/nodes}/bitmapset.h +11 -7
  188. data/ext/pg_query/include/{nodes → postgres/nodes}/execnodes.h +351 -103
  189. data/ext/pg_query/include/{nodes → postgres/nodes}/extensible.h +8 -4
  190. data/ext/pg_query/include/{nodes → postgres/nodes}/lockoptions.h +1 -1
  191. data/ext/pg_query/include/{nodes → postgres/nodes}/makefuncs.h +19 -6
  192. data/ext/pg_query/include/{nodes → postgres/nodes}/memnodes.h +11 -6
  193. data/ext/pg_query/include/postgres/nodes/miscnodes.h +56 -0
  194. data/ext/pg_query/include/{nodes → postgres/nodes}/nodeFuncs.h +89 -29
  195. data/ext/pg_query/include/{nodes → postgres/nodes}/nodes.h +100 -496
  196. data/ext/pg_query/include/postgres/nodes/nodetags.h +471 -0
  197. data/ext/pg_query/include/{nodes → postgres/nodes}/params.h +3 -3
  198. data/ext/pg_query/include/{nodes → postgres/nodes}/parsenodes.h +678 -207
  199. data/ext/pg_query/include/{nodes → postgres/nodes}/pathnodes.h +1282 -454
  200. data/ext/pg_query/include/{nodes → postgres/nodes}/pg_list.h +103 -73
  201. data/ext/pg_query/include/{nodes → postgres/nodes}/plannodes.h +474 -133
  202. data/ext/pg_query/include/{nodes → postgres/nodes}/primnodes.h +754 -254
  203. data/ext/pg_query/include/{nodes → postgres/nodes}/print.h +1 -1
  204. data/ext/pg_query/include/postgres/nodes/queryjumble.h +86 -0
  205. data/ext/pg_query/include/postgres/nodes/replnodes.h +111 -0
  206. data/ext/pg_query/include/postgres/nodes/supportnodes.h +346 -0
  207. data/ext/pg_query/include/{nodes → postgres/nodes}/tidbitmap.h +1 -1
  208. data/ext/pg_query/include/postgres/nodes/value.h +90 -0
  209. data/ext/pg_query/include/{optimizer → postgres/optimizer}/cost.h +14 -5
  210. data/ext/pg_query/include/{optimizer → postgres/optimizer}/geqo.h +9 -7
  211. data/ext/pg_query/include/{optimizer → postgres/optimizer}/geqo_gene.h +1 -1
  212. data/ext/pg_query/include/{optimizer → postgres/optimizer}/optimizer.h +31 -28
  213. data/ext/pg_query/include/{optimizer → postgres/optimizer}/paths.h +29 -12
  214. data/ext/pg_query/include/{optimizer → postgres/optimizer}/planmain.h +15 -17
  215. data/ext/pg_query/include/{parser → postgres/parser}/analyze.h +20 -5
  216. data/ext/pg_query/include/postgres/parser/kwlist.h +498 -0
  217. data/ext/pg_query/include/{parser → postgres/parser}/parse_agg.h +5 -8
  218. data/ext/pg_query/include/{parser → postgres/parser}/parse_coerce.h +6 -1
  219. data/ext/pg_query/include/{parser → postgres/parser}/parse_expr.h +2 -3
  220. data/ext/pg_query/include/{parser → postgres/parser}/parse_func.h +2 -1
  221. data/ext/pg_query/include/{parser → postgres/parser}/parse_node.h +41 -11
  222. data/ext/pg_query/include/{parser → postgres/parser}/parse_oper.h +3 -5
  223. data/ext/pg_query/include/{parser → postgres/parser}/parse_relation.h +11 -5
  224. data/ext/pg_query/include/{parser → postgres/parser}/parse_type.h +4 -3
  225. data/ext/pg_query/include/postgres/parser/parser.h +68 -0
  226. data/ext/pg_query/include/{parser → postgres/parser}/parsetree.h +1 -1
  227. data/ext/pg_query/include/{parser → postgres/parser}/scanner.h +2 -2
  228. data/ext/pg_query/include/{parser → postgres/parser}/scansup.h +2 -5
  229. data/ext/pg_query/include/{partitioning → postgres/partitioning}/partdefs.h +1 -1
  230. data/ext/pg_query/include/{pg_config.h → postgres/pg_config.h} +216 -228
  231. data/ext/pg_query/include/{pg_config_manual.h → postgres/pg_config_manual.h} +80 -58
  232. data/ext/pg_query/include/postgres/pg_config_os.h +8 -0
  233. data/ext/pg_query/include/{pg_getopt.h → postgres/pg_getopt.h} +6 -6
  234. data/ext/pg_query/include/{pg_trace.h → postgres/pg_trace.h} +1 -1
  235. data/ext/pg_query/include/postgres/pgstat.h +778 -0
  236. data/ext/pg_query/include/{pgtime.h → postgres/pgtime.h} +16 -6
  237. data/ext/pg_query/include/{pl_gram.h → postgres/pl_gram.h} +116 -116
  238. data/ext/pg_query/include/{pl_reserved_kwlist.h → postgres/pl_reserved_kwlist.h} +1 -1
  239. data/ext/pg_query/include/{pl_reserved_kwlist_d.h → postgres/pl_reserved_kwlist_d.h} +10 -10
  240. data/ext/pg_query/include/{pl_unreserved_kwlist.h → postgres/pl_unreserved_kwlist.h} +3 -3
  241. data/ext/pg_query/include/{pl_unreserved_kwlist_d.h → postgres/pl_unreserved_kwlist_d.h} +60 -60
  242. data/ext/pg_query/include/{plerrcodes.h → postgres/plerrcodes.h} +9 -1
  243. data/ext/pg_query/include/{plpgsql.h → postgres/plpgsql.h} +79 -86
  244. data/ext/pg_query/include/{port → postgres/port}/atomics/arch-arm.h +9 -3
  245. data/ext/pg_query/include/postgres/port/atomics/arch-hppa.h +17 -0
  246. data/ext/pg_query/include/{port → postgres/port}/atomics/arch-ppc.h +21 -21
  247. data/ext/pg_query/include/{port → postgres/port}/atomics/arch-x86.h +2 -2
  248. data/ext/pg_query/include/{port → postgres/port}/atomics/fallback.h +3 -3
  249. data/ext/pg_query/include/{port → postgres/port}/atomics/generic-gcc.h +3 -3
  250. data/ext/pg_query/include/postgres/port/atomics/generic-msvc.h +101 -0
  251. data/ext/pg_query/include/postgres/port/atomics/generic-sunpro.h +106 -0
  252. data/ext/pg_query/include/{port → postgres/port}/atomics/generic.h +1 -1
  253. data/ext/pg_query/include/{port → postgres/port}/atomics.h +2 -7
  254. data/ext/pg_query/include/{port → postgres/port}/pg_bitutils.h +129 -16
  255. data/ext/pg_query/include/{port → postgres/port}/pg_bswap.h +1 -1
  256. data/ext/pg_query/include/{port → postgres/port}/pg_crc32c.h +1 -1
  257. data/ext/pg_query/include/postgres/port/simd.h +375 -0
  258. data/ext/pg_query/include/postgres/port/win32/arpa/inet.h +3 -0
  259. data/ext/pg_query/include/postgres/port/win32/dlfcn.h +1 -0
  260. data/ext/pg_query/include/postgres/port/win32/grp.h +1 -0
  261. data/ext/pg_query/include/postgres/port/win32/netdb.h +7 -0
  262. data/ext/pg_query/include/postgres/port/win32/netinet/in.h +3 -0
  263. data/ext/pg_query/include/postgres/port/win32/netinet/tcp.h +7 -0
  264. data/ext/pg_query/include/postgres/port/win32/pwd.h +3 -0
  265. data/ext/pg_query/include/postgres/port/win32/sys/resource.h +20 -0
  266. data/ext/pg_query/include/postgres/port/win32/sys/select.h +3 -0
  267. data/ext/pg_query/include/postgres/port/win32/sys/socket.h +26 -0
  268. data/ext/pg_query/include/postgres/port/win32/sys/un.h +17 -0
  269. data/ext/pg_query/include/postgres/port/win32/sys/wait.h +3 -0
  270. data/ext/pg_query/include/postgres/port/win32.h +59 -0
  271. data/ext/pg_query/include/postgres/port/win32_msvc/dirent.h +34 -0
  272. data/ext/pg_query/include/postgres/port/win32_msvc/sys/file.h +1 -0
  273. data/ext/pg_query/include/postgres/port/win32_msvc/sys/param.h +1 -0
  274. data/ext/pg_query/include/postgres/port/win32_msvc/sys/time.h +1 -0
  275. data/ext/pg_query/include/postgres/port/win32_msvc/unistd.h +9 -0
  276. data/ext/pg_query/include/postgres/port/win32_msvc/utime.h +3 -0
  277. data/ext/pg_query/include/postgres/port/win32_port.h +594 -0
  278. data/ext/pg_query/include/{port.h → postgres/port.h} +107 -111
  279. data/ext/pg_query/include/postgres/portability/instr_time.h +197 -0
  280. data/ext/pg_query/include/postgres/postgres.h +579 -0
  281. data/ext/pg_query/include/{postgres_ext.h → postgres/postgres_ext.h} +0 -1
  282. data/ext/pg_query/include/{postmaster → postgres/postmaster}/autovacuum.h +17 -20
  283. data/ext/pg_query/include/{postmaster → postgres/postmaster}/bgworker.h +3 -2
  284. data/ext/pg_query/include/{postmaster → postgres/postmaster}/bgworker_internals.h +2 -2
  285. data/ext/pg_query/include/{postmaster → postgres/postmaster}/bgwriter.h +6 -6
  286. data/ext/pg_query/include/{postmaster → postgres/postmaster}/interrupt.h +1 -1
  287. data/ext/pg_query/include/{postmaster → postgres/postmaster}/pgarch.h +7 -10
  288. data/ext/pg_query/include/{postmaster → postgres/postmaster}/postmaster.h +21 -17
  289. data/ext/pg_query/include/postgres/postmaster/startup.h +41 -0
  290. data/ext/pg_query/include/{postmaster → postgres/postmaster}/syslogger.h +16 -11
  291. data/ext/pg_query/include/{postmaster → postgres/postmaster}/walwriter.h +5 -3
  292. data/ext/pg_query/include/{regex → postgres/regex}/regex.h +27 -22
  293. data/ext/pg_query/include/{replication → postgres/replication}/logicallauncher.h +8 -5
  294. data/ext/pg_query/include/postgres/replication/logicalproto.h +274 -0
  295. data/ext/pg_query/include/postgres/replication/logicalworker.h +32 -0
  296. data/ext/pg_query/include/{replication → postgres/replication}/origin.h +8 -8
  297. data/ext/pg_query/include/postgres/replication/reorderbuffer.h +753 -0
  298. data/ext/pg_query/include/{replication → postgres/replication}/slot.h +42 -12
  299. data/ext/pg_query/include/{replication → postgres/replication}/syncrep.h +6 -12
  300. data/ext/pg_query/include/{replication → postgres/replication}/walreceiver.h +158 -20
  301. data/ext/pg_query/include/{replication → postgres/replication}/walsender.h +20 -20
  302. data/ext/pg_query/include/{rewrite → postgres/rewrite}/prs2lock.h +1 -1
  303. data/ext/pg_query/include/{rewrite → postgres/rewrite}/rewriteHandler.h +1 -6
  304. data/ext/pg_query/include/{rewrite → postgres/rewrite}/rewriteManip.h +11 -2
  305. data/ext/pg_query/include/{rewrite → postgres/rewrite}/rewriteSupport.h +1 -1
  306. data/ext/pg_query/include/{storage → postgres/storage}/backendid.h +3 -3
  307. data/ext/pg_query/include/{storage → postgres/storage}/block.h +24 -37
  308. data/ext/pg_query/include/{storage → postgres/storage}/buf.h +1 -1
  309. data/ext/pg_query/include/{storage → postgres/storage}/bufmgr.h +196 -95
  310. data/ext/pg_query/include/{storage → postgres/storage}/bufpage.h +152 -101
  311. data/ext/pg_query/include/{storage → postgres/storage}/condition_variable.h +14 -3
  312. data/ext/pg_query/include/{storage → postgres/storage}/dsm.h +6 -6
  313. data/ext/pg_query/include/{storage → postgres/storage}/dsm_impl.h +6 -2
  314. data/ext/pg_query/include/{storage → postgres/storage}/fd.h +48 -14
  315. data/ext/pg_query/include/postgres/storage/fileset.h +40 -0
  316. data/ext/pg_query/include/{storage → postgres/storage}/ipc.h +5 -2
  317. data/ext/pg_query/include/{storage → postgres/storage}/item.h +1 -1
  318. data/ext/pg_query/include/{storage → postgres/storage}/itemid.h +1 -1
  319. data/ext/pg_query/include/{storage → postgres/storage}/itemptr.h +96 -57
  320. data/ext/pg_query/include/{storage → postgres/storage}/large_object.h +2 -2
  321. data/ext/pg_query/include/{storage → postgres/storage}/latch.h +17 -13
  322. data/ext/pg_query/include/{storage → postgres/storage}/lmgr.h +7 -1
  323. data/ext/pg_query/include/{storage → postgres/storage}/lock.h +37 -25
  324. data/ext/pg_query/include/{storage → postgres/storage}/lockdefs.h +4 -4
  325. data/ext/pg_query/include/{storage → postgres/storage}/lwlock.h +21 -33
  326. data/ext/pg_query/include/{storage → postgres/storage}/lwlocknames.h +0 -1
  327. data/ext/pg_query/include/{storage → postgres/storage}/off.h +1 -1
  328. data/ext/pg_query/include/{storage → postgres/storage}/pg_sema.h +1 -1
  329. data/ext/pg_query/include/{storage → postgres/storage}/pg_shmem.h +9 -7
  330. data/ext/pg_query/include/{storage → postgres/storage}/pmsignal.h +15 -4
  331. data/ext/pg_query/include/{storage → postgres/storage}/predicate.h +5 -5
  332. data/ext/pg_query/include/{storage → postgres/storage}/proc.h +200 -67
  333. data/ext/pg_query/include/postgres/storage/procarray.h +99 -0
  334. data/ext/pg_query/include/{storage → postgres/storage}/proclist_types.h +1 -1
  335. data/ext/pg_query/include/{storage → postgres/storage}/procsignal.h +5 -7
  336. data/ext/pg_query/include/postgres/storage/relfilelocator.h +99 -0
  337. data/ext/pg_query/include/{storage → postgres/storage}/s_lock.h +118 -298
  338. data/ext/pg_query/include/{storage → postgres/storage}/sharedfileset.h +3 -11
  339. data/ext/pg_query/include/{storage → postgres/storage}/shm_mq.h +5 -4
  340. data/ext/pg_query/include/{storage → postgres/storage}/shm_toc.h +1 -1
  341. data/ext/pg_query/include/{storage → postgres/storage}/shmem.h +1 -23
  342. data/ext/pg_query/include/{storage → postgres/storage}/sinval.h +5 -5
  343. data/ext/pg_query/include/{storage → postgres/storage}/sinvaladt.h +4 -2
  344. data/ext/pg_query/include/{storage → postgres/storage}/smgr.h +21 -17
  345. data/ext/pg_query/include/{storage → postgres/storage}/spin.h +2 -2
  346. data/ext/pg_query/include/{storage → postgres/storage}/standby.h +17 -9
  347. data/ext/pg_query/include/{storage → postgres/storage}/standbydefs.h +2 -2
  348. data/ext/pg_query/include/{storage → postgres/storage}/sync.h +9 -5
  349. data/ext/pg_query/include/{tcop → postgres/tcop}/cmdtag.h +7 -2
  350. data/ext/pg_query/include/{tcop → postgres/tcop}/cmdtaglist.h +3 -2
  351. data/ext/pg_query/include/{tcop → postgres/tcop}/deparse_utility.h +1 -1
  352. data/ext/pg_query/include/{tcop → postgres/tcop}/dest.h +1 -3
  353. data/ext/pg_query/include/{tcop → postgres/tcop}/fastpath.h +1 -2
  354. data/ext/pg_query/include/{tcop → postgres/tcop}/pquery.h +7 -1
  355. data/ext/pg_query/include/{tcop → postgres/tcop}/tcopprot.h +19 -14
  356. data/ext/pg_query/include/{tcop → postgres/tcop}/utility.h +7 -3
  357. data/ext/pg_query/include/{tsearch → postgres/tsearch}/ts_cache.h +3 -5
  358. data/ext/pg_query/include/{utils → postgres/utils}/acl.h +37 -71
  359. data/ext/pg_query/include/{utils → postgres/utils}/aclchk_internal.h +1 -1
  360. data/ext/pg_query/include/{utils → postgres/utils}/array.h +26 -2
  361. data/ext/pg_query/include/postgres/utils/backend_progress.h +45 -0
  362. data/ext/pg_query/include/postgres/utils/backend_status.h +342 -0
  363. data/ext/pg_query/include/{utils → postgres/utils}/builtins.h +20 -11
  364. data/ext/pg_query/include/{utils → postgres/utils}/bytea.h +3 -2
  365. data/ext/pg_query/include/{utils → postgres/utils}/catcache.h +1 -1
  366. data/ext/pg_query/include/{utils → postgres/utils}/date.h +37 -9
  367. data/ext/pg_query/include/{utils → postgres/utils}/datetime.h +48 -27
  368. data/ext/pg_query/include/{utils → postgres/utils}/datum.h +9 -1
  369. data/ext/pg_query/include/{utils → postgres/utils}/dsa.h +5 -1
  370. data/ext/pg_query/include/{utils → postgres/utils}/elog.h +154 -48
  371. data/ext/pg_query/include/{utils → postgres/utils}/errcodes.h +2 -0
  372. data/ext/pg_query/include/{utils → postgres/utils}/expandeddatum.h +14 -3
  373. data/ext/pg_query/include/{utils → postgres/utils}/expandedrecord.h +14 -4
  374. data/ext/pg_query/include/{utils → postgres/utils}/float.h +13 -12
  375. data/ext/pg_query/include/{utils → postgres/utils}/fmgroids.h +1353 -696
  376. data/ext/pg_query/include/{utils → postgres/utils}/fmgrprotos.h +243 -18
  377. data/ext/pg_query/include/{utils → postgres/utils}/fmgrtab.h +6 -5
  378. data/ext/pg_query/include/{utils → postgres/utils}/guc.h +120 -121
  379. data/ext/pg_query/include/postgres/utils/guc_hooks.h +163 -0
  380. data/ext/pg_query/include/{utils → postgres/utils}/guc_tables.h +71 -21
  381. data/ext/pg_query/include/{utils → postgres/utils}/hsearch.h +15 -11
  382. data/ext/pg_query/include/{utils → postgres/utils}/inval.h +7 -3
  383. data/ext/pg_query/include/postgres/utils/logtape.h +77 -0
  384. data/ext/pg_query/include/{utils → postgres/utils}/lsyscache.h +16 -1
  385. data/ext/pg_query/include/{utils → postgres/utils}/memdebug.h +1 -1
  386. data/ext/pg_query/include/{utils → postgres/utils}/memutils.h +14 -53
  387. data/ext/pg_query/include/postgres/utils/memutils_internal.h +136 -0
  388. data/ext/pg_query/include/postgres/utils/memutils_memorychunk.h +237 -0
  389. data/ext/pg_query/include/{utils → postgres/utils}/numeric.h +38 -9
  390. data/ext/pg_query/include/{utils → postgres/utils}/palloc.h +33 -4
  391. data/ext/pg_query/include/{utils → postgres/utils}/partcache.h +3 -2
  392. data/ext/pg_query/include/{utils → postgres/utils}/pg_locale.h +37 -21
  393. data/ext/pg_query/include/postgres/utils/pgstat_internal.h +814 -0
  394. data/ext/pg_query/include/{utils → postgres/utils}/plancache.h +6 -5
  395. data/ext/pg_query/include/{utils → postgres/utils}/portal.h +12 -1
  396. data/ext/pg_query/include/{utils → postgres/utils}/probes.h +59 -59
  397. data/ext/pg_query/include/postgres/utils/ps_status.h +47 -0
  398. data/ext/pg_query/include/{utils → postgres/utils}/queryenvironment.h +1 -1
  399. data/ext/pg_query/include/postgres/utils/regproc.h +39 -0
  400. data/ext/pg_query/include/{utils → postgres/utils}/rel.h +129 -61
  401. data/ext/pg_query/include/{utils → postgres/utils}/relcache.h +21 -14
  402. data/ext/pg_query/include/{utils → postgres/utils}/reltrigger.h +1 -1
  403. data/ext/pg_query/include/{utils → postgres/utils}/resowner.h +1 -1
  404. data/ext/pg_query/include/{utils → postgres/utils}/ruleutils.h +9 -1
  405. data/ext/pg_query/include/{utils → postgres/utils}/sharedtuplestore.h +1 -1
  406. data/ext/pg_query/include/{utils → postgres/utils}/snapmgr.h +38 -15
  407. data/ext/pg_query/include/{utils → postgres/utils}/snapshot.h +14 -1
  408. data/ext/pg_query/include/{utils → postgres/utils}/sortsupport.h +117 -2
  409. data/ext/pg_query/include/{utils → postgres/utils}/syscache.h +9 -1
  410. data/ext/pg_query/include/{utils → postgres/utils}/timeout.h +11 -4
  411. data/ext/pg_query/include/{utils → postgres/utils}/timestamp.h +46 -15
  412. data/ext/pg_query/include/{utils → postgres/utils}/tuplesort.h +209 -41
  413. data/ext/pg_query/include/{utils → postgres/utils}/tuplestore.h +2 -2
  414. data/ext/pg_query/include/{utils → postgres/utils}/typcache.h +24 -17
  415. data/ext/pg_query/include/{utils → postgres/utils}/varlena.h +17 -3
  416. data/ext/pg_query/include/postgres/utils/wait_event.h +294 -0
  417. data/ext/pg_query/include/{utils → postgres/utils}/xml.h +18 -8
  418. data/ext/pg_query/include/{postgres.h → postgres/varatt.h} +65 -471
  419. data/ext/pg_query/include/protobuf/pg_query.pb-c.h +7494 -6382
  420. data/ext/pg_query/include/protobuf/pg_query.pb.h +116922 -84792
  421. data/ext/pg_query/include/protobuf-c/protobuf-c.h +7 -3
  422. data/ext/pg_query/include/protobuf-c.h +7 -3
  423. data/ext/pg_query/pg_query.c +10 -1
  424. data/ext/pg_query/pg_query.pb-c.c +21026 -17002
  425. data/ext/pg_query/pg_query_deparse.c +1 -9896
  426. data/ext/pg_query/pg_query_fingerprint.c +162 -50
  427. data/ext/pg_query/pg_query_fingerprint.h +3 -1
  428. data/ext/pg_query/pg_query_internal.h +1 -1
  429. data/ext/pg_query/pg_query_json_plpgsql.c +56 -12
  430. data/ext/pg_query/pg_query_normalize.c +259 -64
  431. data/ext/pg_query/pg_query_outfuncs.h +1 -0
  432. data/ext/pg_query/pg_query_outfuncs_json.c +71 -16
  433. data/ext/pg_query/pg_query_outfuncs_protobuf.c +73 -12
  434. data/ext/pg_query/pg_query_parse.c +47 -5
  435. data/ext/pg_query/pg_query_parse_plpgsql.c +86 -21
  436. data/ext/pg_query/pg_query_readfuncs_protobuf.c +43 -8
  437. data/ext/pg_query/pg_query_ruby.c +6 -1
  438. data/ext/pg_query/pg_query_ruby_freebsd.sym +2 -0
  439. data/ext/pg_query/pg_query_scan.c +3 -2
  440. data/ext/pg_query/pg_query_split.c +6 -5
  441. data/ext/pg_query/postgres_deparse.c +11067 -0
  442. data/ext/pg_query/postgres_deparse.h +9 -0
  443. data/ext/pg_query/protobuf-c.c +34 -27
  444. data/ext/pg_query/src_backend_catalog_namespace.c +27 -10
  445. data/ext/pg_query/src_backend_catalog_pg_proc.c +4 -1
  446. data/ext/pg_query/src_backend_commands_define.c +11 -1
  447. data/ext/pg_query/src_backend_nodes_bitmapset.c +13 -70
  448. data/ext/pg_query/src_backend_nodes_copyfuncs.c +103 -5894
  449. data/ext/pg_query/src_backend_nodes_equalfuncs.c +102 -3830
  450. data/ext/pg_query/src_backend_nodes_extensible.c +6 -29
  451. data/ext/pg_query/src_backend_nodes_list.c +99 -12
  452. data/ext/pg_query/src_backend_nodes_makefuncs.c +99 -4
  453. data/ext/pg_query/src_backend_nodes_nodeFuncs.c +325 -131
  454. data/ext/pg_query/src_backend_nodes_nodes.c +38 -0
  455. data/ext/pg_query/src_backend_nodes_value.c +28 -19
  456. data/ext/pg_query/src_backend_parser_gram.c +36104 -32074
  457. data/ext/pg_query/src_backend_parser_parser.c +53 -8
  458. data/ext/pg_query/src_backend_parser_scan.c +4893 -3701
  459. data/ext/pg_query/src_backend_parser_scansup.c +4 -28
  460. data/ext/pg_query/src_backend_storage_ipc_ipc.c +13 -4
  461. data/ext/pg_query/src_backend_tcop_postgres.c +133 -105
  462. data/ext/pg_query/src_backend_utils_activity_pgstat_database.c +140 -0
  463. data/ext/pg_query/src_backend_utils_adt_datum.c +17 -7
  464. data/ext/pg_query/src_backend_utils_adt_expandeddatum.c +1 -1
  465. data/ext/pg_query/src_backend_utils_adt_format_type.c +6 -2
  466. data/ext/pg_query/src_backend_utils_adt_numutils.c +489 -0
  467. data/ext/pg_query/src_backend_utils_adt_ruleutils.c +187 -19
  468. data/ext/pg_query/src_backend_utils_error_assert.c +17 -18
  469. data/ext/pg_query/src_backend_utils_error_elog.c +513 -318
  470. data/ext/pg_query/src_backend_utils_fmgr_fmgr.c +44 -17
  471. data/ext/pg_query/src_backend_utils_init_globals.c +9 -6
  472. data/ext/pg_query/src_backend_utils_mb_mbutils.c +74 -131
  473. data/ext/pg_query/src_backend_utils_misc_guc_tables.c +492 -0
  474. data/ext/pg_query/src_backend_utils_mmgr_alignedalloc.c +163 -0
  475. data/ext/pg_query/src_backend_utils_mmgr_aset.c +453 -314
  476. data/ext/pg_query/src_backend_utils_mmgr_generation.c +1039 -0
  477. data/ext/pg_query/src_backend_utils_mmgr_mcxt.c +549 -76
  478. data/ext/pg_query/src_backend_utils_mmgr_slab.c +1021 -0
  479. data/ext/pg_query/src_common_encnames.c +4 -1
  480. data/ext/pg_query/src_common_hashfn.c +420 -0
  481. data/ext/pg_query/src_common_keywords.c +15 -2
  482. data/ext/pg_query/src_common_kwlist_d.h +545 -498
  483. data/ext/pg_query/src_common_kwlookup.c +1 -1
  484. data/ext/pg_query/src_common_psprintf.c +1 -1
  485. data/ext/pg_query/src_common_stringinfo.c +4 -4
  486. data/ext/pg_query/src_common_wchar.c +717 -113
  487. data/ext/pg_query/src_pl_plpgsql_src_pl_comp.c +49 -22
  488. data/ext/pg_query/src_pl_plpgsql_src_pl_funcs.c +3 -18
  489. data/ext/pg_query/src_pl_plpgsql_src_pl_gram.c +1136 -1195
  490. data/ext/pg_query/src_pl_plpgsql_src_pl_handler.c +1 -1
  491. data/ext/pg_query/src_pl_plpgsql_src_pl_reserved_kwlist_d.h +10 -10
  492. data/ext/pg_query/src_pl_plpgsql_src_pl_scanner.c +2 -2
  493. data/ext/pg_query/src_pl_plpgsql_src_pl_unreserved_kwlist_d.h +60 -60
  494. data/ext/pg_query/src_port_pg_bitutils.c +103 -40
  495. data/ext/pg_query/src_port_pgstrcasecmp.c +29 -1
  496. data/ext/pg_query/src_port_qsort.c +12 -224
  497. data/ext/pg_query/src_port_snprintf.c +51 -29
  498. data/ext/pg_query/src_port_strerror.c +9 -19
  499. data/ext/pg_query/src_port_strlcpy.c +79 -0
  500. data/lib/pg_query/deparse.rb +7 -1
  501. data/lib/pg_query/filter_columns.rb +7 -5
  502. data/lib/pg_query/fingerprint.rb +21 -9
  503. data/lib/pg_query/node.rb +18 -13
  504. data/lib/pg_query/param_refs.rb +1 -1
  505. data/lib/pg_query/parse.rb +141 -50
  506. data/lib/pg_query/pg_query_pb.rb +175 -3031
  507. data/lib/pg_query/treewalker.rb +26 -2
  508. data/lib/pg_query/truncate.rb +54 -8
  509. data/lib/pg_query/version.rb +1 -1
  510. data/lib/pg_query.rb +0 -1
  511. metadata +443 -380
  512. data/ext/pg_query/guc-file.c +0 -0
  513. data/ext/pg_query/include/access/rmgr.h +0 -35
  514. data/ext/pg_query/include/access/xloginsert.h +0 -64
  515. data/ext/pg_query/include/bootstrap/bootstrap.h +0 -62
  516. data/ext/pg_query/include/catalog/genbki.h +0 -64
  517. data/ext/pg_query/include/catalog/indexing.h +0 -366
  518. data/ext/pg_query/include/commands/variable.h +0 -38
  519. data/ext/pg_query/include/common/ip.h +0 -37
  520. data/ext/pg_query/include/common/string.h +0 -19
  521. data/ext/pg_query/include/getaddrinfo.h +0 -162
  522. data/ext/pg_query/include/kwlist_d.h +0 -1072
  523. data/ext/pg_query/include/nodes/value.h +0 -61
  524. data/ext/pg_query/include/parser/gram.h +0 -1067
  525. data/ext/pg_query/include/parser/kwlist.h +0 -477
  526. data/ext/pg_query/include/parser/parse_clause.h +0 -54
  527. data/ext/pg_query/include/parser/parse_collate.h +0 -27
  528. data/ext/pg_query/include/parser/parse_target.h +0 -46
  529. data/ext/pg_query/include/parser/parser.h +0 -41
  530. data/ext/pg_query/include/pg_config_os.h +0 -8
  531. data/ext/pg_query/include/pgstat.h +0 -1487
  532. data/ext/pg_query/include/portability/instr_time.h +0 -256
  533. data/ext/pg_query/include/postmaster/fork_process.h +0 -17
  534. data/ext/pg_query/include/replication/logicalproto.h +0 -110
  535. data/ext/pg_query/include/replication/logicalworker.h +0 -19
  536. data/ext/pg_query/include/replication/reorderbuffer.h +0 -467
  537. data/ext/pg_query/include/storage/relfilenode.h +0 -99
  538. data/ext/pg_query/include/utils/dynahash.h +0 -19
  539. data/ext/pg_query/include/utils/pg_lsn.h +0 -29
  540. data/ext/pg_query/include/utils/pidfile.h +0 -56
  541. data/ext/pg_query/include/utils/ps_status.h +0 -25
  542. data/ext/pg_query/include/utils/regproc.h +0 -28
  543. data/ext/pg_query/include/utils/rls.h +0 -50
  544. data/ext/pg_query/include/utils/tzparser.h +0 -39
  545. data/ext/pg_query/src_backend_libpq_pqcomm.c +0 -651
  546. data/ext/pg_query/src_backend_parser_parse_expr.c +0 -313
  547. data/ext/pg_query/src_backend_postmaster_postmaster.c +0 -2230
  548. data/ext/pg_query/src_backend_storage_lmgr_s_lock.c +0 -370
  549. data/ext/pg_query/src_backend_utils_hash_dynahash.c +0 -1086
  550. data/ext/pg_query/src_backend_utils_misc_guc.c +0 -1831
  551. data/ext/pg_query/src_common_string.c +0 -86
  552. data/ext/pg_query/src_port_erand48.c +0 -127
  553. data/ext/pg_query/src_port_pgsleep.c +0 -69
  554. data/ext/pg_query/src_port_random.c +0 -31
  555. data/ext/pg_query/src_port_strnlen.c +0 -39
  556. data/lib/pg_query/json_field_names.rb +0 -1402
  557. /data/ext/pg_query/include/{pg_config_ext.h → postgres/pg_config_ext.h} +0 -0
@@ -1,2230 +0,0 @@
1
- /*--------------------------------------------------------------------
2
- * Symbols referenced in this file:
3
- * - ClientAuthInProgress
4
- *--------------------------------------------------------------------
5
- */
6
-
7
- /*-------------------------------------------------------------------------
8
- *
9
- * postmaster.c
10
- * This program acts as a clearing house for requests to the
11
- * POSTGRES system. Frontend programs send a startup message
12
- * to the Postmaster and the postmaster uses the info in the
13
- * message to setup a backend process.
14
- *
15
- * The postmaster also manages system-wide operations such as
16
- * startup and shutdown. The postmaster itself doesn't do those
17
- * operations, mind you --- it just forks off a subprocess to do them
18
- * at the right times. It also takes care of resetting the system
19
- * if a backend crashes.
20
- *
21
- * The postmaster process creates the shared memory and semaphore
22
- * pools during startup, but as a rule does not touch them itself.
23
- * In particular, it is not a member of the PGPROC array of backends
24
- * and so it cannot participate in lock-manager operations. Keeping
25
- * the postmaster away from shared memory operations makes it simpler
26
- * and more reliable. The postmaster is almost always able to recover
27
- * from crashes of individual backends by resetting shared memory;
28
- * if it did much with shared memory then it would be prone to crashing
29
- * along with the backends.
30
- *
31
- * When a request message is received, we now fork() immediately.
32
- * The child process performs authentication of the request, and
33
- * then becomes a backend if successful. This allows the auth code
34
- * to be written in a simple single-threaded style (as opposed to the
35
- * crufty "poor man's multitasking" code that used to be needed).
36
- * More importantly, it ensures that blockages in non-multithreaded
37
- * libraries like SSL or PAM cannot cause denial of service to other
38
- * clients.
39
- *
40
- *
41
- * Portions Copyright (c) 1996-2020, PostgreSQL Global Development Group
42
- * Portions Copyright (c) 1994, Regents of the University of California
43
- *
44
- *
45
- * IDENTIFICATION
46
- * src/backend/postmaster/postmaster.c
47
- *
48
- * NOTES
49
- *
50
- * Initialization:
51
- * The Postmaster sets up shared memory data structures
52
- * for the backends.
53
- *
54
- * Synchronization:
55
- * The Postmaster shares memory with the backends but should avoid
56
- * touching shared memory, so as not to become stuck if a crashing
57
- * backend screws up locks or shared memory. Likewise, the Postmaster
58
- * should never block on messages from frontend clients.
59
- *
60
- * Garbage Collection:
61
- * The Postmaster cleans up after backends if they have an emergency
62
- * exit and/or core dump.
63
- *
64
- * Error Reporting:
65
- * Use write_stderr() only for reporting "interactive" errors
66
- * (essentially, bogus arguments on the command line). Once the
67
- * postmaster is launched, use ereport().
68
- *
69
- *-------------------------------------------------------------------------
70
- */
71
-
72
- #include "postgres.h"
73
-
74
- #include <unistd.h>
75
- #include <signal.h>
76
- #include <time.h>
77
- #include <sys/wait.h>
78
- #include <ctype.h>
79
- #include <sys/stat.h>
80
- #include <sys/socket.h>
81
- #include <fcntl.h>
82
- #include <sys/param.h>
83
- #include <netdb.h>
84
- #include <limits.h>
85
-
86
- #ifdef HAVE_SYS_SELECT_H
87
- #include <sys/select.h>
88
- #endif
89
-
90
- #ifdef USE_BONJOUR
91
- #include <dns_sd.h>
92
- #endif
93
-
94
- #ifdef USE_SYSTEMD
95
- #include <systemd/sd-daemon.h>
96
- #endif
97
-
98
- #ifdef HAVE_PTHREAD_IS_THREADED_NP
99
- #include <pthread.h>
100
- #endif
101
-
102
- #include "access/transam.h"
103
- #include "access/xlog.h"
104
- #include "bootstrap/bootstrap.h"
105
- #include "catalog/pg_control.h"
106
- #include "common/file_perm.h"
107
- #include "common/ip.h"
108
- #include "common/string.h"
109
- #include "lib/ilist.h"
110
- #include "libpq/auth.h"
111
- #include "libpq/libpq.h"
112
- #include "libpq/pqformat.h"
113
- #include "libpq/pqsignal.h"
114
- #include "miscadmin.h"
115
- #include "pg_getopt.h"
116
- #include "pgstat.h"
117
- #include "port/pg_bswap.h"
118
- #include "postmaster/autovacuum.h"
119
- #include "postmaster/bgworker_internals.h"
120
- #include "postmaster/fork_process.h"
121
- #include "postmaster/interrupt.h"
122
- #include "postmaster/pgarch.h"
123
- #include "postmaster/postmaster.h"
124
- #include "postmaster/syslogger.h"
125
- #include "replication/logicallauncher.h"
126
- #include "replication/walsender.h"
127
- #include "storage/fd.h"
128
- #include "storage/ipc.h"
129
- #include "storage/pg_shmem.h"
130
- #include "storage/pmsignal.h"
131
- #include "storage/proc.h"
132
- #include "tcop/tcopprot.h"
133
- #include "utils/builtins.h"
134
- #include "utils/datetime.h"
135
- #include "utils/memutils.h"
136
- #include "utils/pidfile.h"
137
- #include "utils/ps_status.h"
138
- #include "utils/timeout.h"
139
- #include "utils/timestamp.h"
140
- #include "utils/varlena.h"
141
-
142
- #ifdef EXEC_BACKEND
143
- #include "storage/spin.h"
144
- #endif
145
-
146
-
147
- /*
148
- * Possible types of a backend. Beyond being the possible bkend_type values in
149
- * struct bkend, these are OR-able request flag bits for SignalSomeChildren()
150
- * and CountChildren().
151
- */
152
- #define BACKEND_TYPE_NORMAL 0x0001 /* normal backend */
153
- #define BACKEND_TYPE_AUTOVAC 0x0002 /* autovacuum worker process */
154
- #define BACKEND_TYPE_WALSND 0x0004 /* walsender process */
155
- #define BACKEND_TYPE_BGWORKER 0x0008 /* bgworker process */
156
- #define BACKEND_TYPE_ALL 0x000F /* OR of all the above */
157
-
158
- /*
159
- * List of active backends (or child processes anyway; we don't actually
160
- * know whether a given child has become a backend or is still in the
161
- * authorization phase). This is used mainly to keep track of how many
162
- * children we have and send them appropriate signals when necessary.
163
- *
164
- * "Special" children such as the startup, bgwriter and autovacuum launcher
165
- * tasks are not in this list. Autovacuum worker and walsender are in it.
166
- * Also, "dead_end" children are in it: these are children launched just for
167
- * the purpose of sending a friendly rejection message to a would-be client.
168
- * We must track them because they are attached to shared memory, but we know
169
- * they will never become live backends. dead_end children are not assigned a
170
- * PMChildSlot.
171
- *
172
- * Background workers are in this list, too.
173
- */
174
- typedef struct bkend
175
- {
176
- pid_t pid; /* process id of backend */
177
- int32 cancel_key; /* cancel key for cancels for this backend */
178
- int child_slot; /* PMChildSlot for this backend, if any */
179
-
180
- /*
181
- * Flavor of backend or auxiliary process. Note that BACKEND_TYPE_WALSND
182
- * backends initially announce themselves as BACKEND_TYPE_NORMAL, so if
183
- * bkend_type is normal, you should check for a recent transition.
184
- */
185
- int bkend_type;
186
- bool dead_end; /* is it going to send an error and quit? */
187
- bool bgworker_notify; /* gets bgworker start/stop notifications */
188
- dlist_node elem; /* list link in BackendList */
189
- } Backend;
190
-
191
-
192
-
193
- #ifdef EXEC_BACKEND
194
- static Backend *ShmemBackendArray;
195
- #endif
196
-
197
-
198
-
199
-
200
-
201
- /* The socket number we are listening for connections on */
202
-
203
-
204
- /* The directory names for Unix socket(s) */
205
-
206
-
207
- /* The TCP listen address(es) */
208
-
209
-
210
- /*
211
- * ReservedBackends is the number of backends reserved for superuser use.
212
- * This number is taken out of the pool size given by MaxConnections so
213
- * number of backend slots available to non-superusers is
214
- * (MaxConnections - ReservedBackends). Note what this really means is
215
- * "if there are <= ReservedBackends connections available, only superusers
216
- * can make new connections" --- pre-existing superuser connections don't
217
- * count against the limit.
218
- */
219
-
220
-
221
- /* The socket(s) we're listening to. */
222
- #define MAXLISTEN 64
223
-
224
-
225
- /*
226
- * Set by the -o option
227
- */
228
-
229
-
230
- /*
231
- * These globals control the behavior of the postmaster in case some
232
- * backend dumps core. Normally, it kills all peers of the dead backend
233
- * and reinitializes shared memory. By specifying -s or -n, we can have
234
- * the postmaster stop (rather than kill) peers and not reinitialize
235
- * shared data structures. (Reinit is currently dead code, though.)
236
- */
237
-
238
-
239
-
240
- /* still more option variables */
241
-
242
-
243
-
244
-
245
-
246
- /* for ps display and logging */
247
-
248
-
249
-
250
-
251
-
252
-
253
-
254
- /* PIDs of special child processes; 0 when not running */
255
-
256
-
257
-
258
-
259
-
260
-
261
-
262
-
263
-
264
-
265
- /* Startup process's status */
266
- typedef enum
267
- {
268
- STARTUP_NOT_RUNNING,
269
- STARTUP_RUNNING,
270
- STARTUP_SIGNALED, /* we sent it a SIGQUIT or SIGKILL */
271
- STARTUP_CRASHED
272
- } StartupStatusEnum;
273
-
274
-
275
-
276
- /* Startup/shutdown state */
277
- #define NoShutdown 0
278
- #define SmartShutdown 1
279
- #define FastShutdown 2
280
- #define ImmediateShutdown 3
281
-
282
-
283
-
284
- /* T if recovering from backend crash */
285
-
286
- /*
287
- * We use a simple state machine to control startup, shutdown, and
288
- * crash recovery (which is rather like shutdown followed by startup).
289
- *
290
- * After doing all the postmaster initialization work, we enter PM_STARTUP
291
- * state and the startup process is launched. The startup process begins by
292
- * reading the control file and other preliminary initialization steps.
293
- * In a normal startup, or after crash recovery, the startup process exits
294
- * with exit code 0 and we switch to PM_RUN state. However, archive recovery
295
- * is handled specially since it takes much longer and we would like to support
296
- * hot standby during archive recovery.
297
- *
298
- * When the startup process is ready to start archive recovery, it signals the
299
- * postmaster, and we switch to PM_RECOVERY state. The background writer and
300
- * checkpointer are launched, while the startup process continues applying WAL.
301
- * If Hot Standby is enabled, then, after reaching a consistent point in WAL
302
- * redo, startup process signals us again, and we switch to PM_HOT_STANDBY
303
- * state and begin accepting connections to perform read-only queries. When
304
- * archive recovery is finished, the startup process exits with exit code 0
305
- * and we switch to PM_RUN state.
306
- *
307
- * Normal child backends can only be launched when we are in PM_RUN or
308
- * PM_HOT_STANDBY state. (connsAllowed can also restrict launching.)
309
- * In other states we handle connection requests by launching "dead_end"
310
- * child processes, which will simply send the client an error message and
311
- * quit. (We track these in the BackendList so that we can know when they
312
- * are all gone; this is important because they're still connected to shared
313
- * memory, and would interfere with an attempt to destroy the shmem segment,
314
- * possibly leading to SHMALL failure when we try to make a new one.)
315
- * In PM_WAIT_DEAD_END state we are waiting for all the dead_end children
316
- * to drain out of the system, and therefore stop accepting connection
317
- * requests at all until the last existing child has quit (which hopefully
318
- * will not be very long).
319
- *
320
- * Notice that this state variable does not distinguish *why* we entered
321
- * states later than PM_RUN --- Shutdown and FatalError must be consulted
322
- * to find that out. FatalError is never true in PM_RECOVERY, PM_HOT_STANDBY,
323
- * or PM_RUN states, nor in PM_SHUTDOWN states (because we don't enter those
324
- * states when trying to recover from a crash). It can be true in PM_STARTUP
325
- * state, because we don't clear it until we've successfully started WAL redo.
326
- */
327
- typedef enum
328
- {
329
- PM_INIT, /* postmaster starting */
330
- PM_STARTUP, /* waiting for startup subprocess */
331
- PM_RECOVERY, /* in archive recovery mode */
332
- PM_HOT_STANDBY, /* in hot standby mode */
333
- PM_RUN, /* normal "database is alive" state */
334
- PM_STOP_BACKENDS, /* need to stop remaining backends */
335
- PM_WAIT_BACKENDS, /* waiting for live backends to exit */
336
- PM_SHUTDOWN, /* waiting for checkpointer to do shutdown
337
- * ckpt */
338
- PM_SHUTDOWN_2, /* waiting for archiver and walsenders to
339
- * finish */
340
- PM_WAIT_DEAD_END, /* waiting for dead_end children to exit */
341
- PM_NO_CHILDREN /* all important children have exited */
342
- } PMState;
343
-
344
-
345
-
346
- /*
347
- * While performing a "smart shutdown", we restrict new connections but stay
348
- * in PM_RUN or PM_HOT_STANDBY state until all the client backends are gone.
349
- * connsAllowed is a sub-state indicator showing the active restriction.
350
- * It is of no interest unless pmState is PM_RUN or PM_HOT_STANDBY.
351
- */
352
- typedef enum
353
- {
354
- ALLOW_ALL_CONNS, /* normal not-shutting-down state */
355
- ALLOW_SUPERUSER_CONNS, /* only superusers can connect */
356
- ALLOW_NO_CONNS /* no new connections allowed, period */
357
- } ConnsAllowedState;
358
-
359
-
360
-
361
- /* Start time of SIGKILL timeout during immediate shutdown or child crash */
362
- /* Zero means timeout is not running */
363
-
364
-
365
- /* Length of said timeout */
366
- #define SIGKILL_CHILDREN_AFTER_SECS 5
367
-
368
- /* T if we've reached PM_RUN */
369
-
370
- __thread bool ClientAuthInProgress = false;
371
- /* T during new-client
372
- * authentication */
373
-
374
- /* stderr redirected for syslogger? */
375
-
376
- /* received START_AUTOVAC_LAUNCHER signal */
377
-
378
-
379
- /* the launcher needs to be signaled to communicate some condition */
380
-
381
-
382
- /* received START_WALRECEIVER signal */
383
-
384
-
385
- /* set when there's a worker that needs to be started up */
386
-
387
-
388
-
389
- #ifdef USE_SSL
390
- /* Set when and if SSL has been initialized properly */
391
- static bool LoadedSSL = false;
392
- #endif
393
-
394
- #ifdef USE_BONJOUR
395
- static DNSServiceRef bonjour_sdref = NULL;
396
- #endif
397
-
398
- /*
399
- * postmaster.c - function prototypes
400
- */
401
- static void CloseServerPorts(int status, Datum arg);
402
- static void unlink_external_pid_file(int status, Datum arg);
403
- static void getInstallationPaths(const char *argv0);
404
- static void checkControlFile(void);
405
- static Port *ConnCreate(int serverFd);
406
- static void ConnFree(Port *port);
407
- static void reset_shared(void);
408
- static void SIGHUP_handler(SIGNAL_ARGS);
409
- static void pmdie(SIGNAL_ARGS);
410
- static void reaper(SIGNAL_ARGS);
411
- static void sigusr1_handler(SIGNAL_ARGS);
412
- static void process_startup_packet_die(SIGNAL_ARGS);
413
- static void dummy_handler(SIGNAL_ARGS);
414
- static void StartupPacketTimeoutHandler(void);
415
- static void CleanupBackend(int pid, int exitstatus);
416
- static bool CleanupBackgroundWorker(int pid, int exitstatus);
417
- static void HandleChildCrash(int pid, int exitstatus, const char *procname);
418
- static void LogChildExit(int lev, const char *procname,
419
- int pid, int exitstatus);
420
- static void PostmasterStateMachine(void);
421
- static void BackendInitialize(Port *port);
422
- static void BackendRun(Port *port) pg_attribute_noreturn();
423
- static void ExitPostmaster(int status) pg_attribute_noreturn();
424
- static int ServerLoop(void);
425
- static int BackendStartup(Port *port);
426
- static int ProcessStartupPacket(Port *port, bool ssl_done, bool gss_done);
427
- static void SendNegotiateProtocolVersion(List *unrecognized_protocol_options);
428
- static void processCancelRequest(Port *port, void *pkt);
429
- static int initMasks(fd_set *rmask);
430
- static void report_fork_failure_to_client(Port *port, int errnum);
431
- static CAC_state canAcceptConnections(int backend_type);
432
- static bool RandomCancelKey(int32 *cancel_key);
433
- static void signal_child(pid_t pid, int signal);
434
- static bool SignalSomeChildren(int signal, int targets);
435
- static void TerminateChildren(int signal);
436
-
437
- #define SignalChildren(sig) SignalSomeChildren(sig, BACKEND_TYPE_ALL)
438
-
439
- static int CountChildren(int target);
440
- static bool assign_backendlist_entry(RegisteredBgWorker *rw);
441
- static void maybe_start_bgworkers(void);
442
- static bool CreateOptsFile(int argc, char *argv[], char *fullprogname);
443
- static pid_t StartChildProcess(AuxProcType type);
444
- static void StartAutovacuumWorker(void);
445
- static void MaybeStartWalReceiver(void);
446
- static void InitPostmasterDeathWatchHandle(void);
447
-
448
- /*
449
- * Archiver is allowed to start up at the current postmaster state?
450
- *
451
- * If WAL archiving is enabled always, we are allowed to start archiver
452
- * even during recovery.
453
- */
454
- #define PgArchStartupAllowed() \
455
- ((XLogArchivingActive() && pmState == PM_RUN) || \
456
- (XLogArchivingAlways() && \
457
- (pmState == PM_RECOVERY || pmState == PM_HOT_STANDBY)))
458
-
459
- #ifdef EXEC_BACKEND
460
-
461
- #ifdef WIN32
462
- #define WNOHANG 0 /* ignored, so any integer value will do */
463
-
464
- static pid_t waitpid(pid_t pid, int *exitstatus, int options);
465
- static void WINAPI pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired);
466
-
467
- static HANDLE win32ChildQueue;
468
-
469
- typedef struct
470
- {
471
- HANDLE waitHandle;
472
- HANDLE procHandle;
473
- DWORD procId;
474
- } win32_deadchild_waitinfo;
475
- #endif /* WIN32 */
476
-
477
- static pid_t backend_forkexec(Port *port);
478
- static pid_t internal_forkexec(int argc, char *argv[], Port *port);
479
-
480
- /* Type for a socket that can be inherited to a client process */
481
- #ifdef WIN32
482
- typedef struct
483
- {
484
- SOCKET origsocket; /* Original socket value, or PGINVALID_SOCKET
485
- * if not a socket */
486
- WSAPROTOCOL_INFO wsainfo;
487
- } InheritableSocket;
488
- #else
489
- typedef int InheritableSocket;
490
- #endif
491
-
492
- /*
493
- * Structure contains all variables passed to exec:ed backends
494
- */
495
- typedef struct
496
- {
497
- Port port;
498
- InheritableSocket portsocket;
499
- char DataDir[MAXPGPATH];
500
- pgsocket ListenSocket[MAXLISTEN];
501
- int32 MyCancelKey;
502
- int MyPMChildSlot;
503
- #ifndef WIN32
504
- unsigned long UsedShmemSegID;
505
- #else
506
- void *ShmemProtectiveRegion;
507
- HANDLE UsedShmemSegID;
508
- #endif
509
- void *UsedShmemSegAddr;
510
- slock_t *ShmemLock;
511
- VariableCache ShmemVariableCache;
512
- Backend *ShmemBackendArray;
513
- #ifndef HAVE_SPINLOCKS
514
- PGSemaphore *SpinlockSemaArray;
515
- #endif
516
- int NamedLWLockTrancheRequests;
517
- NamedLWLockTranche *NamedLWLockTrancheArray;
518
- LWLockPadded *MainLWLockArray;
519
- slock_t *ProcStructLock;
520
- PROC_HDR *ProcGlobal;
521
- PGPROC *AuxiliaryProcs;
522
- PGPROC *PreparedXactProcs;
523
- PMSignalData *PMSignalState;
524
- InheritableSocket pgStatSock;
525
- pid_t PostmasterPid;
526
- TimestampTz PgStartTime;
527
- TimestampTz PgReloadTime;
528
- pg_time_t first_syslogger_file_time;
529
- bool redirection_done;
530
- bool IsBinaryUpgrade;
531
- int max_safe_fds;
532
- int MaxBackends;
533
- #ifdef WIN32
534
- HANDLE PostmasterHandle;
535
- HANDLE initial_signal_pipe;
536
- HANDLE syslogPipe[2];
537
- #else
538
- int postmaster_alive_fds[2];
539
- int syslogPipe[2];
540
- #endif
541
- char my_exec_path[MAXPGPATH];
542
- char pkglib_path[MAXPGPATH];
543
- char ExtraOptions[MAXPGPATH];
544
- } BackendParameters;
545
-
546
- static void read_backend_variables(char *id, Port *port);
547
- static void restore_backend_variables(BackendParameters *param, Port *port);
548
-
549
- #ifndef WIN32
550
- static bool save_backend_variables(BackendParameters *param, Port *port);
551
- #else
552
- static bool save_backend_variables(BackendParameters *param, Port *port,
553
- HANDLE childProcess, pid_t childPid);
554
- #endif
555
-
556
- static void ShmemBackendArrayAdd(Backend *bn);
557
- static void ShmemBackendArrayRemove(Backend *bn);
558
- #endif /* EXEC_BACKEND */
559
-
560
- #define StartupDataBase() StartChildProcess(StartupProcess)
561
- #define StartBackgroundWriter() StartChildProcess(BgWriterProcess)
562
- #define StartCheckpointer() StartChildProcess(CheckpointerProcess)
563
- #define StartWalWriter() StartChildProcess(WalWriterProcess)
564
- #define StartWalReceiver() StartChildProcess(WalReceiverProcess)
565
-
566
- /* Macros to check exit status of a child process */
567
- #define EXIT_STATUS_0(st) ((st) == 0)
568
- #define EXIT_STATUS_1(st) (WIFEXITED(st) && WEXITSTATUS(st) == 1)
569
- #define EXIT_STATUS_3(st) (WIFEXITED(st) && WEXITSTATUS(st) == 3)
570
-
571
- #ifndef WIN32
572
- /*
573
- * File descriptors for pipe used to monitor if postmaster is alive.
574
- * First is POSTMASTER_FD_WATCH, second is POSTMASTER_FD_OWN.
575
- */
576
-
577
- #else
578
- /* Process handle of postmaster used for the same purpose on Windows */
579
- HANDLE PostmasterHandle;
580
- #endif
581
-
582
- /*
583
- * Postmaster main entry point
584
- */
585
- #ifdef SIGTTIN
586
- #endif
587
- #ifdef SIGTTOU
588
- #endif
589
- #ifdef SIGXFSZ
590
- #endif
591
- #ifdef HAVE_INT_OPTRESET
592
- #endif
593
- #ifdef USE_SSL
594
- #endif
595
- #ifdef WIN32
596
- #endif
597
- #ifdef EXEC_BACKEND
598
- #endif
599
- #ifdef USE_BONJOUR
600
- #endif
601
- #ifdef HAVE_UNIX_SOCKETS
602
- #endif
603
- #ifdef HAVE_PTHREAD_IS_THREADED_NP
604
- #endif
605
-
606
-
607
- /*
608
- * on_proc_exit callback to close server's listen sockets
609
- */
610
-
611
-
612
- /*
613
- * on_proc_exit callback to delete external_pid_file
614
- */
615
-
616
-
617
-
618
- /*
619
- * Compute and check the directory paths to files that are part of the
620
- * installation (as deduced from the postgres executable's own location)
621
- */
622
- #ifdef EXEC_BACKEND
623
- #endif
624
-
625
- /*
626
- * Check that pg_control exists in the correct location in the data directory.
627
- *
628
- * No attempt is made to validate the contents of pg_control here. This is
629
- * just a sanity check to see if we are looking at a real data directory.
630
- */
631
-
632
-
633
- /*
634
- * Determine how long should we let ServerLoop sleep.
635
- *
636
- * In normal conditions we wait at most one minute, to ensure that the other
637
- * background tasks handled by ServerLoop get done even when no requests are
638
- * arriving. However, if there are background workers waiting to be started,
639
- * we don't actually sleep so that they are quickly serviced. Other exception
640
- * cases are as shown in the code.
641
- */
642
-
643
-
644
- /*
645
- * Main idle loop of postmaster
646
- *
647
- * NB: Needs to be called with signals blocked
648
- */
649
- #ifdef HAVE_PTHREAD_IS_THREADED_NP
650
- #endif
651
-
652
- /*
653
- * Initialise the masks for select() for the ports we are listening on.
654
- * Return the number of sockets to listen on.
655
- */
656
-
657
-
658
-
659
- /*
660
- * Read a client's startup packet and do something according to it.
661
- *
662
- * Returns STATUS_OK or STATUS_ERROR, or might call ereport(FATAL) and
663
- * not return at all.
664
- *
665
- * (Note that ereport(FATAL) stuff is sent to the client, so only use it
666
- * if that's what you want. Return STATUS_ERROR if you don't want to
667
- * send anything to the client, which would typically be appropriate
668
- * if we detect a communications failure.)
669
- *
670
- * Set ssl_done and/or gss_done when negotiation of an encrypted layer
671
- * (currently, TLS or GSSAPI) is completed. A successful negotiation of either
672
- * encryption layer sets both flags, but a rejected negotiation sets only the
673
- * flag for that layer, since the client may wish to try the other one. We
674
- * should make no assumption here about the order in which the client may make
675
- * requests.
676
- */
677
- #ifdef USE_SSL
678
- #else
679
- #endif
680
- #ifdef USE_SSL
681
- #endif
682
- #ifdef ENABLE_GSS
683
- #endif
684
- #ifdef ENABLE_GSS
685
- #endif
686
-
687
- /*
688
- * Send a NegotiateProtocolVersion to the client. This lets the client know
689
- * that they have requested a newer minor protocol version than we are able
690
- * to speak. We'll speak the highest version we know about; the client can,
691
- * of course, abandon the connection if that's a problem.
692
- *
693
- * We also include in the response a list of protocol options we didn't
694
- * understand. This allows clients to include optional parameters that might
695
- * be present either in newer protocol versions or third-party protocol
696
- * extensions without fear of having to reconnect if those options are not
697
- * understood, while at the same time making certain that the client is aware
698
- * of which options were actually accepted.
699
- */
700
-
701
-
702
- /*
703
- * The client has sent a cancel request packet, not a normal
704
- * start-a-new-connection packet. Perform the necessary processing.
705
- * Nothing is sent back to the client.
706
- */
707
- #ifndef EXEC_BACKEND
708
- #else
709
- #endif
710
- #ifndef EXEC_BACKEND
711
- #else
712
- #endif
713
- #ifndef EXEC_BACKEND /* make GNU Emacs 26.1 see brace balance */
714
- #else
715
- #endif
716
-
717
- /*
718
- * canAcceptConnections --- check to see if database state allows connections
719
- * of the specified type. backend_type can be BACKEND_TYPE_NORMAL,
720
- * BACKEND_TYPE_AUTOVAC, or BACKEND_TYPE_BGWORKER. (Note that we don't yet
721
- * know whether a NORMAL connection might turn into a walsender.)
722
- */
723
-
724
-
725
-
726
- /*
727
- * ConnCreate -- create a local connection data structure
728
- *
729
- * Returns NULL on failure, other than out-of-memory which is fatal.
730
- */
731
-
732
-
733
-
734
- /*
735
- * ConnFree -- free a local connection data structure
736
- *
737
- * Caller has already closed the socket if any, so there's not much
738
- * to do here.
739
- */
740
-
741
-
742
-
743
- /*
744
- * ClosePostmasterPorts -- close all the postmaster's open sockets
745
- *
746
- * This is called during child process startup to release file descriptors
747
- * that are not needed by that child process. The postmaster still has
748
- * them open, of course.
749
- *
750
- * Note: we pass am_syslogger as a boolean because we don't want to set
751
- * the global variable yet when this is called.
752
- */
753
- #ifndef WIN32
754
- #endif
755
- #ifndef WIN32
756
- #else
757
- #endif
758
- #ifdef USE_BONJOUR
759
- #endif
760
-
761
-
762
- /*
763
- * InitProcessGlobals -- set MyProcPid, MyStartTime[stamp], random seeds
764
- *
765
- * Called early in the postmaster and every backend.
766
- */
767
-
768
-
769
-
770
- /*
771
- * reset_shared -- reset shared memory and semaphores
772
- */
773
-
774
-
775
-
776
- /*
777
- * SIGHUP -- reread config files, and tell children to do same
778
- */
779
- #ifdef WIN32
780
- #endif
781
- #ifdef USE_SSL
782
- #endif
783
- #ifdef EXEC_BACKEND
784
- #endif
785
- #ifdef WIN32
786
- #endif
787
-
788
-
789
- /*
790
- * pmdie -- signal handler for processing various postmaster signals.
791
- */
792
- #ifdef WIN32
793
- #endif
794
- #ifdef USE_SYSTEMD
795
- #endif
796
- #ifdef USE_SYSTEMD
797
- #endif
798
- #ifdef USE_SYSTEMD
799
- #endif
800
- #ifdef WIN32
801
- #endif
802
-
803
- /*
804
- * Reaper -- signal handler to cleanup after a child process dies.
805
- */
806
- #ifdef WIN32
807
- #endif
808
- #ifdef USE_SYSTEMD
809
- #endif
810
- #ifdef WIN32
811
- #endif
812
-
813
- /*
814
- * Scan the bgworkers list and see if the given PID (which has just stopped
815
- * or crashed) is in it. Handle its shutdown if so, and return true. If not a
816
- * bgworker, return false.
817
- *
818
- * This is heavily based on CleanupBackend. One important difference is that
819
- * we don't know yet that the dying process is a bgworker, so we must be silent
820
- * until we're sure it is.
821
- */
822
- #ifdef WIN32
823
- #endif
824
- #ifdef EXEC_BACKEND
825
- #endif
826
-
827
- /*
828
- * CleanupBackend -- cleanup after terminated backend.
829
- *
830
- * Remove all local state associated with backend.
831
- *
832
- * If you change this, see also CleanupBackgroundWorker.
833
- */
834
- #ifdef WIN32
835
- #endif
836
- #ifdef EXEC_BACKEND
837
- #endif
838
-
839
- /*
840
- * HandleChildCrash -- cleanup after failed backend, bgwriter, checkpointer,
841
- * walwriter, autovacuum, or background worker.
842
- *
843
- * The objectives here are to clean up our local state about the child
844
- * process, and to signal all other remaining children to quickdie.
845
- */
846
- #ifdef EXEC_BACKEND
847
- #endif
848
- #ifdef EXEC_BACKEND
849
- #endif
850
-
851
- /*
852
- * Log the death of a child process.
853
- */
854
- #if defined(WIN32)
855
- #else
856
- #endif
857
-
858
- /*
859
- * Advance the postmaster's state machine and take actions as appropriate
860
- *
861
- * This is common code for pmdie(), reaper() and sigusr1_handler(), which
862
- * receive the signals that might mean we need to change state.
863
- */
864
-
865
-
866
-
867
- /*
868
- * Send a signal to a postmaster child process
869
- *
870
- * On systems that have setsid(), each child process sets itself up as a
871
- * process group leader. For signals that are generally interpreted in the
872
- * appropriate fashion, we signal the entire process group not just the
873
- * direct child process. This allows us to, for example, SIGQUIT a blocked
874
- * archive_recovery script, or SIGINT a script being run by a backend via
875
- * system().
876
- *
877
- * There is a race condition for recently-forked children: they might not
878
- * have executed setsid() yet. So we signal the child directly as well as
879
- * the group. We assume such a child will handle the signal before trying
880
- * to spawn any grandchild processes. We also assume that signaling the
881
- * child twice will not cause any problems.
882
- */
883
- #ifdef HAVE_SETSID
884
- #endif
885
-
886
- /*
887
- * Send a signal to the targeted children (but NOT special children;
888
- * dead_end children are never signaled, either).
889
- */
890
-
891
-
892
- /*
893
- * Send a termination signal to children. This considers all of our children
894
- * processes, except syslogger and dead_end backends.
895
- */
896
-
897
-
898
- /*
899
- * BackendStartup -- start backend process
900
- *
901
- * returns: STATUS_ERROR if the fork failed, STATUS_OK otherwise.
902
- *
903
- * Note: if you change this code, also consider StartAutovacuumWorker.
904
- */
905
- #ifdef EXEC_BACKEND
906
- #else /* !EXEC_BACKEND */
907
- #endif /* EXEC_BACKEND */
908
- #ifdef EXEC_BACKEND
909
- #endif
910
-
911
- /*
912
- * Try to report backend fork() failure to client before we close the
913
- * connection. Since we do not care to risk blocking the postmaster on
914
- * this connection, we set the connection to non-blocking and try only once.
915
- *
916
- * This is grungy special-purpose code; we cannot use backend libpq since
917
- * it's not up and running.
918
- */
919
-
920
-
921
-
922
- /*
923
- * BackendInitialize -- initialize an interactive (postmaster-child)
924
- * backend process, and collect the client's startup packet.
925
- *
926
- * returns: nothing. Will not return at all if there's any failure.
927
- *
928
- * Note: this code does not depend on having any access to shared memory.
929
- * In the EXEC_BACKEND case, we are physically attached to shared memory
930
- * but have not yet set up most of our local pointers to shmem structures.
931
- */
932
-
933
-
934
-
935
- /*
936
- * BackendRun -- set up the backend's argument list and invoke PostgresMain()
937
- *
938
- * returns:
939
- * Shouldn't return at all.
940
- * If PostgresMain() fails, return status.
941
- */
942
-
943
-
944
-
945
- #ifdef EXEC_BACKEND
946
-
947
- /*
948
- * postmaster_forkexec -- fork and exec a postmaster subprocess
949
- *
950
- * The caller must have set up the argv array already, except for argv[2]
951
- * which will be filled with the name of the temp variable file.
952
- *
953
- * Returns the child process PID, or -1 on fork failure (a suitable error
954
- * message has been logged on failure).
955
- *
956
- * All uses of this routine will dispatch to SubPostmasterMain in the
957
- * child process.
958
- */
959
- pid_t
960
- postmaster_forkexec(int argc, char *argv[])
961
- {
962
- Port port;
963
-
964
- /* This entry point passes dummy values for the Port variables */
965
- memset(&port, 0, sizeof(port));
966
- return internal_forkexec(argc, argv, &port);
967
- }
968
-
969
- /*
970
- * backend_forkexec -- fork/exec off a backend process
971
- *
972
- * Some operating systems (WIN32) don't have fork() so we have to simulate
973
- * it by storing parameters that need to be passed to the child and
974
- * then create a new child process.
975
- *
976
- * returns the pid of the fork/exec'd process, or -1 on failure
977
- */
978
- static pid_t
979
- backend_forkexec(Port *port)
980
- {
981
- char *av[4];
982
- int ac = 0;
983
-
984
- av[ac++] = "postgres";
985
- av[ac++] = "--forkbackend";
986
- av[ac++] = NULL; /* filled in by internal_forkexec */
987
-
988
- av[ac] = NULL;
989
- Assert(ac < lengthof(av));
990
-
991
- return internal_forkexec(ac, av, port);
992
- }
993
-
994
- #ifndef WIN32
995
-
996
- /*
997
- * internal_forkexec non-win32 implementation
998
- *
999
- * - writes out backend variables to the parameter file
1000
- * - fork():s, and then exec():s the child process
1001
- */
1002
- static pid_t
1003
- internal_forkexec(int argc, char *argv[], Port *port)
1004
- {
1005
- static unsigned long tmpBackendFileNum = 0;
1006
- pid_t pid;
1007
- char tmpfilename[MAXPGPATH];
1008
- BackendParameters param;
1009
- FILE *fp;
1010
-
1011
- if (!save_backend_variables(&param, port))
1012
- return -1; /* log made by save_backend_variables */
1013
-
1014
- /* Calculate name for temp file */
1015
- snprintf(tmpfilename, MAXPGPATH, "%s/%s.backend_var.%d.%lu",
1016
- PG_TEMP_FILES_DIR, PG_TEMP_FILE_PREFIX,
1017
- MyProcPid, ++tmpBackendFileNum);
1018
-
1019
- /* Open file */
1020
- fp = AllocateFile(tmpfilename, PG_BINARY_W);
1021
- if (!fp)
1022
- {
1023
- /*
1024
- * As in OpenTemporaryFileInTablespace, try to make the temp-file
1025
- * directory, ignoring errors.
1026
- */
1027
- (void) MakePGDirectory(PG_TEMP_FILES_DIR);
1028
-
1029
- fp = AllocateFile(tmpfilename, PG_BINARY_W);
1030
- if (!fp)
1031
- {
1032
- ereport(LOG,
1033
- (errcode_for_file_access(),
1034
- errmsg("could not create file \"%s\": %m",
1035
- tmpfilename)));
1036
- return -1;
1037
- }
1038
- }
1039
-
1040
- if (fwrite(&param, sizeof(param), 1, fp) != 1)
1041
- {
1042
- ereport(LOG,
1043
- (errcode_for_file_access(),
1044
- errmsg("could not write to file \"%s\": %m", tmpfilename)));
1045
- FreeFile(fp);
1046
- return -1;
1047
- }
1048
-
1049
- /* Release file */
1050
- if (FreeFile(fp))
1051
- {
1052
- ereport(LOG,
1053
- (errcode_for_file_access(),
1054
- errmsg("could not write to file \"%s\": %m", tmpfilename)));
1055
- return -1;
1056
- }
1057
-
1058
- /* Make sure caller set up argv properly */
1059
- Assert(argc >= 3);
1060
- Assert(argv[argc] == NULL);
1061
- Assert(strncmp(argv[1], "--fork", 6) == 0);
1062
- Assert(argv[2] == NULL);
1063
-
1064
- /* Insert temp file name after --fork argument */
1065
- argv[2] = tmpfilename;
1066
-
1067
- /* Fire off execv in child */
1068
- if ((pid = fork_process()) == 0)
1069
- {
1070
- if (execv(postgres_exec_path, argv) < 0)
1071
- {
1072
- ereport(LOG,
1073
- (errmsg("could not execute server process \"%s\": %m",
1074
- postgres_exec_path)));
1075
- /* We're already in the child process here, can't return */
1076
- exit(1);
1077
- }
1078
- }
1079
-
1080
- return pid; /* Parent returns pid, or -1 on fork failure */
1081
- }
1082
- #else /* WIN32 */
1083
-
1084
- /*
1085
- * internal_forkexec win32 implementation
1086
- *
1087
- * - starts backend using CreateProcess(), in suspended state
1088
- * - writes out backend variables to the parameter file
1089
- * - during this, duplicates handles and sockets required for
1090
- * inheritance into the new process
1091
- * - resumes execution of the new process once the backend parameter
1092
- * file is complete.
1093
- */
1094
- static pid_t
1095
- internal_forkexec(int argc, char *argv[], Port *port)
1096
- {
1097
- int retry_count = 0;
1098
- STARTUPINFO si;
1099
- PROCESS_INFORMATION pi;
1100
- int i;
1101
- int j;
1102
- char cmdLine[MAXPGPATH * 2];
1103
- HANDLE paramHandle;
1104
- BackendParameters *param;
1105
- SECURITY_ATTRIBUTES sa;
1106
- char paramHandleStr[32];
1107
- win32_deadchild_waitinfo *childinfo;
1108
-
1109
- /* Make sure caller set up argv properly */
1110
- Assert(argc >= 3);
1111
- Assert(argv[argc] == NULL);
1112
- Assert(strncmp(argv[1], "--fork", 6) == 0);
1113
- Assert(argv[2] == NULL);
1114
-
1115
- /* Resume here if we need to retry */
1116
- retry:
1117
-
1118
- /* Set up shared memory for parameter passing */
1119
- ZeroMemory(&sa, sizeof(sa));
1120
- sa.nLength = sizeof(sa);
1121
- sa.bInheritHandle = TRUE;
1122
- paramHandle = CreateFileMapping(INVALID_HANDLE_VALUE,
1123
- &sa,
1124
- PAGE_READWRITE,
1125
- 0,
1126
- sizeof(BackendParameters),
1127
- NULL);
1128
- if (paramHandle == INVALID_HANDLE_VALUE)
1129
- {
1130
- elog(LOG, "could not create backend parameter file mapping: error code %lu",
1131
- GetLastError());
1132
- return -1;
1133
- }
1134
-
1135
- param = MapViewOfFile(paramHandle, FILE_MAP_WRITE, 0, 0, sizeof(BackendParameters));
1136
- if (!param)
1137
- {
1138
- elog(LOG, "could not map backend parameter memory: error code %lu",
1139
- GetLastError());
1140
- CloseHandle(paramHandle);
1141
- return -1;
1142
- }
1143
-
1144
- /* Insert temp file name after --fork argument */
1145
- #ifdef _WIN64
1146
- sprintf(paramHandleStr, "%llu", (LONG_PTR) paramHandle);
1147
- #else
1148
- sprintf(paramHandleStr, "%lu", (DWORD) paramHandle);
1149
- #endif
1150
- argv[2] = paramHandleStr;
1151
-
1152
- /* Format the cmd line */
1153
- cmdLine[sizeof(cmdLine) - 1] = '\0';
1154
- cmdLine[sizeof(cmdLine) - 2] = '\0';
1155
- snprintf(cmdLine, sizeof(cmdLine) - 1, "\"%s\"", postgres_exec_path);
1156
- i = 0;
1157
- while (argv[++i] != NULL)
1158
- {
1159
- j = strlen(cmdLine);
1160
- snprintf(cmdLine + j, sizeof(cmdLine) - 1 - j, " \"%s\"", argv[i]);
1161
- }
1162
- if (cmdLine[sizeof(cmdLine) - 2] != '\0')
1163
- {
1164
- elog(LOG, "subprocess command line too long");
1165
- UnmapViewOfFile(param);
1166
- CloseHandle(paramHandle);
1167
- return -1;
1168
- }
1169
-
1170
- memset(&pi, 0, sizeof(pi));
1171
- memset(&si, 0, sizeof(si));
1172
- si.cb = sizeof(si);
1173
-
1174
- /*
1175
- * Create the subprocess in a suspended state. This will be resumed later,
1176
- * once we have written out the parameter file.
1177
- */
1178
- if (!CreateProcess(NULL, cmdLine, NULL, NULL, TRUE, CREATE_SUSPENDED,
1179
- NULL, NULL, &si, &pi))
1180
- {
1181
- elog(LOG, "CreateProcess call failed: %m (error code %lu)",
1182
- GetLastError());
1183
- UnmapViewOfFile(param);
1184
- CloseHandle(paramHandle);
1185
- return -1;
1186
- }
1187
-
1188
- if (!save_backend_variables(param, port, pi.hProcess, pi.dwProcessId))
1189
- {
1190
- /*
1191
- * log made by save_backend_variables, but we have to clean up the
1192
- * mess with the half-started process
1193
- */
1194
- if (!TerminateProcess(pi.hProcess, 255))
1195
- ereport(LOG,
1196
- (errmsg_internal("could not terminate unstarted process: error code %lu",
1197
- GetLastError())));
1198
- CloseHandle(pi.hProcess);
1199
- CloseHandle(pi.hThread);
1200
- UnmapViewOfFile(param);
1201
- CloseHandle(paramHandle);
1202
- return -1; /* log made by save_backend_variables */
1203
- }
1204
-
1205
- /* Drop the parameter shared memory that is now inherited to the backend */
1206
- if (!UnmapViewOfFile(param))
1207
- elog(LOG, "could not unmap view of backend parameter file: error code %lu",
1208
- GetLastError());
1209
- if (!CloseHandle(paramHandle))
1210
- elog(LOG, "could not close handle to backend parameter file: error code %lu",
1211
- GetLastError());
1212
-
1213
- /*
1214
- * Reserve the memory region used by our main shared memory segment before
1215
- * we resume the child process. Normally this should succeed, but if ASLR
1216
- * is active then it might sometimes fail due to the stack or heap having
1217
- * gotten mapped into that range. In that case, just terminate the
1218
- * process and retry.
1219
- */
1220
- if (!pgwin32_ReserveSharedMemoryRegion(pi.hProcess))
1221
- {
1222
- /* pgwin32_ReserveSharedMemoryRegion already made a log entry */
1223
- if (!TerminateProcess(pi.hProcess, 255))
1224
- ereport(LOG,
1225
- (errmsg_internal("could not terminate process that failed to reserve memory: error code %lu",
1226
- GetLastError())));
1227
- CloseHandle(pi.hProcess);
1228
- CloseHandle(pi.hThread);
1229
- if (++retry_count < 100)
1230
- goto retry;
1231
- ereport(LOG,
1232
- (errmsg("giving up after too many tries to reserve shared memory"),
1233
- errhint("This might be caused by ASLR or antivirus software.")));
1234
- return -1;
1235
- }
1236
-
1237
- /*
1238
- * Now that the backend variables are written out, we start the child
1239
- * thread so it can start initializing while we set up the rest of the
1240
- * parent state.
1241
- */
1242
- if (ResumeThread(pi.hThread) == -1)
1243
- {
1244
- if (!TerminateProcess(pi.hProcess, 255))
1245
- {
1246
- ereport(LOG,
1247
- (errmsg_internal("could not terminate unstartable process: error code %lu",
1248
- GetLastError())));
1249
- CloseHandle(pi.hProcess);
1250
- CloseHandle(pi.hThread);
1251
- return -1;
1252
- }
1253
- CloseHandle(pi.hProcess);
1254
- CloseHandle(pi.hThread);
1255
- ereport(LOG,
1256
- (errmsg_internal("could not resume thread of unstarted process: error code %lu",
1257
- GetLastError())));
1258
- return -1;
1259
- }
1260
-
1261
- /*
1262
- * Queue a waiter to signal when this child dies. The wait will be handled
1263
- * automatically by an operating system thread pool.
1264
- *
1265
- * Note: use malloc instead of palloc, since it needs to be thread-safe.
1266
- * Struct will be free():d from the callback function that runs on a
1267
- * different thread.
1268
- */
1269
- childinfo = malloc(sizeof(win32_deadchild_waitinfo));
1270
- if (!childinfo)
1271
- ereport(FATAL,
1272
- (errcode(ERRCODE_OUT_OF_MEMORY),
1273
- errmsg("out of memory")));
1274
-
1275
- childinfo->procHandle = pi.hProcess;
1276
- childinfo->procId = pi.dwProcessId;
1277
-
1278
- if (!RegisterWaitForSingleObject(&childinfo->waitHandle,
1279
- pi.hProcess,
1280
- pgwin32_deadchild_callback,
1281
- childinfo,
1282
- INFINITE,
1283
- WT_EXECUTEONLYONCE | WT_EXECUTEINWAITTHREAD))
1284
- ereport(FATAL,
1285
- (errmsg_internal("could not register process for wait: error code %lu",
1286
- GetLastError())));
1287
-
1288
- /* Don't close pi.hProcess here - the wait thread needs access to it */
1289
-
1290
- CloseHandle(pi.hThread);
1291
-
1292
- return pi.dwProcessId;
1293
- }
1294
- #endif /* WIN32 */
1295
-
1296
-
1297
- /*
1298
- * SubPostmasterMain -- Get the fork/exec'd process into a state equivalent
1299
- * to what it would be if we'd simply forked on Unix, and then
1300
- * dispatch to the appropriate place.
1301
- *
1302
- * The first two command line arguments are expected to be "--forkFOO"
1303
- * (where FOO indicates which postmaster child we are to become), and
1304
- * the name of a variables file that we can read to load data that would
1305
- * have been inherited by fork() on Unix. Remaining arguments go to the
1306
- * subprocess FooMain() routine.
1307
- */
1308
- void
1309
- SubPostmasterMain(int argc, char *argv[])
1310
- {
1311
- Port port;
1312
-
1313
- /* In EXEC_BACKEND case we will not have inherited these settings */
1314
- IsPostmasterEnvironment = true;
1315
- whereToSendOutput = DestNone;
1316
-
1317
- /* Setup as postmaster child */
1318
- InitPostmasterChild();
1319
-
1320
- /* Setup essential subsystems (to ensure elog() behaves sanely) */
1321
- InitializeGUCOptions();
1322
-
1323
- /* Check we got appropriate args */
1324
- if (argc < 3)
1325
- elog(FATAL, "invalid subpostmaster invocation");
1326
-
1327
- /* Read in the variables file */
1328
- memset(&port, 0, sizeof(Port));
1329
- read_backend_variables(argv[2], &port);
1330
-
1331
- /* Close the postmaster's sockets (as soon as we know them) */
1332
- ClosePostmasterPorts(strcmp(argv[1], "--forklog") == 0);
1333
-
1334
- /*
1335
- * If appropriate, physically re-attach to shared memory segment. We want
1336
- * to do this before going any further to ensure that we can attach at the
1337
- * same address the postmaster used. On the other hand, if we choose not
1338
- * to re-attach, we may have other cleanup to do.
1339
- *
1340
- * If testing EXEC_BACKEND on Linux, you should run this as root before
1341
- * starting the postmaster:
1342
- *
1343
- * echo 0 >/proc/sys/kernel/randomize_va_space
1344
- *
1345
- * This prevents using randomized stack and code addresses that cause the
1346
- * child process's memory map to be different from the parent's, making it
1347
- * sometimes impossible to attach to shared memory at the desired address.
1348
- * Return the setting to its old value (usually '1' or '2') when finished.
1349
- */
1350
- if (strcmp(argv[1], "--forkbackend") == 0 ||
1351
- strcmp(argv[1], "--forkavlauncher") == 0 ||
1352
- strcmp(argv[1], "--forkavworker") == 0 ||
1353
- strcmp(argv[1], "--forkboot") == 0 ||
1354
- strncmp(argv[1], "--forkbgworker=", 15) == 0)
1355
- PGSharedMemoryReAttach();
1356
- else
1357
- PGSharedMemoryNoReAttach();
1358
-
1359
- /* autovacuum needs this set before calling InitProcess */
1360
- if (strcmp(argv[1], "--forkavlauncher") == 0)
1361
- AutovacuumLauncherIAm();
1362
- if (strcmp(argv[1], "--forkavworker") == 0)
1363
- AutovacuumWorkerIAm();
1364
-
1365
- /*
1366
- * Start our win32 signal implementation. This has to be done after we
1367
- * read the backend variables, because we need to pick up the signal pipe
1368
- * from the parent process.
1369
- */
1370
- #ifdef WIN32
1371
- pgwin32_signal_initialize();
1372
- #endif
1373
-
1374
- /* In EXEC_BACKEND case we will not have inherited these settings */
1375
- pqinitmask();
1376
- PG_SETMASK(&BlockSig);
1377
-
1378
- /* Read in remaining GUC variables */
1379
- read_nondefault_variables();
1380
-
1381
- /*
1382
- * Check that the data directory looks valid, which will also check the
1383
- * privileges on the data directory and update our umask and file/group
1384
- * variables for creating files later. Note: this should really be done
1385
- * before we create any files or directories.
1386
- */
1387
- checkDataDir();
1388
-
1389
- /*
1390
- * (re-)read control file, as it contains config. The postmaster will
1391
- * already have read this, but this process doesn't know about that.
1392
- */
1393
- LocalProcessControlFile(false);
1394
-
1395
- /*
1396
- * Reload any libraries that were preloaded by the postmaster. Since we
1397
- * exec'd this process, those libraries didn't come along with us; but we
1398
- * should load them into all child processes to be consistent with the
1399
- * non-EXEC_BACKEND behavior.
1400
- */
1401
- process_shared_preload_libraries();
1402
-
1403
- /* Run backend or appropriate child */
1404
- if (strcmp(argv[1], "--forkbackend") == 0)
1405
- {
1406
- Assert(argc == 3); /* shouldn't be any more args */
1407
-
1408
- /*
1409
- * Need to reinitialize the SSL library in the backend, since the
1410
- * context structures contain function pointers and cannot be passed
1411
- * through the parameter file.
1412
- *
1413
- * If for some reason reload fails (maybe the user installed broken
1414
- * key files), soldier on without SSL; that's better than all
1415
- * connections becoming impossible.
1416
- *
1417
- * XXX should we do this in all child processes? For the moment it's
1418
- * enough to do it in backend children.
1419
- */
1420
- #ifdef USE_SSL
1421
- if (EnableSSL)
1422
- {
1423
- if (secure_initialize(false) == 0)
1424
- LoadedSSL = true;
1425
- else
1426
- ereport(LOG,
1427
- (errmsg("SSL configuration could not be loaded in child process")));
1428
- }
1429
- #endif
1430
-
1431
- /*
1432
- * Perform additional initialization and collect startup packet.
1433
- *
1434
- * We want to do this before InitProcess() for a couple of reasons: 1.
1435
- * so that we aren't eating up a PGPROC slot while waiting on the
1436
- * client. 2. so that if InitProcess() fails due to being out of
1437
- * PGPROC slots, we have already initialized libpq and are able to
1438
- * report the error to the client.
1439
- */
1440
- BackendInitialize(&port);
1441
-
1442
- /* Restore basic shared memory pointers */
1443
- InitShmemAccess(UsedShmemSegAddr);
1444
-
1445
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1446
- InitProcess();
1447
-
1448
- /* Attach process to shared data structures */
1449
- CreateSharedMemoryAndSemaphores();
1450
-
1451
- /* And run the backend */
1452
- BackendRun(&port); /* does not return */
1453
- }
1454
- if (strcmp(argv[1], "--forkboot") == 0)
1455
- {
1456
- /* Restore basic shared memory pointers */
1457
- InitShmemAccess(UsedShmemSegAddr);
1458
-
1459
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1460
- InitAuxiliaryProcess();
1461
-
1462
- /* Attach process to shared data structures */
1463
- CreateSharedMemoryAndSemaphores();
1464
-
1465
- AuxiliaryProcessMain(argc - 2, argv + 2); /* does not return */
1466
- }
1467
- if (strcmp(argv[1], "--forkavlauncher") == 0)
1468
- {
1469
- /* Restore basic shared memory pointers */
1470
- InitShmemAccess(UsedShmemSegAddr);
1471
-
1472
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1473
- InitProcess();
1474
-
1475
- /* Attach process to shared data structures */
1476
- CreateSharedMemoryAndSemaphores();
1477
-
1478
- AutoVacLauncherMain(argc - 2, argv + 2); /* does not return */
1479
- }
1480
- if (strcmp(argv[1], "--forkavworker") == 0)
1481
- {
1482
- /* Restore basic shared memory pointers */
1483
- InitShmemAccess(UsedShmemSegAddr);
1484
-
1485
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1486
- InitProcess();
1487
-
1488
- /* Attach process to shared data structures */
1489
- CreateSharedMemoryAndSemaphores();
1490
-
1491
- AutoVacWorkerMain(argc - 2, argv + 2); /* does not return */
1492
- }
1493
- if (strncmp(argv[1], "--forkbgworker=", 15) == 0)
1494
- {
1495
- int shmem_slot;
1496
-
1497
- /* do this as early as possible; in particular, before InitProcess() */
1498
- IsBackgroundWorker = true;
1499
-
1500
- /* Restore basic shared memory pointers */
1501
- InitShmemAccess(UsedShmemSegAddr);
1502
-
1503
- /* Need a PGPROC to run CreateSharedMemoryAndSemaphores */
1504
- InitProcess();
1505
-
1506
- /* Attach process to shared data structures */
1507
- CreateSharedMemoryAndSemaphores();
1508
-
1509
- /* Fetch MyBgworkerEntry from shared memory */
1510
- shmem_slot = atoi(argv[1] + 15);
1511
- MyBgworkerEntry = BackgroundWorkerEntry(shmem_slot);
1512
-
1513
- StartBackgroundWorker();
1514
- }
1515
- if (strcmp(argv[1], "--forkarch") == 0)
1516
- {
1517
- /* Do not want to attach to shared memory */
1518
-
1519
- PgArchiverMain(argc, argv); /* does not return */
1520
- }
1521
- if (strcmp(argv[1], "--forkcol") == 0)
1522
- {
1523
- /* Do not want to attach to shared memory */
1524
-
1525
- PgstatCollectorMain(argc, argv); /* does not return */
1526
- }
1527
- if (strcmp(argv[1], "--forklog") == 0)
1528
- {
1529
- /* Do not want to attach to shared memory */
1530
-
1531
- SysLoggerMain(argc, argv); /* does not return */
1532
- }
1533
-
1534
- abort(); /* shouldn't get here */
1535
- }
1536
- #endif /* EXEC_BACKEND */
1537
-
1538
-
1539
- /*
1540
- * ExitPostmaster -- cleanup
1541
- *
1542
- * Do NOT call exit() directly --- always go through here!
1543
- */
1544
- #ifdef HAVE_PTHREAD_IS_THREADED_NP
1545
- #endif
1546
-
1547
- /*
1548
- * sigusr1_handler - handle signal conditions from child processes
1549
- */
1550
- #ifdef WIN32
1551
- #endif
1552
- #ifdef USE_SYSTEMD
1553
- #endif
1554
- #ifdef USE_SYSTEMD
1555
- #endif
1556
- #ifdef WIN32
1557
- #endif
1558
-
1559
- /*
1560
- * SIGTERM while processing startup packet.
1561
- * Clean up and exit(1).
1562
- *
1563
- * Running proc_exit() from a signal handler is pretty unsafe, since we
1564
- * can't know what code we've interrupted. But the alternative of using
1565
- * _exit(2) is also unpalatable, since it'd mean that a "fast shutdown"
1566
- * would cause a database crash cycle (forcing WAL replay at restart)
1567
- * if any sessions are in authentication. So we live with it for now.
1568
- *
1569
- * One might be tempted to try to send a message indicating why we are
1570
- * disconnecting. However, that would make this even more unsafe. Also,
1571
- * it seems undesirable to provide clues about the database's state to
1572
- * a client that has not yet completed authentication.
1573
- */
1574
-
1575
-
1576
- /*
1577
- * Dummy signal handler
1578
- *
1579
- * We use this for signals that we don't actually use in the postmaster,
1580
- * but we do use in backends. If we were to SIG_IGN such signals in the
1581
- * postmaster, then a newly started backend might drop a signal that arrives
1582
- * before it's able to reconfigure its signal processing. (See notes in
1583
- * tcop/postgres.c.)
1584
- */
1585
-
1586
-
1587
- /*
1588
- * Timeout while processing startup packet.
1589
- * As for process_startup_packet_die(), we clean up and exit(1).
1590
- *
1591
- * This is theoretically just as hazardous as in process_startup_packet_die(),
1592
- * although in practice we're almost certainly waiting for client input,
1593
- * which greatly reduces the risk.
1594
- */
1595
-
1596
-
1597
-
1598
- /*
1599
- * Generate a random cancel key.
1600
- */
1601
-
1602
-
1603
- /*
1604
- * Count up number of child processes of specified types (dead_end children
1605
- * are always excluded).
1606
- */
1607
-
1608
-
1609
-
1610
- /*
1611
- * StartChildProcess -- start an auxiliary process for the postmaster
1612
- *
1613
- * "type" determines what kind of child will be started. All child types
1614
- * initially go to AuxiliaryProcessMain, which will handle common setup.
1615
- *
1616
- * Return value of StartChildProcess is subprocess' PID, or 0 if failed
1617
- * to start subprocess.
1618
- */
1619
- #ifdef EXEC_BACKEND
1620
- #endif
1621
- #ifdef EXEC_BACKEND
1622
- #else /* !EXEC_BACKEND */
1623
- #endif /* EXEC_BACKEND */
1624
-
1625
- /*
1626
- * StartAutovacuumWorker
1627
- * Start an autovac worker process.
1628
- *
1629
- * This function is here because it enters the resulting PID into the
1630
- * postmaster's private backends list.
1631
- *
1632
- * NB -- this code very roughly matches BackendStartup.
1633
- */
1634
- #ifdef EXEC_BACKEND
1635
- #endif
1636
-
1637
- /*
1638
- * MaybeStartWalReceiver
1639
- * Start the WAL receiver process, if not running and our state allows.
1640
- *
1641
- * Note: if WalReceiverPID is already nonzero, it might seem that we should
1642
- * clear WalReceiverRequested. However, there's a race condition if the
1643
- * walreceiver terminates and the startup process immediately requests a new
1644
- * one: it's quite possible to get the signal for the request before reaping
1645
- * the dead walreceiver process. Better to risk launching an extra
1646
- * walreceiver than to miss launching one we need. (The walreceiver code
1647
- * has logic to recognize that it should go away if not needed.)
1648
- */
1649
-
1650
-
1651
-
1652
- /*
1653
- * Create the opts file
1654
- */
1655
- #define OPTS_FILE "postmaster.opts"
1656
-
1657
-
1658
- /*
1659
- * MaxLivePostmasterChildren
1660
- *
1661
- * This reports the number of entries needed in per-child-process arrays
1662
- * (the PMChildFlags array, and if EXEC_BACKEND the ShmemBackendArray).
1663
- * These arrays include regular backends, autovac workers, walsenders
1664
- * and background workers, but not special children nor dead_end children.
1665
- * This allows the arrays to have a fixed maximum size, to wit the same
1666
- * too-many-children limit enforced by canAcceptConnections(). The exact value
1667
- * isn't too critical as long as it's more than MaxBackends.
1668
- */
1669
-
1670
-
1671
- /*
1672
- * Connect background worker to a database.
1673
- */
1674
-
1675
-
1676
- /*
1677
- * Connect background worker to a database using OIDs.
1678
- */
1679
-
1680
-
1681
- /*
1682
- * Block/unblock signals in a background worker
1683
- */
1684
-
1685
-
1686
-
1687
-
1688
- #ifdef EXEC_BACKEND
1689
- static pid_t
1690
- bgworker_forkexec(int shmem_slot)
1691
- {
1692
- char *av[10];
1693
- int ac = 0;
1694
- char forkav[MAXPGPATH];
1695
-
1696
- snprintf(forkav, MAXPGPATH, "--forkbgworker=%d", shmem_slot);
1697
-
1698
- av[ac++] = "postgres";
1699
- av[ac++] = forkav;
1700
- av[ac++] = NULL; /* filled in by postmaster_forkexec */
1701
- av[ac] = NULL;
1702
-
1703
- Assert(ac < lengthof(av));
1704
-
1705
- return postmaster_forkexec(ac, av);
1706
- }
1707
- #endif
1708
-
1709
- /*
1710
- * Start a new bgworker.
1711
- * Starting time conditions must have been checked already.
1712
- *
1713
- * Returns true on success, false on failure.
1714
- * In either case, update the RegisteredBgWorker's state appropriately.
1715
- *
1716
- * This code is heavily based on autovacuum.c, q.v.
1717
- */
1718
- #ifdef EXEC_BACKEND
1719
- #else
1720
- #endif
1721
- #ifndef EXEC_BACKEND
1722
- #endif
1723
- #ifdef EXEC_BACKEND
1724
- #endif
1725
-
1726
- /*
1727
- * Does the current postmaster state require starting a worker with the
1728
- * specified start_time?
1729
- */
1730
-
1731
-
1732
- /*
1733
- * Allocate the Backend struct for a connected background worker, but don't
1734
- * add it to the list of backends just yet.
1735
- *
1736
- * On failure, return false without changing any worker state.
1737
- *
1738
- * Some info from the Backend is copied into the passed rw.
1739
- */
1740
-
1741
-
1742
- /*
1743
- * If the time is right, start background worker(s).
1744
- *
1745
- * As a side effect, the bgworker control variables are set or reset
1746
- * depending on whether more workers may need to be started.
1747
- *
1748
- * We limit the number of workers started per call, to avoid consuming the
1749
- * postmaster's attention for too long when many such requests are pending.
1750
- * As long as StartWorkerNeeded is true, ServerLoop will not block and will
1751
- * call this function again after dealing with any other issues.
1752
- */
1753
- #define MAX_BGWORKERS_TO_LAUNCH 100
1754
-
1755
- /*
1756
- * When a backend asks to be notified about worker state changes, we
1757
- * set a flag in its backend entry. The background worker machinery needs
1758
- * to know when such backends exit.
1759
- */
1760
-
1761
-
1762
- #ifdef EXEC_BACKEND
1763
-
1764
- /*
1765
- * The following need to be available to the save/restore_backend_variables
1766
- * functions. They are marked NON_EXEC_STATIC in their home modules.
1767
- */
1768
- extern slock_t *ShmemLock;
1769
- extern slock_t *ProcStructLock;
1770
- extern PGPROC *AuxiliaryProcs;
1771
- extern PMSignalData *PMSignalState;
1772
- extern pgsocket pgStatSock;
1773
- extern pg_time_t first_syslogger_file_time;
1774
-
1775
- #ifndef WIN32
1776
- #define write_inheritable_socket(dest, src, childpid) ((*(dest) = (src)), true)
1777
- #define read_inheritable_socket(dest, src) (*(dest) = *(src))
1778
- #else
1779
- static bool write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE child);
1780
- static bool write_inheritable_socket(InheritableSocket *dest, SOCKET src,
1781
- pid_t childPid);
1782
- static void read_inheritable_socket(SOCKET *dest, InheritableSocket *src);
1783
- #endif
1784
-
1785
-
1786
- /* Save critical backend variables into the BackendParameters struct */
1787
- #ifndef WIN32
1788
- static bool
1789
- save_backend_variables(BackendParameters *param, Port *port)
1790
- #else
1791
- static bool
1792
- save_backend_variables(BackendParameters *param, Port *port,
1793
- HANDLE childProcess, pid_t childPid)
1794
- #endif
1795
- {
1796
- memcpy(&param->port, port, sizeof(Port));
1797
- if (!write_inheritable_socket(&param->portsocket, port->sock, childPid))
1798
- return false;
1799
-
1800
- strlcpy(param->DataDir, DataDir, MAXPGPATH);
1801
-
1802
- memcpy(&param->ListenSocket, &ListenSocket, sizeof(ListenSocket));
1803
-
1804
- param->MyCancelKey = MyCancelKey;
1805
- param->MyPMChildSlot = MyPMChildSlot;
1806
-
1807
- #ifdef WIN32
1808
- param->ShmemProtectiveRegion = ShmemProtectiveRegion;
1809
- #endif
1810
- param->UsedShmemSegID = UsedShmemSegID;
1811
- param->UsedShmemSegAddr = UsedShmemSegAddr;
1812
-
1813
- param->ShmemLock = ShmemLock;
1814
- param->ShmemVariableCache = ShmemVariableCache;
1815
- param->ShmemBackendArray = ShmemBackendArray;
1816
-
1817
- #ifndef HAVE_SPINLOCKS
1818
- param->SpinlockSemaArray = SpinlockSemaArray;
1819
- #endif
1820
- param->NamedLWLockTrancheRequests = NamedLWLockTrancheRequests;
1821
- param->NamedLWLockTrancheArray = NamedLWLockTrancheArray;
1822
- param->MainLWLockArray = MainLWLockArray;
1823
- param->ProcStructLock = ProcStructLock;
1824
- param->ProcGlobal = ProcGlobal;
1825
- param->AuxiliaryProcs = AuxiliaryProcs;
1826
- param->PreparedXactProcs = PreparedXactProcs;
1827
- param->PMSignalState = PMSignalState;
1828
- if (!write_inheritable_socket(&param->pgStatSock, pgStatSock, childPid))
1829
- return false;
1830
-
1831
- param->PostmasterPid = PostmasterPid;
1832
- param->PgStartTime = PgStartTime;
1833
- param->PgReloadTime = PgReloadTime;
1834
- param->first_syslogger_file_time = first_syslogger_file_time;
1835
-
1836
- param->redirection_done = redirection_done;
1837
- param->IsBinaryUpgrade = IsBinaryUpgrade;
1838
- param->max_safe_fds = max_safe_fds;
1839
-
1840
- param->MaxBackends = MaxBackends;
1841
-
1842
- #ifdef WIN32
1843
- param->PostmasterHandle = PostmasterHandle;
1844
- if (!write_duplicated_handle(&param->initial_signal_pipe,
1845
- pgwin32_create_signal_listener(childPid),
1846
- childProcess))
1847
- return false;
1848
- #else
1849
- memcpy(&param->postmaster_alive_fds, &postmaster_alive_fds,
1850
- sizeof(postmaster_alive_fds));
1851
- #endif
1852
-
1853
- memcpy(&param->syslogPipe, &syslogPipe, sizeof(syslogPipe));
1854
-
1855
- strlcpy(param->my_exec_path, my_exec_path, MAXPGPATH);
1856
-
1857
- strlcpy(param->pkglib_path, pkglib_path, MAXPGPATH);
1858
-
1859
- strlcpy(param->ExtraOptions, ExtraOptions, MAXPGPATH);
1860
-
1861
- return true;
1862
- }
1863
-
1864
-
1865
- #ifdef WIN32
1866
- /*
1867
- * Duplicate a handle for usage in a child process, and write the child
1868
- * process instance of the handle to the parameter file.
1869
- */
1870
- static bool
1871
- write_duplicated_handle(HANDLE *dest, HANDLE src, HANDLE childProcess)
1872
- {
1873
- HANDLE hChild = INVALID_HANDLE_VALUE;
1874
-
1875
- if (!DuplicateHandle(GetCurrentProcess(),
1876
- src,
1877
- childProcess,
1878
- &hChild,
1879
- 0,
1880
- TRUE,
1881
- DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS))
1882
- {
1883
- ereport(LOG,
1884
- (errmsg_internal("could not duplicate handle to be written to backend parameter file: error code %lu",
1885
- GetLastError())));
1886
- return false;
1887
- }
1888
-
1889
- *dest = hChild;
1890
- return true;
1891
- }
1892
-
1893
- /*
1894
- * Duplicate a socket for usage in a child process, and write the resulting
1895
- * structure to the parameter file.
1896
- * This is required because a number of LSPs (Layered Service Providers) very
1897
- * common on Windows (antivirus, firewalls, download managers etc) break
1898
- * straight socket inheritance.
1899
- */
1900
- static bool
1901
- write_inheritable_socket(InheritableSocket *dest, SOCKET src, pid_t childpid)
1902
- {
1903
- dest->origsocket = src;
1904
- if (src != 0 && src != PGINVALID_SOCKET)
1905
- {
1906
- /* Actual socket */
1907
- if (WSADuplicateSocket(src, childpid, &dest->wsainfo) != 0)
1908
- {
1909
- ereport(LOG,
1910
- (errmsg("could not duplicate socket %d for use in backend: error code %d",
1911
- (int) src, WSAGetLastError())));
1912
- return false;
1913
- }
1914
- }
1915
- return true;
1916
- }
1917
-
1918
- /*
1919
- * Read a duplicate socket structure back, and get the socket descriptor.
1920
- */
1921
- static void
1922
- read_inheritable_socket(SOCKET *dest, InheritableSocket *src)
1923
- {
1924
- SOCKET s;
1925
-
1926
- if (src->origsocket == PGINVALID_SOCKET || src->origsocket == 0)
1927
- {
1928
- /* Not a real socket! */
1929
- *dest = src->origsocket;
1930
- }
1931
- else
1932
- {
1933
- /* Actual socket, so create from structure */
1934
- s = WSASocket(FROM_PROTOCOL_INFO,
1935
- FROM_PROTOCOL_INFO,
1936
- FROM_PROTOCOL_INFO,
1937
- &src->wsainfo,
1938
- 0,
1939
- 0);
1940
- if (s == INVALID_SOCKET)
1941
- {
1942
- write_stderr("could not create inherited socket: error code %d\n",
1943
- WSAGetLastError());
1944
- exit(1);
1945
- }
1946
- *dest = s;
1947
-
1948
- /*
1949
- * To make sure we don't get two references to the same socket, close
1950
- * the original one. (This would happen when inheritance actually
1951
- * works..
1952
- */
1953
- closesocket(src->origsocket);
1954
- }
1955
- }
1956
- #endif
1957
-
1958
- static void
1959
- read_backend_variables(char *id, Port *port)
1960
- {
1961
- BackendParameters param;
1962
-
1963
- #ifndef WIN32
1964
- /* Non-win32 implementation reads from file */
1965
- FILE *fp;
1966
-
1967
- /* Open file */
1968
- fp = AllocateFile(id, PG_BINARY_R);
1969
- if (!fp)
1970
- {
1971
- write_stderr("could not open backend variables file \"%s\": %s\n",
1972
- id, strerror(errno));
1973
- exit(1);
1974
- }
1975
-
1976
- if (fread(&param, sizeof(param), 1, fp) != 1)
1977
- {
1978
- write_stderr("could not read from backend variables file \"%s\": %s\n",
1979
- id, strerror(errno));
1980
- exit(1);
1981
- }
1982
-
1983
- /* Release file */
1984
- FreeFile(fp);
1985
- if (unlink(id) != 0)
1986
- {
1987
- write_stderr("could not remove file \"%s\": %s\n",
1988
- id, strerror(errno));
1989
- exit(1);
1990
- }
1991
- #else
1992
- /* Win32 version uses mapped file */
1993
- HANDLE paramHandle;
1994
- BackendParameters *paramp;
1995
-
1996
- #ifdef _WIN64
1997
- paramHandle = (HANDLE) _atoi64(id);
1998
- #else
1999
- paramHandle = (HANDLE) atol(id);
2000
- #endif
2001
- paramp = MapViewOfFile(paramHandle, FILE_MAP_READ, 0, 0, 0);
2002
- if (!paramp)
2003
- {
2004
- write_stderr("could not map view of backend variables: error code %lu\n",
2005
- GetLastError());
2006
- exit(1);
2007
- }
2008
-
2009
- memcpy(&param, paramp, sizeof(BackendParameters));
2010
-
2011
- if (!UnmapViewOfFile(paramp))
2012
- {
2013
- write_stderr("could not unmap view of backend variables: error code %lu\n",
2014
- GetLastError());
2015
- exit(1);
2016
- }
2017
-
2018
- if (!CloseHandle(paramHandle))
2019
- {
2020
- write_stderr("could not close handle to backend parameter variables: error code %lu\n",
2021
- GetLastError());
2022
- exit(1);
2023
- }
2024
- #endif
2025
-
2026
- restore_backend_variables(&param, port);
2027
- }
2028
-
2029
- /* Restore critical backend variables from the BackendParameters struct */
2030
- static void
2031
- restore_backend_variables(BackendParameters *param, Port *port)
2032
- {
2033
- memcpy(port, &param->port, sizeof(Port));
2034
- read_inheritable_socket(&port->sock, &param->portsocket);
2035
-
2036
- SetDataDir(param->DataDir);
2037
-
2038
- memcpy(&ListenSocket, &param->ListenSocket, sizeof(ListenSocket));
2039
-
2040
- MyCancelKey = param->MyCancelKey;
2041
- MyPMChildSlot = param->MyPMChildSlot;
2042
-
2043
- #ifdef WIN32
2044
- ShmemProtectiveRegion = param->ShmemProtectiveRegion;
2045
- #endif
2046
- UsedShmemSegID = param->UsedShmemSegID;
2047
- UsedShmemSegAddr = param->UsedShmemSegAddr;
2048
-
2049
- ShmemLock = param->ShmemLock;
2050
- ShmemVariableCache = param->ShmemVariableCache;
2051
- ShmemBackendArray = param->ShmemBackendArray;
2052
-
2053
- #ifndef HAVE_SPINLOCKS
2054
- SpinlockSemaArray = param->SpinlockSemaArray;
2055
- #endif
2056
- NamedLWLockTrancheRequests = param->NamedLWLockTrancheRequests;
2057
- NamedLWLockTrancheArray = param->NamedLWLockTrancheArray;
2058
- MainLWLockArray = param->MainLWLockArray;
2059
- ProcStructLock = param->ProcStructLock;
2060
- ProcGlobal = param->ProcGlobal;
2061
- AuxiliaryProcs = param->AuxiliaryProcs;
2062
- PreparedXactProcs = param->PreparedXactProcs;
2063
- PMSignalState = param->PMSignalState;
2064
- read_inheritable_socket(&pgStatSock, &param->pgStatSock);
2065
-
2066
- PostmasterPid = param->PostmasterPid;
2067
- PgStartTime = param->PgStartTime;
2068
- PgReloadTime = param->PgReloadTime;
2069
- first_syslogger_file_time = param->first_syslogger_file_time;
2070
-
2071
- redirection_done = param->redirection_done;
2072
- IsBinaryUpgrade = param->IsBinaryUpgrade;
2073
- max_safe_fds = param->max_safe_fds;
2074
-
2075
- MaxBackends = param->MaxBackends;
2076
-
2077
- #ifdef WIN32
2078
- PostmasterHandle = param->PostmasterHandle;
2079
- pgwin32_initial_signal_pipe = param->initial_signal_pipe;
2080
- #else
2081
- memcpy(&postmaster_alive_fds, &param->postmaster_alive_fds,
2082
- sizeof(postmaster_alive_fds));
2083
- #endif
2084
-
2085
- memcpy(&syslogPipe, &param->syslogPipe, sizeof(syslogPipe));
2086
-
2087
- strlcpy(my_exec_path, param->my_exec_path, MAXPGPATH);
2088
-
2089
- strlcpy(pkglib_path, param->pkglib_path, MAXPGPATH);
2090
-
2091
- strlcpy(ExtraOptions, param->ExtraOptions, MAXPGPATH);
2092
-
2093
- /*
2094
- * We need to restore fd.c's counts of externally-opened FDs; to avoid
2095
- * confusion, be sure to do this after restoring max_safe_fds. (Note:
2096
- * BackendInitialize will handle this for port->sock.)
2097
- */
2098
- #ifndef WIN32
2099
- if (postmaster_alive_fds[0] >= 0)
2100
- ReserveExternalFD();
2101
- if (postmaster_alive_fds[1] >= 0)
2102
- ReserveExternalFD();
2103
- #endif
2104
- if (pgStatSock != PGINVALID_SOCKET)
2105
- ReserveExternalFD();
2106
- }
2107
-
2108
-
2109
- Size
2110
- ShmemBackendArraySize(void)
2111
- {
2112
- return mul_size(MaxLivePostmasterChildren(), sizeof(Backend));
2113
- }
2114
-
2115
- void
2116
- ShmemBackendArrayAllocation(void)
2117
- {
2118
- Size size = ShmemBackendArraySize();
2119
-
2120
- ShmemBackendArray = (Backend *) ShmemAlloc(size);
2121
- /* Mark all slots as empty */
2122
- memset(ShmemBackendArray, 0, size);
2123
- }
2124
-
2125
- static void
2126
- ShmemBackendArrayAdd(Backend *bn)
2127
- {
2128
- /* The array slot corresponding to my PMChildSlot should be free */
2129
- int i = bn->child_slot - 1;
2130
-
2131
- Assert(ShmemBackendArray[i].pid == 0);
2132
- ShmemBackendArray[i] = *bn;
2133
- }
2134
-
2135
- static void
2136
- ShmemBackendArrayRemove(Backend *bn)
2137
- {
2138
- int i = bn->child_slot - 1;
2139
-
2140
- Assert(ShmemBackendArray[i].pid == bn->pid);
2141
- /* Mark the slot as empty */
2142
- ShmemBackendArray[i].pid = 0;
2143
- }
2144
- #endif /* EXEC_BACKEND */
2145
-
2146
-
2147
- #ifdef WIN32
2148
-
2149
- /*
2150
- * Subset implementation of waitpid() for Windows. We assume pid is -1
2151
- * (that is, check all child processes) and options is WNOHANG (don't wait).
2152
- */
2153
- static pid_t
2154
- waitpid(pid_t pid, int *exitstatus, int options)
2155
- {
2156
- DWORD dwd;
2157
- ULONG_PTR key;
2158
- OVERLAPPED *ovl;
2159
-
2160
- /*
2161
- * Check if there are any dead children. If there are, return the pid of
2162
- * the first one that died.
2163
- */
2164
- if (GetQueuedCompletionStatus(win32ChildQueue, &dwd, &key, &ovl, 0))
2165
- {
2166
- *exitstatus = (int) key;
2167
- return dwd;
2168
- }
2169
-
2170
- return -1;
2171
- }
2172
-
2173
- /*
2174
- * Note! Code below executes on a thread pool! All operations must
2175
- * be thread safe! Note that elog() and friends must *not* be used.
2176
- */
2177
- static void WINAPI
2178
- pgwin32_deadchild_callback(PVOID lpParameter, BOOLEAN TimerOrWaitFired)
2179
- {
2180
- win32_deadchild_waitinfo *childinfo = (win32_deadchild_waitinfo *) lpParameter;
2181
- DWORD exitcode;
2182
-
2183
- if (TimerOrWaitFired)
2184
- return; /* timeout. Should never happen, since we use
2185
- * INFINITE as timeout value. */
2186
-
2187
- /*
2188
- * Remove handle from wait - required even though it's set to wait only
2189
- * once
2190
- */
2191
- UnregisterWaitEx(childinfo->waitHandle, NULL);
2192
-
2193
- if (!GetExitCodeProcess(childinfo->procHandle, &exitcode))
2194
- {
2195
- /*
2196
- * Should never happen. Inform user and set a fixed exitcode.
2197
- */
2198
- write_stderr("could not read exit code for process\n");
2199
- exitcode = 255;
2200
- }
2201
-
2202
- if (!PostQueuedCompletionStatus(win32ChildQueue, childinfo->procId, (ULONG_PTR) exitcode, NULL))
2203
- write_stderr("could not post child completion status\n");
2204
-
2205
- /*
2206
- * Handle is per-process, so we close it here instead of in the
2207
- * originating thread
2208
- */
2209
- CloseHandle(childinfo->procHandle);
2210
-
2211
- /*
2212
- * Free struct that was allocated before the call to
2213
- * RegisterWaitForSingleObject()
2214
- */
2215
- free(childinfo);
2216
-
2217
- /* Queue SIGCHLD signal */
2218
- pg_queue_signal(SIGCHLD);
2219
- }
2220
- #endif /* WIN32 */
2221
-
2222
- /*
2223
- * Initialize one and only handle for monitoring postmaster death.
2224
- *
2225
- * Called once in the postmaster, so that child processes can subsequently
2226
- * monitor if their parent is dead.
2227
- */
2228
- #ifndef WIN32
2229
- #else
2230
- #endif /* WIN32 */