You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tupdesc.c 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  1. /*-------------------------------------------------------------------------
  2. *
  3. * tupdesc.c
  4. * POSTGRES tuple descriptor support code
  5. *
  6. * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. *
  10. * IDENTIFICATION
  11. * src/backend/access/common/tupdesc.c
  12. *
  13. * NOTES
  14. * some of the executor utility code such as "ExecTypeFromTL" should be
  15. * moved here.
  16. *
  17. *-------------------------------------------------------------------------
  18. */
  19. #include "postgres.h"
  20. #include "access/hash.h"
  21. #include "access/htup_details.h"
  22. #include "access/tupdesc_details.h"
  23. #include "catalog/pg_collation.h"
  24. #include "catalog/pg_type.h"
  25. #include "miscadmin.h"
  26. #include "parser/parse_type.h"
  27. #include "utils/acl.h"
  28. #include "utils/builtins.h"
  29. #include "utils/datum.h"
  30. #include "utils/hashutils.h"
  31. #include "utils/resowner_private.h"
  32. #include "utils/syscache.h"
  33. /*
  34. * CreateTemplateTupleDesc
  35. * This function allocates an empty tuple descriptor structure.
  36. *
  37. * Tuple type ID information is initially set for an anonymous record type;
  38. * caller can overwrite this if needed.
  39. */
  40. TupleDesc
  41. CreateTemplateTupleDesc(int natts)
  42. {
  43. TupleDesc desc;
  44. /*
  45. * sanity checks
  46. */
  47. AssertArg(natts >= 0);
  48. /*
  49. * Allocate enough memory for the tuple descriptor, including the
  50. * attribute rows.
  51. *
  52. * Note: the attribute array stride is sizeof(FormData_pg_attribute),
  53. * since we declare the array elements as FormData_pg_attribute for
  54. * notational convenience. However, we only guarantee that the first
  55. * ATTRIBUTE_FIXED_PART_SIZE bytes of each entry are valid; most code that
  56. * copies tupdesc entries around copies just that much. In principle that
  57. * could be less due to trailing padding, although with the current
  58. * definition of pg_attribute there probably isn't any padding.
  59. */
  60. desc = (TupleDesc) palloc(offsetof(struct tupleDesc, attrs) +
  61. natts * sizeof(FormData_pg_attribute));
  62. /*
  63. * Initialize other fields of the tupdesc.
  64. */
  65. desc->natts = natts;
  66. desc->constr = NULL;
  67. desc->tdtypeid = RECORDOID;
  68. desc->tdtypmod = -1;
  69. desc->tdrefcount = -1; /* assume not reference-counted */
  70. return desc;
  71. }
  72. /*
  73. * CreateTupleDesc
  74. * This function allocates a new TupleDesc by copying a given
  75. * Form_pg_attribute array.
  76. *
  77. * Tuple type ID information is initially set for an anonymous record type;
  78. * caller can overwrite this if needed.
  79. */
  80. TupleDesc
  81. CreateTupleDesc(int natts, Form_pg_attribute *attrs)
  82. {
  83. TupleDesc desc;
  84. int i;
  85. desc = CreateTemplateTupleDesc(natts);
  86. for (i = 0; i < natts; ++i)
  87. memcpy(TupleDescAttr(desc, i), attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
  88. return desc;
  89. }
  90. /*
  91. * CreateTupleDescCopy
  92. * This function creates a new TupleDesc by copying from an existing
  93. * TupleDesc.
  94. *
  95. * !!! Constraints and defaults are not copied !!!
  96. */
  97. TupleDesc
  98. CreateTupleDescCopy(TupleDesc tupdesc)
  99. {
  100. TupleDesc desc;
  101. int i;
  102. desc = CreateTemplateTupleDesc(tupdesc->natts);
  103. /* Flat-copy the attribute array */
  104. memcpy(TupleDescAttr(desc, 0),
  105. TupleDescAttr(tupdesc, 0),
  106. desc->natts * sizeof(FormData_pg_attribute));
  107. /*
  108. * Since we're not copying constraints and defaults, clear fields
  109. * associated with them.
  110. */
  111. for (i = 0; i < desc->natts; i++)
  112. {
  113. Form_pg_attribute att = TupleDescAttr(desc, i);
  114. att->attnotnull = false;
  115. att->atthasdef = false;
  116. att->atthasmissing = false;
  117. att->attidentity = '\0';
  118. }
  119. /* We can copy the tuple type identification, too */
  120. desc->tdtypeid = tupdesc->tdtypeid;
  121. desc->tdtypmod = tupdesc->tdtypmod;
  122. return desc;
  123. }
  124. /*
  125. * CreateTupleDescCopyConstr
  126. * This function creates a new TupleDesc by copying from an existing
  127. * TupleDesc (including its constraints and defaults).
  128. */
  129. TupleDesc
  130. CreateTupleDescCopyConstr(TupleDesc tupdesc)
  131. {
  132. TupleDesc desc;
  133. TupleConstr *constr = tupdesc->constr;
  134. int i;
  135. desc = CreateTemplateTupleDesc(tupdesc->natts);
  136. /* Flat-copy the attribute array */
  137. memcpy(TupleDescAttr(desc, 0),
  138. TupleDescAttr(tupdesc, 0),
  139. desc->natts * sizeof(FormData_pg_attribute));
  140. /* Copy the TupleConstr data structure, if any */
  141. if (constr)
  142. {
  143. TupleConstr *cpy = (TupleConstr *) palloc0(sizeof(TupleConstr));
  144. cpy->has_not_null = constr->has_not_null;
  145. if ((cpy->num_defval = constr->num_defval) > 0)
  146. {
  147. cpy->defval = (AttrDefault *) palloc(cpy->num_defval * sizeof(AttrDefault));
  148. memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
  149. for (i = cpy->num_defval - 1; i >= 0; i--)
  150. {
  151. if (constr->defval[i].adbin)
  152. cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
  153. }
  154. }
  155. if (constr->missing)
  156. {
  157. cpy->missing = (AttrMissing *) palloc(tupdesc->natts * sizeof(AttrMissing));
  158. memcpy(cpy->missing, constr->missing, tupdesc->natts * sizeof(AttrMissing));
  159. for (i = tupdesc->natts - 1; i >= 0; i--)
  160. {
  161. if (constr->missing[i].am_present)
  162. {
  163. Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
  164. cpy->missing[i].am_value = datumCopy(constr->missing[i].am_value,
  165. attr->attbyval,
  166. attr->attlen);
  167. }
  168. }
  169. }
  170. if ((cpy->num_check = constr->num_check) > 0)
  171. {
  172. cpy->check = (ConstrCheck *) palloc(cpy->num_check * sizeof(ConstrCheck));
  173. memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
  174. for (i = cpy->num_check - 1; i >= 0; i--)
  175. {
  176. if (constr->check[i].ccname)
  177. cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
  178. if (constr->check[i].ccbin)
  179. cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
  180. cpy->check[i].ccvalid = constr->check[i].ccvalid;
  181. cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
  182. }
  183. }
  184. desc->constr = cpy;
  185. }
  186. /* We can copy the tuple type identification, too */
  187. desc->tdtypeid = tupdesc->tdtypeid;
  188. desc->tdtypmod = tupdesc->tdtypmod;
  189. return desc;
  190. }
  191. /*
  192. * TupleDescCopy
  193. * Copy a tuple descriptor into caller-supplied memory.
  194. * The memory may be shared memory mapped at any address, and must
  195. * be sufficient to hold TupleDescSize(src) bytes.
  196. *
  197. * !!! Constraints and defaults are not copied !!!
  198. */
  199. void
  200. TupleDescCopy(TupleDesc dst, TupleDesc src)
  201. {
  202. int i;
  203. /* Flat-copy the header and attribute array */
  204. memcpy(dst, src, TupleDescSize(src));
  205. /*
  206. * Since we're not copying constraints and defaults, clear fields
  207. * associated with them.
  208. */
  209. for (i = 0; i < dst->natts; i++)
  210. {
  211. Form_pg_attribute att = TupleDescAttr(dst, i);
  212. att->attnotnull = false;
  213. att->atthasdef = false;
  214. att->atthasmissing = false;
  215. att->attidentity = '\0';
  216. }
  217. dst->constr = NULL;
  218. /*
  219. * Also, assume the destination is not to be ref-counted. (Copying the
  220. * source's refcount would be wrong in any case.)
  221. */
  222. dst->tdrefcount = -1;
  223. }
  224. /*
  225. * TupleDescCopyEntry
  226. * This function copies a single attribute structure from one tuple
  227. * descriptor to another.
  228. *
  229. * !!! Constraints and defaults are not copied !!!
  230. */
  231. void
  232. TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
  233. TupleDesc src, AttrNumber srcAttno)
  234. {
  235. Form_pg_attribute dstAtt = TupleDescAttr(dst, dstAttno - 1);
  236. Form_pg_attribute srcAtt = TupleDescAttr(src, srcAttno - 1);
  237. /*
  238. * sanity checks
  239. */
  240. AssertArg(PointerIsValid(src));
  241. AssertArg(PointerIsValid(dst));
  242. AssertArg(srcAttno >= 1);
  243. AssertArg(srcAttno <= src->natts);
  244. AssertArg(dstAttno >= 1);
  245. AssertArg(dstAttno <= dst->natts);
  246. memcpy(dstAtt, srcAtt, ATTRIBUTE_FIXED_PART_SIZE);
  247. /*
  248. * Aside from updating the attno, we'd better reset attcacheoff.
  249. *
  250. * XXX Actually, to be entirely safe we'd need to reset the attcacheoff of
  251. * all following columns in dst as well. Current usage scenarios don't
  252. * require that though, because all following columns will get initialized
  253. * by other uses of this function or TupleDescInitEntry. So we cheat a
  254. * bit to avoid a useless O(N^2) penalty.
  255. */
  256. dstAtt->attnum = dstAttno;
  257. dstAtt->attcacheoff = -1;
  258. /* since we're not copying constraints or defaults, clear these */
  259. dstAtt->attnotnull = false;
  260. dstAtt->atthasdef = false;
  261. dstAtt->atthasmissing = false;
  262. dstAtt->attidentity = '\0';
  263. }
  264. /*
  265. * Free a TupleDesc including all substructure
  266. */
  267. void
  268. FreeTupleDesc(TupleDesc tupdesc)
  269. {
  270. int i;
  271. /*
  272. * Possibly this should assert tdrefcount == 0, to disallow explicit
  273. * freeing of un-refcounted tupdescs?
  274. */
  275. Assert(tupdesc->tdrefcount <= 0);
  276. if (tupdesc->constr)
  277. {
  278. if (tupdesc->constr->num_defval > 0)
  279. {
  280. AttrDefault *attrdef = tupdesc->constr->defval;
  281. for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
  282. {
  283. if (attrdef[i].adbin)
  284. pfree(attrdef[i].adbin);
  285. }
  286. pfree(attrdef);
  287. }
  288. if (tupdesc->constr->missing)
  289. {
  290. AttrMissing *attrmiss = tupdesc->constr->missing;
  291. for (i = tupdesc->natts - 1; i >= 0; i--)
  292. {
  293. if (attrmiss[i].am_present
  294. && !TupleDescAttr(tupdesc, i)->attbyval)
  295. pfree(DatumGetPointer(attrmiss[i].am_value));
  296. }
  297. pfree(attrmiss);
  298. }
  299. if (tupdesc->constr->num_check > 0)
  300. {
  301. ConstrCheck *check = tupdesc->constr->check;
  302. for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
  303. {
  304. if (check[i].ccname)
  305. pfree(check[i].ccname);
  306. if (check[i].ccbin)
  307. pfree(check[i].ccbin);
  308. }
  309. pfree(check);
  310. }
  311. pfree(tupdesc->constr);
  312. }
  313. pfree(tupdesc);
  314. }
  315. /*
  316. * Increment the reference count of a tupdesc, and log the reference in
  317. * CurrentResourceOwner.
  318. *
  319. * Do not apply this to tupdescs that are not being refcounted. (Use the
  320. * macro PinTupleDesc for tupdescs of uncertain status.)
  321. */
  322. void
  323. IncrTupleDescRefCount(TupleDesc tupdesc)
  324. {
  325. Assert(tupdesc->tdrefcount >= 0);
  326. ResourceOwnerEnlargeTupleDescs(CurrentResourceOwner);
  327. tupdesc->tdrefcount++;
  328. ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
  329. }
  330. /*
  331. * Decrement the reference count of a tupdesc, remove the corresponding
  332. * reference from CurrentResourceOwner, and free the tupdesc if no more
  333. * references remain.
  334. *
  335. * Do not apply this to tupdescs that are not being refcounted. (Use the
  336. * macro ReleaseTupleDesc for tupdescs of uncertain status.)
  337. */
  338. void
  339. DecrTupleDescRefCount(TupleDesc tupdesc)
  340. {
  341. Assert(tupdesc->tdrefcount > 0);
  342. ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);
  343. if (--tupdesc->tdrefcount == 0)
  344. FreeTupleDesc(tupdesc);
  345. }
  346. /*
  347. * Compare two TupleDesc structures for logical equality
  348. *
  349. * Note: we deliberately do not check the attrelid and tdtypmod fields.
  350. * This allows typcache.c to use this routine to see if a cached record type
  351. * matches a requested type, and is harmless for relcache.c's uses.
  352. * We don't compare tdrefcount, either.
  353. */
  354. bool
  355. equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
  356. {
  357. int i,
  358. j,
  359. n;
  360. if (tupdesc1->natts != tupdesc2->natts)
  361. return false;
  362. if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
  363. return false;
  364. for (i = 0; i < tupdesc1->natts; i++)
  365. {
  366. Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
  367. Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
  368. /*
  369. * We do not need to check every single field here: we can disregard
  370. * attrelid and attnum (which were used to place the row in the attrs
  371. * array in the first place). It might look like we could dispense
  372. * with checking attlen/attbyval/attalign, since these are derived
  373. * from atttypid; but in the case of dropped columns we must check
  374. * them (since atttypid will be zero for all dropped columns) and in
  375. * general it seems safer to check them always.
  376. *
  377. * attcacheoff must NOT be checked since it's possibly not set in both
  378. * copies.
  379. */
  380. if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
  381. return false;
  382. if (attr1->atttypid != attr2->atttypid)
  383. return false;
  384. if (attr1->attstattarget != attr2->attstattarget)
  385. return false;
  386. if (attr1->attlen != attr2->attlen)
  387. return false;
  388. if (attr1->attndims != attr2->attndims)
  389. return false;
  390. if (attr1->atttypmod != attr2->atttypmod)
  391. return false;
  392. if (attr1->attbyval != attr2->attbyval)
  393. return false;
  394. if (attr1->attstorage != attr2->attstorage)
  395. return false;
  396. if (attr1->attalign != attr2->attalign)
  397. return false;
  398. if (attr1->attnotnull != attr2->attnotnull)
  399. return false;
  400. if (attr1->atthasdef != attr2->atthasdef)
  401. return false;
  402. if (attr1->attidentity != attr2->attidentity)
  403. return false;
  404. if (attr1->attisdropped != attr2->attisdropped)
  405. return false;
  406. if (attr1->attislocal != attr2->attislocal)
  407. return false;
  408. if (attr1->attinhcount != attr2->attinhcount)
  409. return false;
  410. if (attr1->attcollation != attr2->attcollation)
  411. return false;
  412. /* attacl, attoptions and attfdwoptions are not even present... */
  413. }
  414. if (tupdesc1->constr != NULL)
  415. {
  416. TupleConstr *constr1 = tupdesc1->constr;
  417. TupleConstr *constr2 = tupdesc2->constr;
  418. if (constr2 == NULL)
  419. return false;
  420. if (constr1->has_not_null != constr2->has_not_null)
  421. return false;
  422. n = constr1->num_defval;
  423. if (n != (int) constr2->num_defval)
  424. return false;
  425. for (i = 0; i < n; i++)
  426. {
  427. AttrDefault *defval1 = constr1->defval + i;
  428. AttrDefault *defval2 = constr2->defval;
  429. /*
  430. * We can't assume that the items are always read from the system
  431. * catalogs in the same order; so use the adnum field to identify
  432. * the matching item to compare.
  433. */
  434. for (j = 0; j < n; defval2++, j++)
  435. {
  436. if (defval1->adnum == defval2->adnum)
  437. break;
  438. }
  439. if (j >= n)
  440. return false;
  441. if (strcmp(defval1->adbin, defval2->adbin) != 0)
  442. return false;
  443. }
  444. if (constr1->missing)
  445. {
  446. if (!constr2->missing)
  447. return false;
  448. for (i = 0; i < tupdesc1->natts; i++)
  449. {
  450. AttrMissing *missval1 = constr1->missing + i;
  451. AttrMissing *missval2 = constr2->missing + i;
  452. if (missval1->am_present != missval2->am_present)
  453. return false;
  454. if (missval1->am_present)
  455. {
  456. Form_pg_attribute missatt1 = TupleDescAttr(tupdesc1, i);
  457. if (!datumIsEqual(missval1->am_value, missval2->am_value,
  458. missatt1->attbyval, missatt1->attlen))
  459. return false;
  460. }
  461. }
  462. }
  463. else if (constr2->missing)
  464. return false;
  465. n = constr1->num_check;
  466. if (n != (int) constr2->num_check)
  467. return false;
  468. for (i = 0; i < n; i++)
  469. {
  470. ConstrCheck *check1 = constr1->check + i;
  471. ConstrCheck *check2 = constr2->check;
  472. /*
  473. * Similarly, don't assume that the checks are always read in the
  474. * same order; match them up by name and contents. (The name
  475. * *should* be unique, but...)
  476. */
  477. for (j = 0; j < n; check2++, j++)
  478. {
  479. if (strcmp(check1->ccname, check2->ccname) == 0 &&
  480. strcmp(check1->ccbin, check2->ccbin) == 0 &&
  481. check1->ccvalid == check2->ccvalid &&
  482. check1->ccnoinherit == check2->ccnoinherit)
  483. break;
  484. }
  485. if (j >= n)
  486. return false;
  487. }
  488. }
  489. else if (tupdesc2->constr != NULL)
  490. return false;
  491. return true;
  492. }
  493. /*
  494. * hashTupleDesc
  495. * Compute a hash value for a tuple descriptor.
  496. *
  497. * If two tuple descriptors would be considered equal by equalTupleDescs()
  498. * then their hash value will be equal according to this function.
  499. *
  500. * Note that currently contents of constraint are not hashed - it'd be a bit
  501. * painful to do so, and conflicts just due to constraints are unlikely.
  502. */
  503. uint32
  504. hashTupleDesc(TupleDesc desc)
  505. {
  506. uint32 s;
  507. int i;
  508. s = hash_combine(0, hash_uint32(desc->natts));
  509. s = hash_combine(s, hash_uint32(desc->tdtypeid));
  510. for (i = 0; i < desc->natts; ++i)
  511. s = hash_combine(s, hash_uint32(TupleDescAttr(desc, i)->atttypid));
  512. return s;
  513. }
  514. /*
  515. * TupleDescInitEntry
  516. * This function initializes a single attribute structure in
  517. * a previously allocated tuple descriptor.
  518. *
  519. * If attributeName is NULL, the attname field is set to an empty string
  520. * (this is for cases where we don't know or need a name for the field).
  521. * Also, some callers use this function to change the datatype-related fields
  522. * in an existing tupdesc; they pass attributeName = NameStr(att->attname)
  523. * to indicate that the attname field shouldn't be modified.
  524. *
  525. * Note that attcollation is set to the default for the specified datatype.
  526. * If a nondefault collation is needed, insert it afterwards using
  527. * TupleDescInitEntryCollation.
  528. */
  529. void
  530. TupleDescInitEntry(TupleDesc desc,
  531. AttrNumber attributeNumber,
  532. const char *attributeName,
  533. Oid oidtypeid,
  534. int32 typmod,
  535. int attdim)
  536. {
  537. HeapTuple tuple;
  538. Form_pg_type typeForm;
  539. Form_pg_attribute att;
  540. /*
  541. * sanity checks
  542. */
  543. AssertArg(PointerIsValid(desc));
  544. AssertArg(attributeNumber >= 1);
  545. AssertArg(attributeNumber <= desc->natts);
  546. /*
  547. * initialize the attribute fields
  548. */
  549. att = TupleDescAttr(desc, attributeNumber - 1);
  550. att->attrelid = 0; /* dummy value */
  551. /*
  552. * Note: attributeName can be NULL, because the planner doesn't always
  553. * fill in valid resname values in targetlists, particularly for resjunk
  554. * attributes. Also, do nothing if caller wants to re-use the old attname.
  555. */
  556. if (attributeName == NULL)
  557. MemSet(NameStr(att->attname), 0, NAMEDATALEN);
  558. else if (attributeName != NameStr(att->attname))
  559. namestrcpy(&(att->attname), attributeName);
  560. att->attstattarget = -1;
  561. att->attcacheoff = -1;
  562. att->atttypmod = typmod;
  563. att->attnum = attributeNumber;
  564. att->attndims = attdim;
  565. att->attnotnull = false;
  566. att->atthasdef = false;
  567. att->atthasmissing = false;
  568. att->attidentity = '\0';
  569. att->attisdropped = false;
  570. att->attislocal = true;
  571. att->attinhcount = 0;
  572. /* attacl, attoptions and attfdwoptions are not present in tupledescs */
  573. tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(oidtypeid));
  574. if (!HeapTupleIsValid(tuple))
  575. elog(ERROR, "cache lookup failed for type %u", oidtypeid);
  576. typeForm = (Form_pg_type) GETSTRUCT(tuple);
  577. att->atttypid = oidtypeid;
  578. att->attlen = typeForm->typlen;
  579. att->attbyval = typeForm->typbyval;
  580. att->attalign = typeForm->typalign;
  581. att->attstorage = typeForm->typstorage;
  582. att->attcollation = typeForm->typcollation;
  583. ReleaseSysCache(tuple);
  584. }
  585. /*
  586. * TupleDescInitBuiltinEntry
  587. * Initialize a tuple descriptor without catalog access. Only
  588. * a limited range of builtin types are supported.
  589. */
  590. void
  591. TupleDescInitBuiltinEntry(TupleDesc desc,
  592. AttrNumber attributeNumber,
  593. const char *attributeName,
  594. Oid oidtypeid,
  595. int32 typmod,
  596. int attdim)
  597. {
  598. Form_pg_attribute att;
  599. /* sanity checks */
  600. AssertArg(PointerIsValid(desc));
  601. AssertArg(attributeNumber >= 1);
  602. AssertArg(attributeNumber <= desc->natts);
  603. /* initialize the attribute fields */
  604. att = TupleDescAttr(desc, attributeNumber - 1);
  605. att->attrelid = 0; /* dummy value */
  606. /* unlike TupleDescInitEntry, we require an attribute name */
  607. Assert(attributeName != NULL);
  608. namestrcpy(&(att->attname), attributeName);
  609. att->attstattarget = -1;
  610. att->attcacheoff = -1;
  611. att->atttypmod = typmod;
  612. att->attnum = attributeNumber;
  613. att->attndims = attdim;
  614. att->attnotnull = false;
  615. att->atthasdef = false;
  616. att->atthasmissing = false;
  617. att->attidentity = '\0';
  618. att->attisdropped = false;
  619. att->attislocal = true;
  620. att->attinhcount = 0;
  621. /* attacl, attoptions and attfdwoptions are not present in tupledescs */
  622. att->atttypid = oidtypeid;
  623. /*
  624. * Our goal here is to support just enough types to let basic builtin
  625. * commands work without catalog access - e.g. so that we can do certain
  626. * things even in processes that are not connected to a database.
  627. */
  628. switch (oidtypeid)
  629. {
  630. case TEXTOID:
  631. case TEXTARRAYOID:
  632. att->attlen = -1;
  633. att->attbyval = false;
  634. att->attalign = 'i';
  635. att->attstorage = 'x';
  636. att->attcollation = DEFAULT_COLLATION_OID;
  637. break;
  638. case BOOLOID:
  639. att->attlen = 1;
  640. att->attbyval = true;
  641. att->attalign = 'c';
  642. att->attstorage = 'p';
  643. att->attcollation = InvalidOid;
  644. break;
  645. case INT4OID:
  646. att->attlen = 4;
  647. att->attbyval = true;
  648. att->attalign = 'i';
  649. att->attstorage = 'p';
  650. att->attcollation = InvalidOid;
  651. break;
  652. case INT8OID:
  653. att->attlen = 8;
  654. att->attbyval = FLOAT8PASSBYVAL;
  655. att->attalign = 'd';
  656. att->attstorage = 'p';
  657. att->attcollation = InvalidOid;
  658. break;
  659. default:
  660. elog(ERROR, "unsupported type %u", oidtypeid);
  661. }
  662. }
  663. /*
  664. * TupleDescInitEntryCollation
  665. *
  666. * Assign a nondefault collation to a previously initialized tuple descriptor
  667. * entry.
  668. */
  669. void
  670. TupleDescInitEntryCollation(TupleDesc desc,
  671. AttrNumber attributeNumber,
  672. Oid collationid)
  673. {
  674. /*
  675. * sanity checks
  676. */
  677. AssertArg(PointerIsValid(desc));
  678. AssertArg(attributeNumber >= 1);
  679. AssertArg(attributeNumber <= desc->natts);
  680. TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
  681. }
  682. /*
  683. * BuildDescForRelation
  684. *
  685. * Given a relation schema (list of ColumnDef nodes), build a TupleDesc.
  686. *
  687. * Note: the default assumption is no OIDs; caller may modify the returned
  688. * TupleDesc if it wants OIDs. Also, tdtypeid will need to be filled in
  689. * later on.
  690. */
  691. TupleDesc
  692. BuildDescForRelation(List *schema)
  693. {
  694. int natts;
  695. AttrNumber attnum;
  696. ListCell *l;
  697. TupleDesc desc;
  698. bool has_not_null;
  699. char *attname;
  700. Oid atttypid;
  701. int32 atttypmod;
  702. Oid attcollation;
  703. int attdim;
  704. /*
  705. * allocate a new tuple descriptor
  706. */
  707. natts = list_length(schema);
  708. desc = CreateTemplateTupleDesc(natts);
  709. has_not_null = false;
  710. attnum = 0;
  711. foreach(l, schema)
  712. {
  713. ColumnDef *entry = lfirst(l);
  714. AclResult aclresult;
  715. Form_pg_attribute att;
  716. /*
  717. * for each entry in the list, get the name and type information from
  718. * the list and have TupleDescInitEntry fill in the attribute
  719. * information we need.
  720. */
  721. attnum++;
  722. attname = entry->colname;
  723. typenameTypeIdAndMod(NULL, entry->typeName, &atttypid, &atttypmod);
  724. aclresult = pg_type_aclcheck(atttypid, GetUserId(), ACL_USAGE);
  725. if (aclresult != ACLCHECK_OK)
  726. aclcheck_error_type(aclresult, atttypid);
  727. attcollation = GetColumnDefCollation(NULL, entry, atttypid);
  728. attdim = list_length(entry->typeName->arrayBounds);
  729. if (entry->typeName->setof)
  730. ereport(ERROR,
  731. (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
  732. errmsg("column \"%s\" cannot be declared SETOF",
  733. attname)));
  734. TupleDescInitEntry(desc, attnum, attname,
  735. atttypid, atttypmod, attdim);
  736. att = TupleDescAttr(desc, attnum - 1);
  737. /* Override TupleDescInitEntry's settings as requested */
  738. TupleDescInitEntryCollation(desc, attnum, attcollation);
  739. if (entry->storage)
  740. att->attstorage = entry->storage;
  741. /* Fill in additional stuff not handled by TupleDescInitEntry */
  742. att->attnotnull = entry->is_not_null;
  743. has_not_null |= entry->is_not_null;
  744. att->attislocal = entry->is_local;
  745. att->attinhcount = entry->inhcount;
  746. }
  747. if (has_not_null)
  748. {
  749. TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
  750. constr->has_not_null = true;
  751. constr->defval = NULL;
  752. constr->missing = NULL;
  753. constr->num_defval = 0;
  754. constr->check = NULL;
  755. constr->num_check = 0;
  756. desc->constr = constr;
  757. }
  758. else
  759. {
  760. desc->constr = NULL;
  761. }
  762. return desc;
  763. }
  764. /*
  765. * BuildDescFromLists
  766. *
  767. * Build a TupleDesc given lists of column names (as String nodes),
  768. * column type OIDs, typmods, and collation OIDs.
  769. *
  770. * No constraints are generated.
  771. *
  772. * This is essentially a cut-down version of BuildDescForRelation for use
  773. * with functions returning RECORD.
  774. */
  775. TupleDesc
  776. BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
  777. {
  778. int natts;
  779. AttrNumber attnum;
  780. ListCell *l1;
  781. ListCell *l2;
  782. ListCell *l3;
  783. ListCell *l4;
  784. TupleDesc desc;
  785. natts = list_length(names);
  786. Assert(natts == list_length(types));
  787. Assert(natts == list_length(typmods));
  788. Assert(natts == list_length(collations));
  789. /*
  790. * allocate a new tuple descriptor
  791. */
  792. desc = CreateTemplateTupleDesc(natts);
  793. attnum = 0;
  794. l2 = list_head(types);
  795. l3 = list_head(typmods);
  796. l4 = list_head(collations);
  797. foreach(l1, names)
  798. {
  799. char *attname = strVal(lfirst(l1));
  800. Oid atttypid;
  801. int32 atttypmod;
  802. Oid attcollation;
  803. atttypid = lfirst_oid(l2);
  804. l2 = lnext(l2);
  805. atttypmod = lfirst_int(l3);
  806. l3 = lnext(l3);
  807. attcollation = lfirst_oid(l4);
  808. l4 = lnext(l4);
  809. attnum++;
  810. TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
  811. TupleDescInitEntryCollation(desc, attnum, attcollation);
  812. }
  813. return desc;
  814. }