Skip to content

Commit f7a3051

Browse files
Migrate PLtsql node serialization to extension side
Move PLtsql outfuncs/readfuncs code generation entirely to the extension, eliminating the need for PLtsql-specific headers in the engine's gen_node_support.pl input files. Key changes: - gen_pltsql_node_support.pl now generates pltsql_nodetags.h with extension-owned T_PLtsql_* NodeTag values (offset from 1000 to avoid collision with engine's NodeTag enum). Includes ABI stability check that fails the build if node types are added without updating $last_nodetag/$last_nodetag_no. - Wrapper files pltsql_outfuncs.c and pltsql_readfuncs.c mirror the engine's pattern: #include the generated static functions and switch fragments, expose public pltsql_outNode() and pltsql_parseNodeString() dispatch functions. - pltsql_serialize_macros.h provides WRITE_*/READ_* macros replicated from engine internals (not exposed in any PG header). - pl_handler.c registers outNode_hook and parseNodeString_hook in _PG_init() so the engine's outNode()/parseNodeString() delegate to extension code for PLtsql node types. - pltsql.h includes generated pltsql_nodetags.h for T_PLtsql_* defines. - Makefile updated: compiles wrapper .o files (not gen .o directly), with proper dependency rules for generated files. Files changed: src/pltsql_serialize/gen_pltsql_node_support.pl - nodetags generation + ABI check src/pltsql_serialize/pltsql_outfuncs.c - new wrapper src/pltsql_serialize/pltsql_readfuncs.c - new wrapper src/pltsql_serialize/pltsql_serialize_macros.h - shared macros src/pltsql_serialize/pltsql_node_stubs.c - custom read/write nodes src/pltsql.h - include pltsql_nodetags.h src/pl_handler.c - register hooks Makefile - build rules Task: BABEL-6037 Signed-off-by: Manisha Deshpande <mmdeshp@amazon.com>
1 parent 1a30c79 commit f7a3051

File tree

11 files changed

+3612
-2
lines changed

11 files changed

+3612
-2
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,9 @@ contrib/babelfishpg_tsql/src/pl_gram.c
4545
contrib/babelfishpg_tsql/src/pl_gram.h
4646
contrib/babelfishpg_tsql/src/pl_gram.output
4747

48+
# PLtsql serialization - auto-generated by gen_pltsql_node_support.pl
49+
contrib/babelfishpg_tsql/src/pltsql_serialize/pltsql_nodetags.h
50+
contrib/babelfishpg_tsql/src/pltsql_serialize/pltsql_outfuncs_gen.c
51+
contrib/babelfishpg_tsql/src/pltsql_serialize/pltsql_outfuncs_switch.c
52+
contrib/babelfishpg_tsql/src/pltsql_serialize/pltsql_readfuncs_gen.c
53+
contrib/babelfishpg_tsql/src/pltsql_serialize/pltsql_readfuncs_switch.c

contrib/babelfishpg_tsql/Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,29 @@ export ANTLR4_RUNTIME_INCLUDE_DIR=/usr/local/include/antlr4-runtime
8686
export ANTLR4_RUNTIME_LIB_DIR=/usr/local/lib
8787

8888
OBJS += src/pltsql_bulkcopy.o
89+
OBJS += src/pltsql_serialize/pltsql_node_stubs.o
90+
91+
# PLtsql serialization code generation (babelfishpg_tsql.enable_routine_parse_cache)
92+
PLTSQL_SER_DIR = src/pltsql_serialize
93+
PLTSQL_SER_HEADERS = $(PLTSQL_SER_DIR)/pltsql_serializable_1.h $(PLTSQL_SER_DIR)/pltsql_serializable_2.h
94+
PLTSQL_GEN_SCRIPT = $(PLTSQL_SER_DIR)/gen_pltsql_node_support.pl
95+
96+
$(PLTSQL_SER_DIR)/pltsql_nodetags.h $(PLTSQL_SER_DIR)/pltsql_outfuncs_gen.c $(PLTSQL_SER_DIR)/pltsql_readfuncs_gen.c $(PLTSQL_SER_DIR)/pltsql_outfuncs_switch.c $(PLTSQL_SER_DIR)/pltsql_readfuncs_switch.c: $(PLTSQL_SER_HEADERS) $(PLTSQL_GEN_SCRIPT)
97+
$(PERL) $(PLTSQL_GEN_SCRIPT) --outdir $(PLTSQL_SER_DIR) $(PLTSQL_SER_HEADERS)
98+
99+
# Wrapper .c files #include the generated .c files (mirroring engine pattern)
100+
# so we compile the wrappers, not the gen files directly.
101+
OBJS += $(PLTSQL_SER_DIR)/pltsql_outfuncs.o
102+
OBJS += $(PLTSQL_SER_DIR)/pltsql_readfuncs.o
103+
104+
# pltsql_nodetags.h must be generated before any .o that includes pltsql.h
105+
# The first OBJS entry (src/pl_gram.o) triggers this via implicit ordering,
106+
# but we make it explicit for safety.
107+
src/pl_gram.o: $(PLTSQL_SER_DIR)/pltsql_nodetags.h
108+
109+
# Wrapper .o depends on generated .c files (included via #include)
110+
$(PLTSQL_SER_DIR)/pltsql_outfuncs.o: $(PLTSQL_SER_DIR)/pltsql_outfuncs_gen.c $(PLTSQL_SER_DIR)/pltsql_outfuncs_switch.c
111+
$(PLTSQL_SER_DIR)/pltsql_readfuncs.o: $(PLTSQL_SER_DIR)/pltsql_readfuncs_gen.c $(PLTSQL_SER_DIR)/pltsql_readfuncs_switch.c
89112

90113
PG_CXXFLAGS += -g -Werror -Wfloat-conversion
91114
PG_CXXFLAGS += -Wno-deprecated -Wno-error=attributes -Wno-suggest-attribute=format # disable some warnings from ANTLR runtime header

contrib/babelfishpg_tsql/src/pl_handler.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
#include "schemacmds.h"
9999
#include "session.h"
100100
#include "pltsql.h"
101+
#include "nodes/readfuncs.h"
102+
103+
/* PLtsql node serialization dispatch (defined in pltsql_serialize/) */
104+
extern void pltsql_outNode(StringInfo str, const void *obj);
105+
extern Node *pltsql_parseNodeString(const char *token, int length);
101106
#include "pltsql_partition.h"
102107
#include "pltsql_permissions.h"
103108
#include "pl_explain.h"
@@ -6149,6 +6154,10 @@ _PG_init(void)
61496154

61506155
check_pltsql_support_tsql_transactions_hook = pltsql_support_tsql_transactions;
61516156

6157+
/* PLtsql node serialization hooks */
6158+
outNode_hook = pltsql_outNode;
6159+
parseNodeString_hook = pltsql_parseNodeString;
6160+
61526161
inited = true;
61536162
}
61546163

contrib/babelfishpg_tsql/src/pltsql.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
*
66
* IMPORTANT: Any struct changes here (adding/removing fields, reordering)
77
* must also be reflected in pltsql_serializable_1.h in
8-
* postgresql_modified_for_babelfish/src/include/pltsql/ to keep the
9-
* ANTLR parse tree serialization in sync.
8+
* src/pltsql_serialize/ to keep the ANTLR parse tree serialization for
9+
* procedure caching in sync (babelfishpg_tsql.enable_routine_parse_cache).
1010
*
1111
* Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
1212
* Portions Copyright (c) 1994, Regents of the University of California
@@ -33,6 +33,9 @@
3333
#include "executor/spi.h"
3434
#include "libpq/libpq-be.h"
3535
#include "optimizer/planner.h"
36+
37+
/* PLtsql NodeTag values — generated by gen_pltsql_node_support.pl */
38+
#include "pltsql_serialize/pltsql_nodetags.h"
3639
#include "utils/expandedrecord.h"
3740
#include "utils/plancache.h"
3841
#include "utils/portal.h"

0 commit comments

Comments
 (0)