Re: A stack allocation API

From: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
To: Thomas Munro <thomas(dot)munro(at)gmail(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Re: A stack allocation API
Date: 2026-03-18 19:54:00
Message-ID: CAN4CZFOe-z0uYgZm1JGPRPOR6DzxAr82O+Gg97_RvdGTDXU3FQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello!

+ !defined(PG_STACK_USE_ALLOC) &&

There's a typo there, that should be ALLOCA?

+/* Just use palloc. */
+#ifdef PG_STACK_USE_PALLOC
+#define DECLARE_PG_STACK_IMPL(size)
+#define pg_stack_alloc_aligned_impl(size, align) \
+ pg_stack_palloc_aligned((size), (align))
+#define pg_stack_ptr_p() false
+#endif

Isn't a ptr parameter missing from this branch?

+#define pg_stack_strdup_with_len(data, size) \
+ (pg_stack_sanity_checks(1), \
+ pg_stack_let_size = (size), \
+ pg_stack_strdup_with_len_impl( \
+ pg_stack_alloc_aligned_impl(pg_stack_let_size + 1, \
+ alignof(char)),

Both seems like a not realistic edge case, but since the array macros
above have a length check, shouldn't this also guard against +1
overflowing?

+/* Populate and insert a new entry. */
+static inline void *
+pg_stack_debug_link(pg_stack_debug_node **head,
+ pg_stack_debug_node *node,
+ void *ptr,
+ size_t size)
+{
+ node->ptr = ptr;
+ node->size = size;

Again unrealistic edge case, but if the above macros have overflow
checks for typically 64 bit size_t, shouldn't this also have some
safety checks, or at least setting node->size consistently to uint32_t
max if size is bigger?

+#define pg_stack_strdup(cstr) \
+ pg_stack_strdup_with_len((cstr), strlen(cstr))
+#define pg_stack_strndup(cstr, n) \
+ pg_stack_strdup_with_len((cstr), strnlen((cstr), (n)))

+#define pg_stack_text_to_cstring(text) \
+ pg_stack_strdup_with_len(VARDATA_ANY(text), VARSIZE_ANY_EXHDR(text))
+#define pg_stack_text_datum_to_cstring(datum) \
+ pg_stack_text_to_cstring((text *) DatumGetPointer(datum))

I would at least very clearly document that these macros evaluate
their arguments twice. I can't think of any scenario that would
seriously break things with them (or at least not break the stack),
but it could be still easy to forget that these are macros and not
functions.

Or can't they work similarly to DECLARE_PG_STACK_SIZE and store the
pointer in a local variable?

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Antonin Houska 2026-03-18 20:07:09 Re: Adding REPACK [concurrently]
Previous Message Tom Lane 2026-03-18 19:52:02 Re: [PROPOSAL] Termination of Background Workers for ALTER/DROP DATABASE